2009-07-27 84 views
1

我已經寫了一個小函數來獲取一個url,並調整圖像的大小並將其存儲在我的本地,但腳本需要大約.85秒才能運行創建文件夾,並在調整大小.64秒。我目前支持JPEG和PNG,如下所示。PHP圖像調整大小/重新定位 - 加快速度

我想知道是否有更快的方法或我正在做的事情花了很長時間,因爲目前我對於我來說是不可接受的,我真的很想讓它更快執行。

任何想法/想法,非常感謝。

謝謝!

function getTime() { 
     $timer = explode(' ', microtime()); 
     $timer = $timer[1] + $timer[0]; 
     return $timer; 
    } 

    function createThumb($thumb, $ids){ 
    $start = getTime(); 

    // File and new size 
    $filename = $thumb; 

    // Get new dimensions 
    $img1 = getimagesize($filename); 

    if ($img1[0] > $img1[1]) { 
     $percentage = ('72'/$img1[0]); 
    } else { 
     $percentage = ('72'/$img1[1]); 
    } 
    $new_width = $img1[0] * $percentage; 
    $new_height = $img1[1] * $percentage; 

    // Resample 
    $image_p = imagecreatetruecolor($new_width, $new_height); 
    if($img1['mime']=='image/png'){ 
     $image = imagecreatefrompng($filename); 
     imagealphablending($image_p, false); 
     imagesavealpha($image_p,true); 
     $transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127); 
     imagefilledrectangle($image_p, 0, 0, $new_width, $new_height, $transparent); 
     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $img1[0], $img1[1]); 
    } 
    else { 
     $image = imagecreatefromjpeg($filename); 
    } 
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $img1[0], $img1[1]); 



    $imgPath = '/foo/bar/location/'.$ids; 
    $imgName =''; 
    //category, product, support 
    if(!is_dir($imgPath)) { 
     mkdir($imgPath, 0777); 
     chmod($imgPath, 0777); 
    } 

    if(!is_file($imgPath."/index.html")){ 
      $ourFileName = $imgPath."/index.html"; 
      $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file"); 
      fwrite($ourFileHandle,'<html><body>401</body></html>'); 
      fclose($ourFileHandle);  
    } 

    // Output 
    if($img1['mime']=='image/png'){ 
     $name = rand(1, 156406571337); 
     $imgName = date("y_m_d_h_m_s").$name.'.png'; 
     imagepng($image_p, $imgPath.'/'.$imgName); 

    } else { 
     $name = rand(1, 156406571337); 
     $imgName = date("y_m_d_h_m_s").$name.'.jpg'; 
     imagejpeg($image_p, $imgPath.'/'.$imgName, 100); 
    } 
    $end = getTime(); 
    echo '<strong>createImage</strong>: '.round($end - $start,4).' seconden<br />'; 
    exit; 
    return $imgName; 

    } 
+1

你真的需要這樣做完全PHP嗎?或者你可以在命令行中使用(例如)imagemagick嗎? 作爲一個旁註:如果使用microtime(true),那麼在沒有參數的情況下,您將不必分解/添加由它返回的兩個部分:-) – 2009-07-27 18:41:16

回答

7

弗雷德里科,是廣管局圖書館只是普通緩慢。 : - \我建議使用PHP ImageMagick庫。語法超級braindead簡單:

$image = new Imagick('image.jpg'); 
$image->thumbnailImage(100,0); // 100px wide, 0 = preserve aspect ratio 

我希望這是一個選項。

+0

給這個旋轉,從未使用過。 會讓你知道。謝謝! – Petrogad 2009-07-27 19:07:40

1

最終,圖像處理是CPU和時間密集型操作。通過任何方式,0.64秒不會過分大小的圖像。如上所示,ImageMagick可能會更快一些,但是它仍然需要比輸出一堆文本更長的時間。

1

請注意,所提到的Imagick-class作爲hobodave在5.1.3之前的PHP安裝中不可用,並且至少需要ImageMagick 6.2.4。

如果您需要您的應用程序向後兼容,則應考慮通過命令行執行ImageMagick。

要運行命令行可執行文件,您可以使用backticks operator