我有$this->_tempFile
,其中包含上传图片文件的临时路径。
现在我希望缩放图像,它会调整大小但不能正确缩放。输出是一个大的黑色正方形,底部高度和图像的一半(宽度)。我尝试了其他图像,结果就是这些尺寸: 293px×453px,为什么我不知道?
这是我scaleImage();
scaleImage(900, 582);
public function scaleImage($width, $height){
$rel_difference = array('width'=>0, 'height'=>0);
if($width > 604) $rel_difference['width'] = ($width-604)/604;
if($height > 453) $rel_difference['height'] = ($height-453)/453;
asort($rel_difference);
$tmpname = $this->_tempFile;
$newwidth = $width/(1+end($rel_difference));
$newheight = $height/(1+end($rel_difference));
$newwidth = round($newwidth);
$newheight = round($newheight);
$jpeg_quality = 90;
switch(exif_imagetype($tmpname)) {
case IMAGETYPE_GIF:
$img_r = imagecreatefromgif($tmpname);
break;
case IMAGETYPE_JPEG:
$img_r = imagecreatefromjpeg($tmpname);
break;
case IMAGETYPE_PNG:
$img_r = imagecreatefrompng($tmpname);
break;
default:
echo json_encode(array('error' => 'Not an image!'));
exit(0);
break;
}
$dst_r = ImageCreateTrueColor( $newwidth, $newheight );
imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0, $newwidth , $newheight, $width, $height);
imagejpeg($dst_r,$tmpname,$jpeg_quality);
}
这里有什么问题?
答案 0 :(得分:1)
我怀疑你不小心翻过你传递的参数的高度和宽度。
scaleImage(582, 900);