SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    imagecopyresized() question

    Hi there,

    I have successfully created a script that uploads images and copies them via gd.

    PHP Code:
        list($width$height) = getimagesize($file_tmp);
               
    //calculate the image ratio
               
    $imgratio=$width/$height;
               if (
    $imgratio>1){
                  
    $newwidth $ThumbWidth;
                  
    $newheight $ThumbWidth/$imgratio;
               }else{
                     
    $newheight $ThumbWidth;
                     
    $newwidth $ThumbWidth*$imgratio;
               }
               
    //function for resize image.
               
    $resized_img imagecreatetruecolor($newwidth,$newheight);
               
    $img_name $rand_name.$file_ext;
               
    //the resizing is going on here!
               
    imagecopyresized($resized_img$new_img0000$newwidth$newheight$width$height); 
    I would like to make the script check to see if the image width is greater than 700px. If it is, I would like to save the image to new dimensions with 700px the max width; otherwise, just save the image as is.

    Can you help me structure my if/else to properly accomplish this?

    My thoughts are that I would say something like this:

    PHP Code:
    if ($newwidth 700) {
    $newwidth 700;
    $newheight = ($newwidth $imgratio)
    } else {
    //continue as planned 
    Any thoughts are greatly appreciated. Thank you.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Set $thumbwidth to 700, and you'll never get an image wider than 700.

    If imagewidth > imageheight;
    Newwidth = thumbwidth = 700
    If imagewidth < imageheight
    Newwidth = thumbwidth * ratio. Ratio (imagewidth/imageheight) must be less than 1, so thumbwidth * ratio must be less than thumbwidth (700);
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be obtuse, but do you mean like this:

    PHP Code:
               if ($imgratio>1){
                  
    $newwidth $ThumbWidth;
                  
    $newheight $ThumbWidth/$imgratio;
                  
    //=================
                
               
    }else{
                     
    $newheight $ThumbWidth;
                     
    $newwidth $ThumbWidth*$imgratio;
               }
               
               if (
    $newwidth $newheight) {
                
    $newwidth $ThumbWidth 700;
               }
               
               if (
    $newwidth $newheight) {
                
    $newwidth $ThumbWidth $imgratio;
               }
               
               
    //function for resize image.
               
    $resized_img imagecreatetruecolor($newwidth,$newheight); 
    If so, at what point do I make the new height for to match the 700 px width?

  4. #4
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I had to do this myself just the other day:
    PHP Code:
    function resizeImage($source$destination$target_width){
        
    $success false;
        
    $dims getimagesize($source);
        
    $width $dims[0];
        
    $height $dims[1];
        
    $ratio $width/$height;
        
        
    $target_height $target_width*$ratio;
        
    $thumb imagecreatetruecolor($target_width$target_height);
        
    $source imagecreatefromjpeg($source);
        
    imagecopyresampled($thumb$source0000$target_width$target_height$width$height);
        
        if(
    imagejpeg($thumb$destination)) $success true;
        
        return 
    $success;
    }

    $max_image_width 700//set image width here
    $dims getimagesize($filename);
    if(
    $dims[1] > $max_image_width){
        if(!
    resizeImage($upload_filename$destination_filename$max_image_width)){
            echo 
    'There was an error copying image';
            exit();
        }

    Any good?

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    No, I mean the script you have already does this - think of it this way. If $ThumbWidth has a value of 700 at the start of your script; what are the possible values of $newwidth at the end?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the responses. I feel very close to a solution. I have figured out how to resize width, but not height. If the image width is greater than 700, I set the width to 700. But I haven't been able to change the height proportionally! The image is stretched as a result. Any ideas?

    PHP Code:
                    if ($width>700) {
                        
    $newwidth 700;
                        
    $newheight $newwidth $imgratio;
                        
                        if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                               
    $new_img imagecreatefromjpeg($file_tmp);
                           }elseif(
    $file_type == "image/pjpg" || $file_type == "image/jpg"){
                               
    $new_img imagecreatefromjpeg($file_tmp);
                           }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){
                               
    $new_img imagecreatefrompng($file_tmp);
                           }elseif(
    $file_type == "image/gif"){
                               
    $new_img imagecreatefromgif($file_tmp);
                           }
                        
                        
                           
    $file_tmp imagecreatetruecolor($newwidth,$newheight);
                        
    imagecopyresized($file_tmp$new_img0000$newwidth$newheight$width$height);
                           
    //finally, save the image
                           
    ImageJpeg ($file_tmp$bigpath);
                           
    ImageDestroy ($file_tmp);
                           
    ImageDestroy ($new_img); 
    Thanks for the advice.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Turn your * into a / and see what happens.
    (nw/nh = w/h)
    (nw/nh = r)
    (nw = r * nh)
    (nw/r = nh)

    OT: Go go Algebra skills...whoever said you'd never use them.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks StarLion.

    My math teacher would definitely be a little disappointed with my algebra. Thanks for helping me understand that a bit better.

    I now have figured out how to resize the image properly and save it to my database, etc. For some reason, however, I have not figured out how to make a thumbnail for the new re-sized 700 px image. I am properly thumbnailing for regular images perfectly, however. If anyone has a moment I could use an extra pair of eyes to help me see my mistake:

    PHP Code:

    list($width$height) = getimagesize($file_tmp);
           
    //calculate the image ratio
           
    $imgratio=$width/$height;
                    
                if (
    $width>700) {
                        
    $newwidth 700;
                        
    $newheight $newwidth $imgratio;
                        
                        if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                               
    $new_img imagecreatefromjpeg($file_tmp);
                           }elseif(
    $file_type == "image/pjpg" || $file_type == "image/jpg"){
                               
    $new_img imagecreatefromjpeg($file_tmp);
                           }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){
                               
    $new_img imagecreatefrompng($file_tmp);
                           }elseif(
    $file_type == "image/gif"){
                               
    $new_img imagecreatefromgif($file_tmp);
                           }
                        
                        
                           
    $file_tmp imagecreatetruecolor($newwidth,$newheight);
                           
    imagecopyresized($file_tmp$new_img0000$newwidth$newheight$wi                                       dth$height);
                           
    //finally, save the image
                           
    ImageJpeg ($file_tmp$bigpath);
                           
    ImageDestroy ($file_tmp);
                            
                                     if (
    $imgratio>1){
                                          
    $new2width $ThumbWidth;
                                          
    $new2height $ThumbWidth/$imgratio;
                                          
    //=================
                                        
                                     
    }else{
                                             
    $new2height $ThumbWidth;
                                             
    $new2width $ThumbWidth*$imgratio;
                                       
                                                  
    //function for resize image.
                                           
    $resized_img imagecreatetruecolor($new2width,$n                                                                    ew2height);
                                           
    //the resizing is going on here!
                                           
    imagecopyresized($resized_img$new_img000,                                                                      0$new2width$new2height$width$height);
                                           
    //finally, save the image
                                           
    ImageJpeg ($resized_img$thumbpath);
                                           
    ImageDestroy ($resized_img);
                                           
    ImageDestroy ($new_img);
                                           
                                     } 
    Thanks everyone for the support in trying to figure out this issue. I really appreciate it.

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    what do you do if $width is not > 700? Cause... you've got a very large dangling if there.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  10. #10
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply StarLion. This is the "else" to that "if":

    PHP Code:
    } else
           if(
    $file_size){
              if(
    $file_type == "image/pjpeg" || $file_type == "image/jpeg"){
                   
    $new_img imagecreatefromjpeg($file_tmp);
               }elseif(
    $file_type == "image/pjpg" || $file_type == "image/jpg"){
                   
    $new_img imagecreatefromjpeg($file_tmp);
               }elseif(
    $file_type == "image/x-png" || $file_type == "image/png"){
                   
    $new_img imagecreatefrompng($file_tmp);
               }elseif(
    $file_type == "image/gif"){
                   
    $new_img imagecreatefromgif($file_tmp);
               }
               
    //list the width and height and keep the height ratio.
               
    list($width$height) = getimagesize($file_tmp);
               
    //calculate the image ratio
               
    $imgratio=$width/$height;
               if (
    $imgratio>1){
                  
    $newwidth $ThumbWidth;
                  
    $newheight $ThumbWidth/$imgratio;
                  
    //=================
                
               
    }else{
                     
    $newheight $ThumbWidth;
                     
    $newwidth $ThumbWidth*$imgratio;
               }
               
    //function for resize image.
               
    $resized_img imagecreatetruecolor($newwidth,$newheight);
               
    //the resizing is going on here!
               
    imagecopyresized($resized_img$new_img0000$newwidth$newheight$width$height);
               
    //finally, save the image
               
    ImageJpeg ($resized_img$thumbpath);
               
    ImageDestroy ($resized_img);
               
    ImageDestroy ($new_img);         
            }
            
    //ok copy the finished file to the thumbnail directory
            
    move_uploaded_file ($file_tmp$bigpath); 
    This is the piece of code that I based the other re-sizing on, also. Everything works as intended except creating the thumbnails as described in my other post.

    Thanks for helping me out with this.

  11. #11
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Take a look at where the code in post 10 has the end of the inner Else. Then look at your code in post 8. Spot the difference?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •