仅显示图像的某些部分并使用GD调整其大小

问题描述:

I currently have a script ready that resizes a whole image with GD but I need to get a specific part of an image to display and resize only that specific part.

This is the image:

http://craffy.gdscei.com/enjikaka.png

This is what needs to be displayed, took out the rest with Photoshop:

http://craffy.gdscei.com/enjikakap.png

The final image needs to be 150x150.

This is the script i tried:

<?php

    $srcp = imagecreatefrompng("enjikaka.png");
    $destp = imagecreate(150, 150);
    imagecopyresampled($destp, $srcp, 0, 0, -8, -8, 150, 150, 64, 32);
    header('Content-type: image/png');
    imagepng($destp);

?>

But this one does not pick the correct part of the image. Can anyone help me here?

Why the (-8, -8)? Those should be the upper left corner of your area to copy. It should be 8, 8. And the last two parameters: (64, 32) are the width and height of your source area. Those should be 8, 8 too.

imagecopyresampled ($destp, $srcp, 0, 0, 8, 8, 150, 150, 8, 8);

I assume here that your source image is built up by 8x8 units. You should check the coordinates in photosop.

I suggest you read the documentation of the function. That shopuld be the first thing you do when things do not go as you expected.

$srcp = imagecreatefrompng("enjikaka.png");
$destp = imagecreate(150, 150);
imagecopy($despt, $srcp, $dst_x , $dst_y , $src_x , $src_y , $src_w , $src_h);

I think you should include this call to imagecopy in your script, which should handle the cropping of the image.