缩放图像无法正常工作?
我有一个 $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);
}
这里出了什么问题?
I have a $this->_tempFile
that holds the temp path of an uploaded image file.
Now I wish to scale an image, it do resizes it but it doesn't scale right. The output is a big black square the bottom at the height and half of the image (width). And i tried with other images, and all turns out being these dimensions:
293px × 453px , why i don't know?
Here's my function for the 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);
}
What is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您不小心翻转了您传递的参数中的高度和宽度。也如此
I suspect you have accidentally flipped the height and width in the parameters you're passing. So do