代码之家  ›  专栏  ›  技术社区  ›  Petrogad

PHP图像调整大小/重新定位-加快速度

  •  1
  • Petrogad  · 技术社区  · 16 年前

    我编写了一个小函数来获取一个URL,调整图像的大小并将其存储在本地,但是当需要创建文件夹时,脚本运行大约需要.85秒,而调整大小则需要.64秒。我目前支持jpeg和png,如下所示。

    我想知道是否有一个更快的方法或者我正在做的事情需要花费很长时间,因为我现在的时间对我来说是无法接受的,我真的想让这个更快地执行。

    非常感谢您的任何想法/想法。

    谢谢您!

      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;
    
      }
    
    3 回复  |  直到 12 年前
        1
  •  7
  •   hobodave    16 年前

    弗雷德里克,是的,GD图书馆的速度很慢。:-\n我建议使用php imagemagick库。语法非常简单:

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

    我希望这是你的选择。

        2
  •  1
  •   ceejayoz    16 年前

    最终,图像操作是一个CPU和时间密集型操作。0.64秒对一张尺寸合适的图片来说,无论如何都不过分。如上所述,ImageMagick可能会快一点,但它仍然需要比输出一堆文本更长的时间。

        3
  •  1
  •   André Hoffmann    16 年前

    请注意,hobodave提到的imagick类在5.1.3之前的PHP安装中不可用,并且至少需要imagemagick 6.2.4。

    如果需要应用程序向后兼容,则应考虑通过命令行执行imagemagick。

    要运行命令行可执行文件,可以使用 backticks operator .

    推荐文章