Dave Chenell
Dave Chenell

Reputation: 601

PHP create thumbnail around user specified point

So I have a user image and a user specified point of interest based off of that image.

I have retrieved that point of interest from a XML file and put them into variables so I have 2 points.

x= 246 y= 73

My question: How can I crop a 45 by 53 thumbnail image with the above coordinates being the center-point of the thumbnail? I don't want the image to scale at all, just crop.

Upvotes: 0

Views: 281

Answers (1)

marsbear
marsbear

Reputation: 1459

With GD it should work this way:

// Open source image
$srcImg = imagecreatefromjpeg ( $filename );

// Create new image for the cropped version
$destImg = imagecreate ( 45, 53 );

// Calculate the upper left of the image-part we want to crop
$startX = x - 45 / 2;
$startY = y - 53 / 2;

// Copy image part into the new image
imagecopy ( $destImg, $srcImg , 0, 0, $startX, $startY, 45, 53 );

// Write the new image with quality 90
imagejpeg($destImg, 'newfile.jpg', 90);

You might want to check for rounded coordinates as your image might blur if you don't. You should check if your cropped image coordinates are well within your original image if the user lets say chooses a corner as poi.

Upvotes: 2

Related Questions