在PHP中裁剪图像

use*_*228 16 php gd image crop

我想在PHP中裁剪图像并保存文件.我知道你应该使用GD库,但我不知道如何.有任何想法吗?

谢谢

And*_*ris 24

您可以imagecopy用来裁剪图像的所需部分.命令如下:

imagecopy  ( 
    resource $dst_im - the image object ,
    resource $src_im - destination image ,
    int $dst_x - x coordinate in the destination image (use 0) , 
    int $dst_y - y coordinate in the destination image (use 0) , 
    int $src_x - x coordinate in the source image you want to crop , 
    int $src_y - y coordinate in the source image you want to crop , 
    int $src_w - crop width ,
    int $src_h - crop height 
)
Run Code Online (Sandbox Code Playgroud)

来自PHP.net的代码- 从源图像中裁剪出80x40像素的图像

<?php
// Create image instances
$src = imagecreatefromgif('php.gif');
$dest = imagecreatetruecolor(80, 40);

// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);

// Output and free from memory
header('Content-Type: image/gif');
imagegif($dest);

imagedestroy($dest);
imagedestroy($src);
?>
Run Code Online (Sandbox Code Playgroud)