Solved

Image resize

Posted on 2006-07-20
7
288 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

816 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

8 Experts available now in Live!

Get 1:1 Help Now