2012-08-31 37 views
5

我有一堆產品預覽圖片,但它們不是相同尺寸Imagecrop不保存圖像

所以我想知道,是否有可能作物在旅途中的圖像,而不保存它?

這兩個環節應該說明我的意思:

http://xn--nstvedhandel-6cb.dk/alpha_1/?side=vis_annonce&id=12

http://xn--nstvedhandel-6cb.dk/alpha_1/?side=vis_annonce&id=13

+2

是的,但你可能*想要*保存它,因爲圖像調整大小操作是非常昂貴的,而且你不希望每次都必須重複執行它們。 – deceze

回答

5

是的,它是可能這裏是我如何做到這一點:

//Your Image 
$imgSrc = "image.jpg"; 
list($width, $height) = getimagesize($imgSrc); 
$myImage = imagecreatefromjpeg($imgSrc); 

// calculating the part of the image thumbnail 
if ($width > $height) 
{ 
    $y = 0; 
    $x = ($width - $height)/2; 
    $smallestSide = $height; 
} 
else 
{ 
    $x = 0; 
    $y = ($height - $width)/2; 
    $smallestSide = $width; 
} 

// copying the part into thumbnail 
$thumbSize = 100; 
$thumb = imagecreatetruecolor($thumbSize, $thumbSize); 
imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); 

//final output 
header('Content-type: image/jpeg'); 
imagejpeg($thumb); 

這不是一個檸補光燈操作,和其他人一樣我也reccomend您保存縮略圖它創建到文件系統之後。您可能想要退房PHP's GD library

1

這當然是可能的,但你是想可能做的不是一個好主意 - 這裏的原因。

如果你裁剪一張圖片並保存,你(或者你的服務器)再也不需要這樣做了。這不是一個輕鬆的操作。

但是,如果你不停地修剪,你的服務器將不得不每天都這樣做 - 這是非常低效的。

作爲最糟糕的情況,爲什麼不自動裁剪一次到所需的尺寸(而不是手動進行)並簡​​單地保存這些結果?

最後,鑑於它是一家商店,不會手動裁剪它們,從而爲您的產品提供最佳圖像 - 從而獲得最佳銷售機會?

2

你可能會想試試這個簡單的腳本:https://github.com/wes/phpimageresize

它還允許緩存,這應該與性能問題提供幫助。

但我更喜歡調整圖像大小並將它們保存爲縮略圖。