Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Image resize

I have written a function, and after a lot of work got it running though with no problems, however it returns a binary stream for the new resized image, and I have no idea how to write this stream to disc, any ideas?

The function is:-
function resize_jpg($inputFilename, $new_side)
      {      
      $imagedata = getimagesize($inputFilename);      
      $w = $imagedata[0];      
      $h = $imagedata[1];            
      if ($h > $w)
            {            
            $new_w = ($new_side / $h) * $w;            
            $new_h = $new_side;            
            }
      else
            {            
            $new_h = ($new_side / $w) * $h;            
            $new_w = $new_side;      
            }      
      $new_h = 50;
      $new_w = 50;      
      $im2 = ImageCreateTrueColor($new_w, $new_h);      
      $image = ImageCreateFromJpeg($inputFilename);      
      imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]);      
      return $im2;
      }
0
tonelm54
Asked:
tonelm54
1 Solution
 
tonelm54Author Commented:
Sorry, posted my debug version

should be:-
function resize_jpg($inputFilename, $new_side)
     {    
     $imagedata = getimagesize($inputFilename);    
     $w = $imagedata[0];    
     $h = $imagedata[1];          
     $new_h = 50;
     $new_w = 50;    
     $im2 = ImageCreateTrueColor($new_w, $new_h);    
     $image = ImageCreateFromJpeg($inputFilename);    
     imagecopyResampled ($im2, $image, 0, 0, 0, 0, $new_w, $new_h, $imagedata[0], $imagedata[1]);    
     return $im2;
     }
0
 
lukeinjaxCommented:
i have this script that takes all of the pictures in a directory and creates thumbnails in a "thumbnails" sub folder. if the thumbnail is already there, it does nothing. not sure exactly what you are attempting to do with your function, but something like this may work for you...

<?
      function generate_thumbnails($images_dir) {
            $imagemagick_path = "/sw/bin";
            $max_thumbnail_width = 150;
            $max_thumbnail_height = 150;
            $sleep_time = 1000;
            $count = 0;
            
            $thumbs_dir = $images_dir . "thumbnails";
            
            if ($thumbs_handle = opendir("$thumbs_dir/")) {
                  while (false !== ($thumb_file = readdir($thumbs_handle))) {
                        $thumbstring.=$thumb_file;
                  }
            
                  closedir($thumbs_handle);
            }
                  
            if ($handle = opendir("$images_dir")) {
                  while (false !== ($file = readdir($handle))) {
                        if (stristr($file,'jpg') && !stristr($file,'tb_')) {
                              if (!stristr($thumbstring,$file)) {
                                    //*****************image resize code starts here*****************                                   
                                    usleep($sleep_time);
                                    $size = getimagesize("$images_dir$file");
                                                                        
                                    // Wide Image
                                    if($size[0] > $size[1]) {  
                                          $thumbnail_width = $max_thumbnail_width;
                                          $thumbnail_height = (int)($thumbnail_width * $size[1] / $size[0]);                                                        
                                    }  
                                    
                                    // Tall Image
                                    else {
                                          $thumbnail_height = $max_thumbnail_height;
                                          $thumbnail_width = (int)($thumbnail_height * $size[0] / $size[1]);
                                    }
                                    
                                    exec("$imagemagick_path/convert -geometry " .
                                     "{$thumbnail_width}x{$thumbnail_height} " .
                                     "$images_dir/$file $thumbs_dir/tb_$file");
                        
                                    //*****************image resize code ends here*****************
                              }      

                              $pictures[] = $file;
                              $count++;
                        }
                  }
            
                  closedir($handle);
            }

            sort($pictures);
            reset($pictures);
      }
?>
0
 
tonelm54Author Commented:
That would do the job, although I cannot use it, as exec has been disabled on the server Im trying to use it on.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
cristi_2112Commented:
I have a better script for creating thumbnails. Here you go :
/*
* $sourcefile = path/name to source image
* $targetfile = path/name to target image
* $extf = extension of image (you can take it with explode($sourcefile, '.'); and then the last member of the array
* $max_width $max_height = properties of the new image (in case they already are smaller, the thumbnail is just a copy of the original image
* $image_quality = between 1 and 100
*/
function createThumb($sourcefile, $targetfile, $extf, $max_width, $max_height, $image_quality = 100)
{
      eval('$src_img = ImageCreateFrom'.$extf.'($sourcefile);');
      $orig_x = ImageSX($src_img);
      $orig_y = ImageSY($src_img);

      $new_y = $max_height;
      $new_x = $orig_x/($orig_y/$max_height);

      if ($max_width >= $orig_x )
      {
            $new_y = $orig_y;
            $new_x = $orig_x;
      }
      elseif ($new_x > $max_width)
      {
            $new_x = $max_width;
            $new_y = $orig_y/($orig_x/$max_width);
      }

      $dst_img = ImageCreateTrueColor($new_x,$new_y);

      ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $new_x, $new_y, $orig_x, $orig_y);

      eval('Image'.$extf.'($dst_img, $targetfile, $image_quality);');
      ImageDestroy($src_img);
      ImageDestroy($dst_img);
}
0
 
tansofunCommented:
http://us2.php.net/manual/en/function.fwrite.php

fwrite() writes the contents of string to the file stream pointed to by handle.
?
0
 
cristi_2112Commented:
yes. that's correct.
0
 
dimakhCommented:
hi,

here is a tested solution for you to get image resized and saved at your server.
here i've written a function which uses GD Libraries for image manipulation. the code works on GD version 1.6 as well as 2.0
//variable definations as:
//$orig_file: absolute path of Orginal File uploaded,
//$dst_file: absolute path for the resized thumbnail to be saved,
//$new_w: width for thumbnail image,
//$new_h: height for thumbnail image

function createjpegthumb($orig_file,$dst_file,$new_w,$new_h){
    global $gd2;
     $src_img=imagecreatefromjpeg($name);

     $old_x=imagesx($src_img);
     $old_y=imagesy($src_img);
     
     $maxAspect = $new_w / $new_h;
     $origAspect = $old_x / $old_y;

     if ($origAspect <= $maxAspect) {
          $thumb_w = $new_h * $origAspect;
          $thumb_h = $new_h;  
     } else {
          $thumb_w = $new_w;
          $thumb_h = $new_w / $origAspect;  
     }  
     
        $dst_img=imagecreatetruecolor($thumb_w, $thumb_h);

        imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
        imagejpeg($dst_img,$filename);

     if($this->gdversion=="20"){
        $om = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($om, $im, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);        
        }
        if($this->gdversion=="16"){
        $om = imagecreate($newWidth, $newHeight);
        imagecopyresized($om, $im, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
        }
       
        return($om);

}

Hope the above solution works for you. This answer is already accepted for a simillar question.
visit http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21897764.html


If any queries, please write me back.

Thanks and Regards,
dimakh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now