#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,246
    Rep Power
    220

    About resizing image to smaller of the 2 (widht or height)


    Can you please help me with this script?

    These are my resize criteria.

    1 - Width smaller than 1063, height smaller than 650. Don’t resize
    2 - One dimension bigger (width over 1063 OR height over 650). Don’t resize.
    3 - Both bigger (width over 1063 AND height over 650), resize to the smaller of the 2.

    My main concern is number 3 where if($height_diff<$width_diff)

    PHP Code:
    $config2['source_image'] = $this->upload->upload_path $this->upload->file_name
    $size getimagesize($config2['source_image']); 

    if(
    $size[0] < 1063 && $size[1] < 650)
        {

        }
    elseif( (
    $size[0] < 1063 && $size[1] > 650) || ($size[0] > 1063 && $size[1] < 650))    
        {

        }
    elseif(
    $size[0] > 1063 && $size[1] > 650)  //3 - Both bigger (width over 1063 AND height over 650), resize to the smaller of the 2.
        
    {
            
    $width_diff=(($size[0]*100)/1063);
            
    $height_diff=(($size[1]*100)/650);
            


            
    /*Here is the tricky part.
                I thought I do

            if($height_diff<$width_diff)
                {  
                     $config2['height'] = 650;
                }
            else 
                {
                    $config2['width'] = 1063;
                }

            but it doesn't work. It messes the ratio          

            Then I tried this and it seems to be ok but I am not sure

            if($height_diff<$width_diff)
                {  
                     $config2['height'] = 650;
                     $config2['width'] = 650;
                }
            else 
                {
                    $config2['width'] = 1063;
                    $config2['height'] = 1063;
                } 
            */  
               
        

            
    $config2['new_image'] = $this->upload->upload_path $this->upload->file_name
    $config2['image_library'] = 'gd2'
    $config2['quality'] = "100%"
    $config2['maintain_ratio'] = TRUE
    What would be the right way to do this?

    Thanks
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,437
    Rep Power
    9645
    Pick the right ratio so that one dimension fits the constraint and the other dimension is too large, then calculate both of the new dimensions according to the ratio.
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,246
    Rep Power
    220
    Hello. Please walk me through this Requnix. I thikn I got it.

    Let's say both dimentions are bigger 1700x670 (width over 1063 AND height over 650).

    If I want to resize to the smaller of the 2 (percentage of difference from the target dimension), first thing I do is to find if it's width or height:

    PHP Code:
    $width_diff=(($size[0]*100)/1063);
    $height_diff=(($size[1]*100)/650); 
    Now as we can see height is the smaller of the 2, so:

    PHP Code:
    if($height_diff<$width_diff
    1 - I pick the default height we want which is 650.
    2 - Calculate the ratio of the original x = (670*100)/1700 which is 39%
    3 - Calculate width by calculating 62% ratio (650*100)/39 = 1666 px
    How am I doing?
    Last edited by English Breakfast Tea; October 26th, 2017 at 01:57 AM.
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,437
    Rep Power
    9645
    Keep going and you'll find out whether you have it right or not. Really.
  8. #5
  9. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,246
    Rep Power
    220
    Ummmm
    PHP Code:
    elseif($size[0] > 1063 && $size[1] > 650)
        {
            
    $width_diff=(($size[0]*100)/1063);
            
            
    $height_diff=(($size[1]*100)/650);
            
            if(
    $height_diff<$width_diff)
                {  
                    
    $config2[‘height’] = 650
                    
    $width_height_ratio = ($size[1]*100)/$size[0];
                    
    $config2[‘width’] = round((650*100)/$width_height_ratio);
                }
            else 
                {
                    
    $config2[‘width’] = 1063;
                    
    $width_height_ratio = ($size[1]*100)/$size[0];
                    
    $config2[‘height’] = round((1063*$width_height_ratio)/100);
                }    
        } 
  10. #6
  11. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,112
    Rep Power
    4103
    Coincidentally, I just coded something like this last weekend. Needed to generate thumbnails that were close to but no smaller than 100x100 but preserved aspect ratio so I picked whichever side was closest to 100 and kept that then adjusted the other.

    PHP Code:
            if ($currentWidth $currentHeight){
                
    $newWidth $targetWidth;
                
    $newHeight = ($targetWidth $currentWidth) * $currentHeight;
            } else {
                
    $newWidth = ($targetHeight $currentHeight) * $currentWidth;
                
    $newHeight $targetHeight;
            }

            return [
    floor($newWidth), floor($newHeight)]; 
    So if the width is the smaller dimension, use that and calculate the height based on the percentage change. Otherwise, do the opposite.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo