PHP图像调整大小/重新定位-加快速度 [英] PHP Image Resize / Relocate - Speeding it up

查看:98
本文介绍了PHP图像调整大小/重新定位-加快速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个小函数来获取url,并调整图像的大小并将其存储在我的本地文件中,但是该脚本在需要创建文件夹时需要花费约0.85秒的时间来运行,而在创建文件夹时需要花费.64秒钟的时间.调整大小.我目前支持JPEG和PNG,如下所示.

I've written a little function to take a url, and resize the image and store it on my local, however the script is taking about .85 seconds to run when it needs to create the folder, and .64 seconds on a resize. I currently have JPEG and PNG supported as seen below.

我想知道是否有一种更快的方法,或者我正在做的事情花费的时间太长,因为当前的时间对我来说是我无法接受的,所以我真的想让它更快地执行.

I'm wondering if there is a quicker method or something I'm doing that is taking to long, as the current times i have are unacceptable for me, I would really like to get this to execute faster.

任何想法/想法都会受到赞赏.

Any thoughts / ideas are greatly appreciated.

谢谢!

  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;

  }

推荐答案

Frederico,是的,GD库运行缓慢. :-\我建议使用PHP ImageMagick库.语法非常简单:

Frederico, yea the GD library is just plain slow. :-\ I'd suggest using the PHP ImageMagick library. The syntax is super braindead simple:

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

我希望这是您的选择.

这篇关于PHP图像调整大小/重新定位-加快速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆