我試圖在PHP中調整圖像大小,如果上傳的圖像太大。我創建了一個函數,應該調整的文件,然後(希望)返回數組 - 除了它不工作:(作爲一個數組返回一個圖像
private function _resizeImage($image, $width = 780, $height = 780) {
$imgDetails = GetImageSize($image["tmp_name"]);
// Content type
//header("Content-Type: image/jpeg");
//header("Content-Disposition: attachment; filename=resized-$image");
// Get dimensions
$width_orig = $imgDetails['0'];
$height_orig = $imgDetails['1'];
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
switch ($imgDetails['2'])
{
case 1: $newImage = imagecreatefromgif($image["tmp_name"]); break;
case 2: $newImage = imagecreatefromjpeg($image["tmp_name"]); break;
case 3: $newImage = imagecreatefrompng($image["tmp_name"]); break;
default: trigger_error('Unsupported filetype!', E_USER_WARNING); break;
}
if (!$newImage) {
// We get errors from PHP's ImageCreate functions...
// So let's echo back the contents of the actual image.
readfile ($image);
} else {
// Create the resized image destination
$thumb = @ImageCreateTrueColor ($width, $height);
// Copy from image source, resize it, and paste to image destination
@ImageCopyResampled ($thumb, $newImage, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output resized image
//ImageJPEG ($thumb);
}
// Output
$newFile = imagejpeg($thumb, null, 100);
return $newFile;
}
這是由叫做:
if($imgDetails['0'] > 780 || $imgDetails['1'] < 780) {
$file = $this->_resizeImage($file); // Resize image if bigger than 780x780
}
但我沒有得到一個對象回來了,我不知道爲什麼。
[imagejpeg](http://php.net/manual/en/function.imagejpeg.php)返回布爾值。不是一個對象。 – 2013-03-08 19:05:39