Actually I don't know that there is available any php function to crop and re-size a image with given parameters(x1,y1,x2,y2,width,height) and image name.
I am looking like below function which create new image with given parameters from existing one.
newimg(x1开发者_如何学JAVA,y1,x2,y2,width,height,image);
Currently i have got all above parameters with javascript but now i want to crop image according to above parameters.
imagecopyresampled()
can do just that:
imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity.
In other words, imagecopyresampled() will take an rectangular area from
src_image
of widthsrc_w
and heightsrc_h
at position (src_x
,src_y
) and place it in a rectangular area ofdst_image
of widthdst_w
and heightdst_h
at position (dst_x
,dst_y
).If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if
dst_image
is the same assrc_image
) but if the regions overlap the results will be unpredictable.
In your case (untested):
function newimg($x1, $y1, $x2, $y2, $width, $height, $image) {
$newimg = ... // Create new image of $width x $height
imagecopyresampled(
$newimg, // Destination
$image, // Source
0, // Destination, x
0, // Destination, y
$x1, // Source, x
$y1, // Source, y
$width, // Destination, width
$height, // Destination, height
$x2 - $x1, // Source, width
$y2 - $y1 // Source, height
);
return $newimg;
}
The main image libraries for PHP are GD and ImageMagick. Both can resize and crop images.
精彩评论