我正在努力調整和重新採樣使用PHP的一些jpeg圖像。它需要任何大於500像素×500像素的圖像,並使最大側500像素。這應該是相對簡單的,但每次運行腳本時,它都會產生黑色的jpeg。創建的jpeg具有適當的尺寸,但不包括調整大小的圖像。 GD庫已啓用,我確定它正在查找原始圖像。我一直在尋找這塊代碼一天半沒有運氣,我沒有看到什麼?imagecopyresampled使黑盒子,但不是重採樣圖像
<?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);
}
}
?>
打開錯誤報告後,它顯示在第20行我的高度和寬度變量未定義。他們是$寬度和$高度,但應該是$ orgwidth和$ orgheight。感謝錯誤報告提示,即時通訊新的和完全忘了它。 – Andalinmicphew 2011-02-02 17:12:27