在GD库中调整大小后裁剪图像
我需要首先按比例调整图像大小(宽度是重要的尺寸),然后裁剪以去除任何多余的高度,然后将新版本存储在目录中。
我已经成功地调整了大小,最终在我的目录中得到了正确宽度的图像。我需要把多余的高度剪掉。但我不知道我需要在哪里做。我是否需要以某种方式使用 copyimageresampled ?我想裁剪所有图像,使它们的高度为 50 像素。
这是我迄今为止调整大小的内容:
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
ImageJpeg($dst, 'images/' . $_FILES['image']['name']);
I need to first resize an image proportionally (the width is the important dimension) and then crop afterwards to chop of any excess height and then store the new version in a directory.
I've managed to do the resizing fine and I end up with images that are the correct width in my directory. Somehwere in here I need to crop the excess height off. But I can't work out where I need to do it. Do I need to use copyimageresampled somehow. I want to crop all images so they're 50px in height.
Here's what I have so far fo the resizing:
$src = ImageCreateFromJpeg($upfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
ImageJpeg($dst, 'images/' . $_FILES['image']['name']);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我所追求的。一个 2 阶段过程。诀窍是将图像调整为临时图像,然后裁剪它:
http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html
This is what I was after. A 2 stage process. The trick is to resize the image in to a temporary image and then crop it:
http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html
这可能会帮助您在调整大小后进行裁剪: 911-need-code-help
This might help you to crop after resize: 911-need-code-help
裁剪就像使用 GD 调整大小一样
一些示例代码:
您可以定义裁剪宽度和宽度。高度,并且应该全部设置好。正如您所看到的,它只不过是调整大小而已。
参考:http://www. johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/
Croping is just like resizing with GD
Some sample code:
You define your crop width & height, and should be all set. Its not much more than a resize as you can see.
Reference: http://www.johnconde.net/blog/cropping-an-image-with-php-and-the-gd-library/