简体   繁体   中英

How can I crop an image properly with php?

I am trying to crop an image on PHP . I saw that there is a function for doing that on PHP : imagecrop . Here is the code in which I am trying to crop the image.

$image = imagecreatefromjpeg(//Path of the image);
$croppedImage = imagecrop($image, array("x"=>0,"y"=>0,"width"=>100,"height"=>100));
imagejpeg($croppedImage, //Path in which the image will be stored); 

Here I want that the crop of the image starts in the left corner of the image and get the values of width and height that I put above.

But it just resize my image, not crop it. What am I doing wrong?

EDIT:

I tried also with the function imagecopyresampled . Here it is what I have tried:

dst_image(Destination image link resource) = newImage; //Here the new image that I want to create.

src_image(Source image link resource) = image; //Here the original image.

//Here 0,0 because I want that the new image crop starts in the left corner of the original image.

dst_x(x-coordinate of destination point) = 0;

dst_y(y-coordinate of destination point) = 0;

//Here 0,0 because I want that the crop starts on the 0,0 of the original image

src_x(x-coordinate of source point) = 0;

src_y(y-coordinate of source point) = 0;

dst_w(Destination width) = 150; //The new width of the crop image.

dst_h(Destination height)  = 150; //The new height of the crop image.

src_w(Source width) = 500; //The original width of the image.

src_h(Source height) = 500; //The original height of the image.

So finally the function will be like:

$b = imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w , $dst_h , $src_w , $src_h ); 

I just have problems in this function it is why avoid the rest (save and imagetruecolor and the rest...)

This function give to me the result expected but the new image it is black, why?

Thanks in advance!

try this:

$dst_x = 0;   // X-coordinate of destination point
$dst_y = 0;   // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // $src_x + $dst_w Crop end X position in original image
$src_h = 220; // $src_y + $dst_h Crop end Y position in original image

// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/cropped_whatever.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');

另外,您可以使用的功能imagecrop imagick

Finally, I got the solution of my problem. I had to put the same value of dst_w and src_w . The same to dst_h and src_h . ¡¡And it works!!

The final solution it is the following:

$b = imagecopyresampled ($dst_image, $src_image, 0, 0, 0, 0, 150, 150, 150, 150);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM