この小さな関数を作成して、より大きなjpg / jpeg / pngソースのサムネイル画像を生成しました。これは、jpg / jpeg画像で完全に機能しますが、png画像のサイズによっては、不確定なポイントで関数がクラッシュします。小さな300x200の画像は機能しますが、2880x1800のようなものは機能しません。
これが私の(注釈付き)関数です:
function make_thumb($filename, $destination, $desired_width) {
$extension = pathinfo($filename, PATHINFO_EXTENSION);
// Read source image
if ($extension == 'jpg' || $extension == 'jpeg') {
$source_image = imagecreatefromjpeg($filename);
} else if ($extension == 'png') {
$source_image = imagecreatefrompng($filename); // I think the crash occurs here.
} else {
return 'error';
}
$width = imagesx($source_image);
$height = imagesy($source_image);
$img_ratio = floor($height / $width);
// Find the "desired height" of this thumbnail, relative to the desired width
$desired_height = floor($height * ($desired_width / $width));
// Create a new "virtual" image
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
// Copy source image at a resized size
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
// Create the physical thumbnail image to its destination
if ($extension == 'jpg' || $extension == 'jpeg') {
$source_image = imagejpeg($virtual_image, $destination);
} else if ($extension == 'png') {
$source_image = imagepng($virtual_image, $destination, 1);
} else {
return 'another error';
}
}
私と同様の問題について言及していることがわかった唯一のドキュメントはこれでした。これは私の問題ですか?解決策はありますか?なぜこれを行うのですか?