Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Image resize

Posted on 2006-07-20
7
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

715 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