Solved

Image resize

Posted on 2006-07-20
7
286 Views
Last Modified: 2010-05-18
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
Comment
Question by:tonelm54
7 Comments
 

Author Comment

by:tonelm54
ID: 17151145
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
 

Expert Comment

by:lukeinjax
ID: 17151695
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
 

Author Comment

by:tonelm54
ID: 17152700
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Expert Comment

by:cristi_2112
ID: 17152987
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
 
LVL 2

Expert Comment

by:tansofun
ID: 17155177
http://us2.php.net/manual/en/function.fwrite.php

fwrite() writes the contents of string to the file stream pointed to by handle.
?
0
 
LVL 9

Expert Comment

by:cristi_2112
ID: 17155590
yes. that's correct.
0
 
LVL 1

Accepted Solution

by:
dimakh earned 500 total points
ID: 17159619
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now