views:

229

answers:

3

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.

Thank you!

  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;

  }
+7  A: 

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

I hope this is an option for you.

hobodave
Giving this a whirl, never used it before.Will let you know. Thanks!
Frederico
I agree. Imagick is substantially faster. Produces higher quality, smaller files too.
jason
Also, note that it's often not available on lots of hosts... it's something I always have to install myself.
jason
Why am I getting phantom downvotes 9 months later?
hobodave
+1  A: 

Ultimately, image manipulation is a CPU- and time-intensive action. 0.64 seconds isn't outrageous on a decently sized image by any means. ImageMagick, as suggested above, is likely to be a bit faster, but it's still going to take a lot longer than outputting a bunch of text.

ceejayoz
+1  A: 

Please note that the Imagick-class as hobodave mentioned isn't available in PHP installations before 5.1.3 and requires at least ImageMagick 6.2.4.

If you need your application to be backwards-compatible you should consider executing ImageMagick via the command line.

To run command line executables you could use the backticks operator.

André Hoffmann