SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    image resizing - maintaining one image size

    Hi guys,

    I have the following code which is a mixture of snippets found in this article:
    http://www.sitepoint.com/article/image-resizing-php

    and this thread:
    http://www.sitepoint.com/forums/showthread.php?t=477873

    PHP Code:
                if ($w $h)
                {
                    
    $percentage = (100 $w);
                }
                else
                {
                    
    $percentage = (100 $h);
                }

                
    $nw round($w $percentage);
                
    $nh round($h $percentage);

                
    $nimg imagecreatetruecolor($nw$nh);
                
    $simg imagecreatefromjpeg($tmp);
                
    imagecopyresampled($nimg$simg0000$nw$nh$w$h);
                
    $files scandir($path);
                
    $next sprintf("%'03s"count($files));
                
    $thumb=$path.$new_file_name."_sm.jpg";
                
    imagejpeg($nimg$thumb50); 
    It works alongside an upload script, which is used to upload images of different sizes. At the moment, the code simply resizes the images to a size relative to their original dimensions. What I am ideally wanting is to have the images resized to a maximum width and height. eg: if the image is a portrait shot, then the max width will be, say, 200px once resized and then if it is a landscape shot, then its max width will be 400px.

    Does this make sense? Any help much appreciated ...

    D

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    if($w>$h){
      $nw=$maxw;
      $nh=($w/$maxw)*$h;
    }else{
      $nh=$maxh;
      $nw=($h/$maxh)*$w;
    }
    Saul

  3. #3
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks very much, wil give it a run ...

    D

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Daemon'

    Looks like that will work if your submitted pictures are always larger than the resized photo's, but what happens if the user submits a picture ( width=40 & height = 20 ) . The code you submitted will convert the width to 400 and make the height (40/400 = 0.1 * 20 = 2) the picture is now 400 by 2. Perhaps you should get an aspect ratio and multiply both the width and height times that aspect ratio.

    PHP Code:
    if ( $w>$h ) {
      If ( 
    $w $maxw ) {
        
    $nw=$maxw;
        
    $nh=($w/$maxw)*$h;
      } else {
        
    $AspectRatio $maxw $w;
        
    $nw $w $AspectRatio;
        
    $nh $h *AspectRatio;
    }else{
      If ( 
    $h $maxh ) {
        
    $nh=$maxh;
        
    $nw=($h/$maxh)*$w;
      } else {
        
    $AspectRatio $maxh $h;
        
    $nh $h $AspectRatio;
        
    $nw $w $AspectRatio;

    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    SitePoint Guru hisham777's Avatar
    Join Date
    Dec 2005
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if i understood you right this is waht you need
    if you are using GD library
    Code:
    // Get the image size you want to resize
    $size = GetImageSize( $filePath );
    
    
    if($size[0] > $size[1])
    {
    $thumbnail_width = 100;
    $thumbnail_height = (int)(100 * $size[1] / $size[0]);
    }
     else
    {
     $thumbnail_width = (int)(100 * $size[0] / $size[1]);
    $thumbnail_height = 100;
    }
    Do this before resizing
    i hope it helps

    By the way your code works with JPEG only
    Never be shy to ask silly Qs
    An answer is always better than none

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably you should try Asido: it supports various environments, so you will be able to resize your images not only with GD but with ImageMagick via shell, or Imagick/Magick_Wand php extension, etc.; you can also do various types of resizing:


    Here's a demo:

    PHP Code:
    <?php
     
    /**
    * Set the path to the Asido library
    */
    include('./../../asido/dev/class.asido.php');
     
    /**
    * Use the GD driver
    */
    asido::driver('gd');
     
    /**
    * Create an Asido_Image object
    */
    $i1 asido::image(
    'the-source-image.jpg',
    'filename-with-which-you-want-to-save-the-result.png'
    );
     
    /**
    * Watermark it
    */
    asido::watermark($i1'put-the-watermark-image-here.png');
     
    /**
    * Resize it proportionally to make it fit inside a 200x200 frame: in
    * this way you can be sure that ALWAYS either the width or the height
    * will be 200px. 
    */
    asido::resize($i1200200ASIDO_RESIZE_PROPORTIONAL);
     
    /**
    * Save it and overwrite the file if it exists
    */
    $i1->save(ASIDO_OVERWRITE_ENABLED);
     
    ?>
    Now, it seems what you need is "fit" resize: this is a proportional resize, where the image is `forced` to fit inside a `frame` set by the dimensions you provide as arguments; if the image is bigger than this `frame`, it will be resized(shrinked) to fit, however, if it is smaller than this `frame`, no resizing will be performed.

    If you have more questions regarding Asido, do not hasitate to ask

  7. #7
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guys, I have tried all the various code options given, thanks very much (bar the Asido option), but none of the resized images maintain their aspect ratio, they either look to squashed or too stretched ...

    I really appreciate all the help so far ...

    D

  8. #8
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Code php:
    if($w>$h){
      $nw=$maxw;
      $nh=($w/$maxw)*$h;
    }else{
      $nh=$maxh;
      $nw=($h/$maxh)*$w;
    }
    Actually I made a mistake there, it should be:

    Code php:
    if($w>$h){
      $nw=$maxw;
      $nh=($maxw/$w)*$h;
    }else{
      $nh=$maxh;
      $nw=($maxh/$h)*$w;
    }
    Saul

  9. #9
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ( $w>$h ) {
      If ( 
    $w $maxw ) { // We need to shrink this?
        
    $AspectRatio $w/$maxw//  Get factor by which to shrink
        
    $nw=$maxw// Change width to max width
        
    $nh=$h/$AspectRatio;  // Shrink height by same amount
      
    } else { // We need to enlarge 
        
    $AspectRatio $maxw $w// Get factor by which to enlarge
        
    $nw $w $AspectRatio//  Change height to max height
        
    $nh $h *AspectRatio//  Enlarge width by same amount
      
    }
    }else{
      If ( 
    $h $maxh ) {
        
    $AspectRatio $h/$maxh;
        
    $nh=$maxh;
        
    $nw=$w/$AspectRatio;
      } else {
        
    $AspectRatio $maxh $h;
        
    $nh $h $AspectRatio;
        
    $nw $w $AspectRatio;
      }

    Did not fully think through the code I modified the first time try this snippet.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  10. #10
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hm, Byron, I think my code above does the same with less typing.
    Saul

  11. #11
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would have if you had divided instead of multiplying, run some test numbers...
    My code can actually be made shorter...
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  12. #12
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    OK, let's see...

    Let's assume that the max size is 100x100 and:

    a) we have an image of 20x10:
    Code:
    20>10
    $nw=100
    $nh=(100/20)*10=50
    b) we have an image of 200x100:
    Code:
    200>100
    $nw=100
    $nh=(100/200)*100=50
    Likewise if the height is larger than width.

    I don't see nothing wrong here.
    Saul

  13. #13
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the responses guys.
    I will try it all again later at home ...

    D

  14. #14
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Daemon,

    Thank you very much, your revised script did the trick perfectly ...
    CHeers,
    D


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
  •