Good day. Help me find a bug.
PHP code available
function ResizeImage($image_from, $image_to, $pixels = 1, $scale = 1, $quality = 90) { if($pixels == 0) $pixels = 100; $h = $os = $originalsize = getimagesize ( $image_from ); if ($originalsize [0] > $pixels or $originalsize [1] > $pixels) { switch ($scale) { // по высоте case 1 : $fitheight = $pixels; $fitwidth = $h [0] * $fitheight / $h [1]; break; // по ширине case 2 : $fitwidth = $pixels; $fitheight = $h [1] * $fitwidth / $h [0]; break; // по наибольшей стороне case 3 : if (($h [0]) > ($h [1])) { $fitwidth = $pixels; $fitheight = $h [1] * $fitwidth / $h [0]; } else { $fitheight = $pixels; $fitwidth = $h [0] * $fitheight / $h [1]; } break; } if ($os [2] == 2 or ($os [2] >= 9 AND $os [2] <= 12)) $i = ImageCreateFromJPEG ( $image_from ); if ($os [2] == 3) $i = ImageCreateFromPng ( $image_from ); if ($os [2] == 1) $i = ImageCreateFromGif ( $image_from ); $o = ImageCreateTrueColor ( $fitwidth, $fitheight ); if ($os [2] == 3) { imagealphablending( $o, false); imagesavealpha( $o, true); } imagefilledrectangle($o, 0, 0, $fitwidth, $fitheight, imagecolorallocate($o, 255, 255, 255)); imagecopyresampled ( $o, $i, 0, 0, 0, 0, $fitwidth, $fitheight, $h [0], $h [1] ); imagejpeg ( $o, $image_to, $quality ); chmod ( $image_to, 0777 ); imagedestroy ( $o ); imagedestroy ( $i ); return 2; } if ($originalsize [0] <= $pixels AND $originalsize [1] <= $pixels) { if ($os [2] == 2 or ($os [2] >= 9 AND $os [2] <= 12)) $i = ImageCreateFromJPEG ( $image_from ); if ($os [2] == 3) $i = ImageCreateFromPng ( $image_from ); if ($os [2] == 1) $i = ImageCreateFromGif ( $image_from ); if ($os [2] == 3) { imagealphablending( $i, false); imagesavealpha( $i, true); } imagejpeg ( $i, $image_to, $quality ); chmod ( $image_to, 0777 ); return 1; } }
When loading a PNG image, the background turns black.
imagealphablending( $i, false); imagesavealpha( $i, true); What is wrong in the code? Thank!