2011-10-10 220 views
0

其实我不知道有任何PHP函数可以用给定参数(x1,y1,x2,y2,宽度,高度)和图像名称裁剪和重新调整图像大小。调整大小和裁剪图像

我看起来像下面的函数,从现有的参数创建新的图像与给定的参数。

newimg(x1,y1,x2,y2,width,height,image); 

目前我已经得到了所有以上参数与JavaScript,但现在我想按照上述参数裁剪图像。

+0

https://gist.github.com/880506 – Phil

回答

3

imagecopyresampled()可以做到这一点:

imagecopyresampled()拷贝一个图像到另一个图像的矩形部分,平滑地内插像素值,因此,特别地,减小图像的大小而仍然保持非常清晰。

换句话说,imagecopyresampled()将采取矩形区域从宽度src_wsrc_image和在位置高度src_hsrc_xsrc_y)并将其放置在的的宽度dst_wdst_image的矩形区域和高度dst_h在位置(dst_x,dst_y)。

如果源和目标坐标以及宽度和高度不同,则将执行适当的图像片段的伸展或收缩。坐标指的是左上角。此功能可用于复制同一图像中的区域(如果dst_imagesrc_image相同),但如果区域重叠,则结果将不可预知。


在你的情况(未经测试):

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; 
}