PHP を使用して、いくつかの jpeg 画像のサイズ変更と再サンプリングに取り組んでいます。500px x 500px を超える画像を取得し、最大辺を 500px にします。これは比較的単純なはずですが、スクリプトを実行するたびに黒い jpeg が作成されます。作成された jpeg のサイズは適切ですが、サイズ変更された画像は含まれていません。GD ライブラリが有効になっており、元のイメージが検出されていることを確認しました。このコード ブロックを 1 日半見ていましたが、運が悪かったのですが、何が表示されていないのでしょうか?
<?php
$testimage = 'SandyCayCaribbeanbeach.jpg';
$testfolder = "testimage/testimage.jpg";
list($orgwidth, $orgheight, $type, $attr) = getimagesize($testimage);
echo "org. width " . $orgwidth . "px" . "<br />";
echo "org. height " . $orgheight . "px" . "<br />";
if($orgwidth > 500 || $orgheight > 500){
if($orgwidth > $orgheight){
header('Content-type: image/jpeg');
$ratio = $orgwidth/500;
$newwidth = floor($orgwidth/$ratio);
$newheight = floor($orgheight/$ratio);
$image_p = imagecreatetruecolor($newwidth, $newheight);
$image = imagecreatefromjpeg($testimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, $testfolder, 100);
}
else{
header('Content-type: image/jpeg');
$ratio = $orgheight/500;
$newheight = floor($orgheight/$ratio);
$newwidth = floor($orgwidth/$ratio);
$image_p = imagecreatetruecolor($newwidth, $newheight);
$image = imagecreatefromjpeg($testimage);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($image_p, $testfolder, 100);
}
}
?>