imagecopyresampled 来调整图像大小和裁剪图像 - 未返回预期结果

发布于 2024-10-10 15:17:08 字数 671 浏览 0 评论 0原文

imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

这就是我想要做的:我有一个大小为 600x1000px 的图像,我想在将该图像大小调整为 300x500px 后创建一个 100x100px 的拇指,拇指方块左上角点的 x 坐标应为 100( src x) 和 120(src y)。

根据我从手册中了解到的内容,该命令应该是

$dst_image = imagecreatetruecolor(100,100);
$src_image = imagecreatefromjpeg('/home/sandbox/imagetoresize.jpg');
imagecopyresized ($dst_image, $src_image, 0, 0, 100, 120, **300 , 500 , 600 , 1000** )

它可以很好地裁剪图像,但没有正确调整其大小。我从来没有让它与我在图像编辑器(GIMP)中看到的相匹配。我做错了什么?我确认所有数字都是正确的,但无论我做什么,它总是向上或向下移动。

imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

This is what I want to do: I have an image that's 600x1000px in size, and I want to create a thumb that's 100x100px after resizing that image to 300x500px, the x coordinate for the top left point of the thumb square should be at 100(src x) and 120(src y).

According to what I understand from the manual, the command should be

$dst_image = imagecreatetruecolor(100,100);
$src_image = imagecreatefromjpeg('/home/sandbox/imagetoresize.jpg');
imagecopyresized ($dst_image, $src_image, 0, 0, 100, 120, **300 , 500 , 600 , 1000** )

It is cropping the image just fine, but it isn't resizing it correctly. I never got it to match what I see in my image editor (the GIMP). What am I doing wrong? I confirmed that all the numbers are correct, but it's always shifted up or down no matter what I do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

打小就很酷 2024-10-17 15:17:08

这是我使用 PHP GD 编写的函数的链接,该函数可将任何大小的图像调整为任意大小。它有一个解释,以及使用裁剪或信箱来适应目标宽高比的选项。

http://www.spotlesswebdesign.com/blog.php?id=1

更新

它应该看起来更像这样。

$dst_image = imagecreatetruecolor(100,100);
$src_image = imagecreatefromjpeg('/home/sandbox/imagetoresize.jpg');
imagecopyresized ($dst_image, $src_image, 0, 0, 100, 120, 100, 100, 400, 400);

从源中获取 400x400 的正方形,并将其复制到目标中的 100x100 的正方形中。源方块的左上角是 100 x 和 120 y。 x 和 y 表示距左上角的像素数。

Here's a link to a function I wrote using PHP GD to resize any sized image to any arbitrary size. It has an explaination, and options to use crop-to-fit or letterboxing to fit the destination aspect ratio.

http://www.spotlesswebdesign.com/blog.php?id=1

update

it should look more like this.

$dst_image = imagecreatetruecolor(100,100);
$src_image = imagecreatefromjpeg('/home/sandbox/imagetoresize.jpg');
imagecopyresized ($dst_image, $src_image, 0, 0, 100, 120, 100, 100, 400, 400);

takes a 400x400 square from the source, and copies it into a 100x100 square in the destination. the top-left of the source square is 100 x and 120 y. x and y represent number of pixels from the top left corner.

云巢 2024-10-17 15:17:08

是的,这很好地解决了问题。

对于 Google 员工:我基本上需要做的是将源宽度和源高度链接到我将在源图像中裁剪的区域的实际宽度和高度。这意味着代码需要是:

imagecopyresized ($dst_image, $src_image, 0, 0, 200, 240, 100, 100, 200, 200);

所以变量实际上意味着以下内容:
$src_x = 原图中正方形左上角点的 x 坐标。由于原始尺寸是要从中提取拇指的调整大小版本的两倍,因此该值将为 200 ((original_width / resized_width) * x)。

$src_y = 相同的东西,但是使用 y 坐标。

$dst_w = 生成的缩略图的宽度 - 100。

$dst_h = 生成的缩略图的高度 - 100。

$src_w = 我将从原始图像中裁剪的区域的宽度 ((original_width / resized_width) * $dst_w)

$ src_h = 我将从原始内容中裁剪的区域的高度 ((original_width / resized_width) * $dst_h)


dqhendricks:非常感谢您的帮助,我真的很感激。我为此绞尽脑汁好几个小时。

Yes, that fixed it nicely.

For Googlers: What I basically needed to do is to have the source width and source height link to the actual width and height of the area that I will crop in the source image. Which means that the code needed to be:

imagecopyresized ($dst_image, $src_image, 0, 0, 200, 240, 100, 100, 200, 200);

So the variables actually mean the following:
$src_x = the x co-ordinate of the top left point of the square in the original. Since the original is twice the size of the resized version from which the thumb is to be extracted, this will be 200 ((original_width / resized_width) * x).

$src_y = the same thing, but with the y co-ordinate.

$dst_w = the width of the generated thumbnail - 100.

$dst_h = the height of the generated thumbnail - 100.

$src_w = the width of the area that I will crop from the original ((original_width / resized_width) * $dst_w)

$src_h = the height of the area that I will crop from the original ((original_width / resized_width) * $dst_h)


dqhendricks: Thanks a lot for your help, I really appreciate it. I was scratching my head over this for hours.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文