简体   繁体   English

如何使用php正确裁剪图像?

[英]How can I crop an image properly with php?

I am trying to crop an image on PHP . 我正在尝试在PHP上裁剪图像。 I saw that there is a function for doing that on PHP : imagecrop . 我看到有一个在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. 在这里,我希望图像的裁剪从图像的左角开始,并获得我在上方放置的width和height的值。

But it just resize my image, not crop it. 但这只是调整我的图像大小,而不是裁剪图像。 What am I doing wrong? 我究竟做错了什么?

EDIT: 编辑:

I tried also with the function imagecopyresampled . 我也尝试了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...) 我只是在此函数中有问题,这就是为什么要避免其余部分(保存和imagetruecolor以及其余部分...)

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 . 我必须输入相同的dst_wsrc_w值。 The same to dst_h and src_h . dst_hsrc_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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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