Bu fonksiyon jpg bulunuyor ve şeffaf (veya değil) gif bulunuyor boyutlandırma için mükemmel benim için çalışıyor:
function resizeImage($originalImage, $toWidth, $toHeight, $isJPG)
{
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale = $width / $toWidth;
$yscale = $height / $toHeight;
// Recalculate new size with default ratio
if ($yscale > $xscale) {
$new_width = round($width * (1 / $yscale));
$new_height = round($height * (1 / $yscale));
} else {
$new_width = round($width * (1 / $xscale));
$new_height = round($height * (1 / $xscale));
}
// Resize the original image
if ($isJPG) {
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
} else {
//$imageResized = imagecreatetruecolor($new_width, $new_height);
//$imageTmp = imagecreatefromgif ($originalImage);
//imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
# what follows is for resizing a gif, transparent or not
# http://ru2.php.net/imagecopyresampled
# load/create images
$imageTmp = imagecreatefromgif($originalImage);
$imageResized = imagecreatetruecolor($new_width, $new_height);
imagealphablending($imageResized, false);
# get and reallocate transparency-color
$transindex = imagecolortransparent($imageTmp);
if ($transindex >= 0) {
$transcol = imagecolorsforindex($imageTmp, $transindex);
$transindex = imagecolorallocatealpha(
$imageResized,
$transcol['red'],
$transcol['green'],
$transcol['blue'],
127
);
imagefill($imageResized, 0, 0, $transindex);
}
# resample
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
# restore transparency
if ($transindex >= 0) {
imagecolortransparent($imageResized, $transindex);
for ($y = 0; $y < $new_height; ++$y) {
for ($x = 0; $x < $new_width; ++$x) {
if (((imagecolorat($imageResized, $x, $y) >> 24) & 0x7F) >= 100) {
imagesetpixel(
$imageResized,
$x,
$y,
$transindex
);
}
}
}
}
# save GIF
imagetruecolortopalette($imageResized, true, 255);
imagesavealpha($imageResized, false);
}
return $imageResized;
}
Orijinal fonksiyonu PhpToys 1.0 ve şeffaf. Videoları ile çalışır part this PHP docs comment geliyor.