Jump to content

Resize function


graham23s

Recommended Posts

Hi There guys

 

finished writing a function to resize uploaded images, it workes pretty well, but earlier i uploaded an image and because i'm resizing in % it was still pretty big, so i was going to set specific widths and heights  (100w & 150h) instead of in %

 

is it better to resize in percentage or set height/widths would you say?

 

code:

 

     function resize_images($filename,$uploaddirectory,$var_loggedinuser) {  
     
        ## Find out the files extension 
        $ext = explode(".", $filename); 
        $ext = $ext[count($ext)-1]; 
        
        ## Make all filenames lowercase
        $ext = strtolower($ext);
          
          if($ext == "jpg" || $ext == "jpeg")     
           
              $image = imagecreatefromjpeg($uploaddirectory); 
               
          elseif($ext == "png") 
           
              $image = imagecreatefrompng($uploaddirectory); 
               
          elseif($ext == "gif") 
           
              $image = imagecreatefromgif($uploaddirectory); 
               
        ## save the file in %     
        $size = 0.50; 
         
        ## rename the thumbnail 
        $newimagename_thumb = $var_loggedinuser. "-" .time(); 
        $save = "thumbs/$newimagename_thumb.$ext"; 
               
        ## get the files dimensions 
        list($width,$height) = getimagesize($uploaddirectory); 
        
        ## New measurements 
        $modwidth = $width * $size;  
        $modheight = $height * $size;  
        
        ## Start making the image
        $thumbnail = imagecreatetruecolor($modwidth, $modheight) ;  
        imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);   
               
        ## Only return the image identified correctly
        if($ext == 'jpg' || $ext == 'jpeg') 
      
        imagejpeg($thumbnail, $save, 100); 
      
        if($ext == 'gif') 
      
        imagegif($thumbnail, $save, 100); 
      
        if($ext == 'png') 
      
        imagepng($thumbnail, $save, 100); 
        
        return($newimagename_thumb. "." .$ext);
        
        }

 

should i re-write this from scratch or is it easily modified to implement set width and height can anyone tell me?

 

thanks guys

 

Graham

 

Link to comment
Share on other sites

It depends on the application.  If you are ever going to display the images in a tiled format, you want them to have the same maximum dimensions, etc.  In that case, sizes are definitely better, but you want to retain the aspect ratio and pad the image later so every image display as, say, 200x200 pixels no matter what.

 

If you want to change over, it would not be difficult to modify what you have. 

 

If the original image was 1677x1100 and you wanted something for 250x250, You would take 250/1677 (the longest side) to get the x factor (.149076).  Then, mutliply that by 1100 to get the length of the other side.  In this case, it is 164.  So, the new image size would be 250x164.  You would do that with every image.  When you display that as a square, you would just bad the top and bottom by 43 pixels on each side.  Then, you have what you need.  You do have to make sure to figure out which side is the longest one before you start, though.  Hopefully that helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.