KOD
<?php
function forceConstraints($srcFile, $srcType, $dstType, $dstWidth, $dstHeight, $dstPath)
{
if ($srcType == ".jpg")
$handle = @imagecreatefromjpeg('upload/'.$srcFile);
else if ($srcType == ".png")
$handle = @imagecreatefrompng('upload/'.$srcFile);
else if ($srcType == ".gif")
$handle = @imagecreatefromgif('upload/'.$srcFile);
else
{ echo "error1";
return false;}
if (!$handle) { echo "error5";
return false;}
$srcWidth = @imagesx($handle);
$srcHeight = @imagesy($handle);
$newHandle = @imagecreatetruecolor($dstWidth, $dstHeight);
$fill_color = imagecolorallocate($newHandle, 255, 0, 0);
imagefill($newHandle, 0, 0, $fill_color);
if (!$newHandle)
{ echo "error2";
return false;}
$ratio_orig = $srcWidth/$srcHeight;
if ($dstWidth/$dstHeight > $ratio_orig) {
$dstWidth = $dstHeight*$ratio_orig;
} else {
$dstHeight = $dstWidth/$ratio_orig;
}
if ($dstWidth<100)
($poziom = (100 - $dstWidth)/2);
if ($dstHeight<100)
($pion = (100 - $dstHeight)/2);
if (!@imagecopyresampled($newHandle, $handle, $poziom, $pion, 0, 0, $dstWidth, $dstHeight, $srcWidth, $srcHeight))
{ echo "error3";
return false;}
@imagedestroy($handle);
if ($dstType == ".png")
@imagepng($newHandle, 'upload/'.$dstPath);
else if ($dstType == ".jpg")
@imagejpeg($newHandle, 'upload/'.$dstPath);
else if ($dstType == ".gif")
@imagegif($newHandle, 'upload/'.$dstPath);
else
{ echo "error4";
return false;}
@imagedestroy($newHandle);
return true;
}
?>
To co dopisałem to dwie linijki, pod "
$newHandle = @imagecreatetruecolor($dstWidth, $dstHeight);".
Zmienić kolor wypełnienia możesz edytując parametry funkcji
imagecolorallocate