开发者

resize and crop a image

开发者 https://www.devze.com 2023-04-12 01:50 出处:网络
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.

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 width src_w and height src_h at position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_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 as src_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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号