[英]Most Efficient Way to Create Thumbnails?
我有大量的縮略圖要做。 目前,我正在使用ImageMagick,但它證明效率太低(它太慢,使用太多CPU /內存等)。
我已經開始評估GraphicsMagick,我希望得到“哇”的結果。 我沒有得到它們。 有人可以快速查看我的基准腳本(僅進行簡單的速度和文件大小比較;還沒有CPU和內存檢查):
這是我得到的示例輸出:
'gm convert' took 75.0039 seconds to execute 10 iteration(s).
'convert' took 83.1421 seconds to execute 10 iteration(s).
Average filesize of gm convert: 144,588 bytes.
Average filesize of convert: 81,194 bytes.
GraphicsMagick的速度並不快 - 輸出的文件大小比ImageMagick高得多。
我假設你有一些需要拇指的圖像隊列,你的應用程序通過它們工作? 你可以把一些工作的東西抽到像EC2那樣的東西上。 如果您的隊列超過一定大小,則啟動預先准備好的EC2實例來處理負載。 如果隊列很大,你甚至可以啟動幾台機器。
您不需要這些實例一直運行 - 只有當您自己的服務器無法處理負載時才需要它們。
顯然你需要預測你的成本,看看它是否值得,但鑒於你只需支付你使用的時間,價格從8.5c /小時開始,它可能足夠經濟滿足你的需求。
我想使用GD2,嘗試我使用的這個功能。 它很容易使用:
function scaleImage($source, $max_width, $max_height, $destination) {
list($width, $height) = getimagesize($source);
if ($width > 150 || $height > 150) {
$ratioh = $max_height / $height;
$ratiow = $max_width / $width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$newwidth = intval($ratio * $width);
$newheight = intval($ratio * $height);
$newImage = imagecreatetruecolor($newwidth, $newheight);
$exts = array("gif", "jpg", "jpeg", "png");
$pathInfo = pathinfo($source);
$ext = trim(strtolower($pathInfo["extension"]));
$sourceImage = null;
// Generate source image depending on file type
switch ($ext) {
case "jpg":
case "jpeg":
$sourceImage = imagecreatefromjpeg($source);
break;
case "gif":
$sourceImage = imagecreatefromgif($source);
break;
case "png":
$sourceImage = imagecreatefrompng($source);
break;
}
imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output file depending on type
switch ($ext) {
case "jpg":
case "jpeg":
imagejpeg($newImage, $destination);
break;
case "gif":
imagegif($newImage, $destination);
break;
case "png":
imagepng($newImage, $destination);
break;
}
}
}
我建議你使用ExactImage。 根據基准測試,它比ImageMagick更快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.