PHP - 使用 imagecopyresampled() 裁剪图像?
我想使用 imagecreatetruecolor 裁剪图像,它总是裁剪它留下黑色空间,或者缩放太大。我希望图像的宽度恰好为 191 像素,高度为 90 像素,因此我还需要调整图像的大小以及裁剪,因为必须保持比例。好吧,有一些该项目的示例:
调整大小脚本(简化)如下所示:
$src_img=imagecreatefromjpeg($photoTemp);
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height);
$ newImage['crop'] 数组包括:
['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-height']
但我得到的是:
任何人都看到,我是什么做错了吗?
谢谢,迈克。
I'd like to crop an image using imagecreatetruecolor and it always crops it leaving black spaces, or the zoom is too big. I want the image to be exactly 191px wide and 90px high, so I also need to resize the image, as well as crop, because the ratio has to be kept. Well, there are some samples of the project:
The resize script (simplified) goes like this:
$src_img=imagecreatefromjpeg($photoTemp);
list($width,$height)=getimagesize($photoTemp);
$dst_img=imagecreatetruecolor(191, 90);
imagecopyresampled($dst_img, $src_img, 0, 0, $newImage['crop']['x'], $newImage['crop']['y'], $newImage['crop']['width'], $newImage['crop']['height'], $width, $height);
The $newImage['crop'] array includes:
['x'] => $_POST['inp-x']
['y'] => $_POST['inp-x']
['width'] => $_POST['inp-width']
['height'] => $_POST['inp-height']
But what I get is:
Anyone sees, what I'm doing wrong?
Thanks, Mike.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
Try
好吧,我自己发现了问题,代码应该是这样的:
Ok, I found the problem myself, the code should be like this:
还有 imagecrop 函数,它允许您传入一个数组
x
、y
、宽度
和高度
值。There's also the imagecrop function, which allows you to pass in an array with
x
,y
,width
, andheight
values.你也可以这样做,我自己就这样做了,并且有效
(x1,y1)=>作物开始的位置
(x2,y2)=>作物结束的地方
you can do it too, I myself did it, and it works
(x1,y1)=> where crop starts
(x2,y2)=> where crop ends