Gabriel Santos
Gabriel Santos

Reputation: 4974

Crop distorting / scaling image

I have the follow PHP code:

$w = 300; // Width of new image
$h = 300; // Height of new image
$oh = 540; // Original file height
$ow = 720; // Original file width
$x = 196;
$y = 50;

$image = imagecreatefromjpeg('fileToCrop.jpg');
$cropped_image = imagecreatetruecolor($w, $h);
imagecopyresampled($cropped_image, $image, 0, 0, $x, $y, $ow, $oh, $w, $h);

imagejpeg($cropped_image, 'fileToCrop.jpg', 100);

And want to crop the image, but my images are distorting / higher than original, eg:

Original:

enter image description here

Cropped ("N" for "Not" are showing):

enter image description here

I can't see what is wrong with my code, and what are happening to images goes bigger..

Upvotes: 2

Views: 474

Answers (2)

emartel
emartel

Reputation: 7773

You inverted the last 4 parameters of imagecopyresampled

bool imagecopyresampled ( 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 )

Change it for

imagecopyresampled($cropped_image, $image, 0, 0, $x, $y, $w, $h, $ow, $oh);

But in fact, are you sure you're not looking for a straight copy of the cropped region?

imagecopy($cropped_image, $image, 0, 0, $x, $y, $ow, $oh);

Upvotes: 1

fedmich
fedmich

Reputation: 5381

because you omitted the quality on imagejpeg() it defaults to 75% http://php.net/manual/en/function.imagejpeg.php

can you try adding 95 on 3rd parameter?

imagejpeg($cropped_image, 'fileToCrop.jpg', 95);

P.S. If you can use a 3rdparty php script I'll suggest you just use timthumb http://www.binarymoon.co.uk/projects/timthumb/

if you need a change of cropping location,

http://www.binarymoon.co.uk/2010/08/timthumb-part-4-moving-crop-location/

Upvotes: 0

Related Questions