如何拍摄已上传到服务器的500x500(或任何尺寸)图像,并根据定义的特定x,y坐标生成新图像?例如(0,0)到(50,0); (0,50)至(50,50)。我想抓住图像的左上角,而不是将图像尺寸调整到50x50px,并且在某种意义上“裁剪”它以用作缩略图。
如何在PHP中执行此操作?
答案 0 :(得分:1)
您想使用imagecopy。首先使用您想要的尺寸创建图像,然后使用imagecopy将源图像的一部分放入新图像中:
// use whatever mechanism you prefer to load your source image into $image
$width = 50;
$height = 50;
// Define your starting coordinates in the source image.
$source_x = 20;
$source_y = 100;
$new_image = imagecreatetruecolor($width, $height);
imagecopy($new_image, $image, 0, 0, $source_x, $source_y, $width, $height);
// Now $new_image has the portion cropped from the source and you can output or save it.
imagejpeg($new_image);
答案 1 :(得分:0)
http://www.php.net/manual/en/function.imagick-cropthumbnailimage.php#81547
$image = new Imagick($path."test1.jpg");
$image->cropThumbnailImage(160,120); // Crop image and thumb
$image->writeImage($path."test1.jpg");
答案 2 :(得分:0)
我已经看到了一些方法来做到这一点。如果您想动态生成拇指,可以使用:
function make_thumb($src,$dest,$desired_width)
{
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height*($desired_width/$width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
/* copy source image at a resized size */
imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest);
}
您还可以在imagejpeg函数中设置quality参数。
或者,如果您想将图像缩略图保存到我想看的目录:
http://bgallz.org/270/php-create-thumbnail-images/
或