开发者

imagecopyresampled to resize and crop an image - not returning the expected result

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜