tags:

views:

64

answers:

2

I am using the following code snippet in order to crop a image?

   function crop($width,$height) {
       $new_image = imagecreatetruecolor($width, $height);
       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $this->getWidth(), $this->getHeight(), $width, $height );
       $this->image = $new_image;
   }

Here, $this->image is the original image $this->getWidth() and $this->getHeight() holds the original dimensions of the picture, where as $width and $height, is the crop area.

But for some reason, the crop image is resized(we can almost say it is resized).

How to fix this?

+3  A: 

Well, while you are giving the source dimensions, you are giving the dimensions of entire image, so it is resizing instead of cropping.

This should solve your problem

   function crop($width,$height) {
       $new_image = imagecreatetruecolor($width, $height);
       imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $width, $height );
       $this->image = $new_image;
   }
Starx
@Starx, Thanks, this works, but @sjoerd said to use imagecopy. Can you tell me how to do that
Starx
@q234e, just replace `imagecopyresampled` with `imagecopy`
Starx
A: 

This is what imagecopyresampled does: it rescales the image and does not crop it. Try imagecopy instead.

Sjoerd
No it's not. `imagecopy` and `imagecopyresampled` work the same way. The difference between them, is that `imagecopy` just "scales" the pixels up or down depending on the move. `imagecopyresampled` interpolates pixel values when resizing (so it makes a "smoother" and better looking image). But both do crop and/or resize the image...
ircmaxell