2011-11-17 118 views
1

如何获取已上传到服务器的500x500(或任何大小)图像,并根据定义的特定x,y坐标生成新图像?例如(0,0)至(50,0); (0,50)至(50,50)。我不想将图像大小调整为50x50像素,而是想抓取图像的左上角部分,并在某种意义上将其“裁剪”为缩略图。PHP:使用X,Y坐标创建缩略图图像

我该如何去做这个在PHP中?

+0

你想脚本生成缩略图并保存到稍后使用的文件夹?或者你想让它在飞行中生成? – Shattuck

+0

我想将它们保存到以后使用的文件夹中。 – Aaron

+0

查看http://bgallz.org/270/php-create-thumbnail-images/ – Shattuck

回答

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); 
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函数中设置质量参数。

或者,如果你想将图像拇指保存到一个目录我想看看:

http://bgallz.org/270/php-create-thumbnail-images/

http://www.imagemagick.org/script/index.php