Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Resize Function

  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Location
    Scotland
    Posts
    134
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Resize Function

    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?

    PHP 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$image0000$modwidth$modheight$width$height);   
                   
            
    ## Only return the image identified correctly
            
    if($ext == 'jpg' || $ext == 'jpeg'
          
            
    imagejpeg($thumbnail$save100); 
          
            if(
    $ext == 'gif'
          
            
    imagegif($thumbnail$save100); 
          
            if(
    $ext == 'png'
          
            
    imagepng($thumbnail$save100); 
            
            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

  • #2
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Why don't you just do both?

    Set the maximum height and width and then just check if the percentage of the height/width mentioned is bigger than the maximum height/width and then reduce it relatively.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    It depends on what you're using the images for, and if you want them an exact size or if you just need them less than a certain size. For the former you would just set the size, and for the latter you would resize it proportionately.


  •  

    Posting Permissions

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