Solved

Image resize

Posted on 2006-07-20
7
287 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 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

20 Experts available now in Live!

Get 1:1 Help Now