Uploading an image and saving multiple sizes to file system using PHP, Laravel, GD -


i'm trying allow users upload image , save image 'original copy' in directory

public_html/data/user_images/yyyy/mm/dd/guid.ext  

next, using gd create multiple copies of image @ different sizes , save dynamic location in file system.

public_html/data/user_images/width/yyyy/mm/dd/guid.ext   

i keep coming across following error leads me believe file needs exist before can save on manipulated image using gd library.

imagejpeg(/data/user_images/200/2013/05/21/9714624e10eed645e822babd0acccf69ac421d59.png):   failed open stream: no such file or directory 

i same error both relative , absolute paths. know directory or file doesn't exist. code works expected when saving on original copy of uploaded image, can't create new 1 in dynamic directory.

below code image helper. parameters takes original saved image location, array of sizes , file name new images. createdirectories function returns array containing absolute , relative dynamically created paths.

public static function create_sizes($image, $sizes, $filename){      $current_size = getimagesize($image);     $urls = array();      switch(strtolower($current_size['mime'])){         case 'image/png':             $image_original = imagecreatefrompng($image);             break;         case 'image/jpeg':             $image_original = imagecreatefromjpeg($image);             break;         case 'image/gif':             $image_original = imagecreatefromgif($image);             break;         default: die();     }        foreach($sizes $width){          $directories = self::createdirectories('user_images/'.$width);          $aspect_ratio = $current_size[0] / $current_size[1];         $height = (int) ($width / $aspect_ratio);          $photox = imagesx($image_original);         $photoy = imagesy($image_original);          $image_new = imagecreatetruecolor($width, $height);          imagecopyresampled($image_new, $image_original, 0, 0, 0, 0, $width+1, $height+1, $photox, $photoy);          imagejpeg($image_new, $directories['absolute'].$filename, 100);         array_push($urls, $directories['relative'].$filename);     }      imagedestroy($image_original);     imagedestroy($image_new);      return $urls; } 

have quick on imageprocessor class. think you: http://pastebin.com/zny02n57

usage: when user uploads file it's name in $_files['input_name']['tmp_name']. first of create empty instance of class:

$originalimage = new imageprocessor(); //note automatically detect file type :) if(!$originalimage->load($_files['input_name']['tmp_name']))     die('image invalid'); 

now have choose method of resizing. can force image have fixed width or height or have exact size choose, on black background if not fit.

fixed width: $newimage = $originalimage->resizewpreserve($newwidth) fixed height: $newimage = $originalimage->resizehpreserve($newheight) fixed size: $newimage = $originalimage->resizecanvas($newwidth, $newheight)  then, save it, simply: $newimage->save('filename.jpg'); 

you can repeat save , resize in loop like:

$sizes = array(640, 800, 1024); foreach($sizes $width) {     $originalimage->resizewpreserve($width)->save(sprintf('images/%s/%d.jpg', 'somename', $width)); } 

class


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -