如果上传的图像太大,我正在尝试在 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
}
但我没有得到一个对象回来,我不知道为什么。