如何獲取已上傳到服務器的500x500(或任何大小)圖像,並根據定義的特定x,y座標生成新圖像?例如(0,0)至(50,0); (0,50)至(50,50)。我不想將圖像大小調整爲50x50像素,而是想抓取圖像的左上角部分,並在某種意義上將其「裁剪」爲縮略圖。PHP:使用X,Y座標創建縮略圖圖像
我該如何去做這個在PHP中?
如何獲取已上傳到服務器的500x500(或任何大小)圖像,並根據定義的特定x,y座標生成新圖像?例如(0,0)至(50,0); (0,50)至(50,50)。我不想將圖像大小調整爲50x50像素,而是想抓取圖像的左上角部分,並在某種意義上將其「裁剪」爲縮略圖。PHP:使用X,Y座標創建縮略圖圖像
我該如何去做這個在PHP中?
你想用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);
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");
我見過幾個方法可以做到這一點。如果您想在飛行中生成拇指,您可以使用:
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/
或
你想腳本生成縮略圖並保存到稍後使用的文件夾?或者你想讓它在飛行中生成? – Shattuck
我想將它們保存到以後使用的文件夾中。 – Aaron
查看http://bgallz.org/270/php-create-thumbnail-images/ – Shattuck