php imagecopyresampled 以调整大小和裁剪图像 - 不返回预期结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4631931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
imagecopyresampled to resize and crop an image - not returning the expected result
提问by David
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).
这就是我想要做的:我有一个大小为 600x1000px 的图像,我想在将该图像调整为 300x500px 后创建一个 100x100px 的拇指,拇指方块左上角的 x 坐标应为 100( src x) 和 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.
它可以很好地裁剪图像,但没有正确调整其大小。我从来没有让它与我在图像编辑器(GIMP)中看到的相匹配。我究竟做错了什么?我确认所有数字都是正确的,但无论我做什么,它总是向上或向下移动。
回答by dqhendricks
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.
这是我使用 PHP GD 编写的将任何大小的图像调整为任意大小的函数的链接。它有一个解释,以及使用裁剪以适应或信箱以适应目标纵横比的选项。
http://www.spotlesswebdesign.com/blog.php?id=1
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.
从源中获取一个 400x400 的正方形,并将其复制到目标中的 100x100 正方形中。源方块的左上角是 100 x 和 120 y。x 和 y 表示距左上角的像素数。
回答by David
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:
对于 Google 员工:我基本上需要做的是将源宽度和源高度链接到我将在源图像中裁剪的区域的实际宽度和高度。这意味着代码需要:
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_x = 原始正方形左上角的 x 坐标。由于原始文件的大小是要从中提取缩略图的调整大小版本的两倍,因此这将是 200 ((original_width / resized_width) * x)。
$src_y = the same thing, but with the y co-ordinate.
$src_y = 相同的东西,但使用 y 坐标。
$dst_w = the width of the generated thumbnail - 100.
$dst_w = 生成缩略图的宽度 - 100。
$dst_h = the height of the generated thumbnail - 100.
$dst_h = 生成缩略图的高度 - 100。
$src_w = the width of the area that I will crop from the original ((original_width / resized_width) * $dst_w)
$src_w = 我将从原始裁剪区域的宽度 ((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)
$src_h = 我将从原始裁剪区域的高度 ((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.
dqhendricks:非常感谢您的帮助,我真的很感激。我为此苦恼了好几个小时。