SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image resize class

    Frankly I'm too lazy to make my own so can anyone recommend a good php class to resize images? I only need to resize JPG the only important thing is

    say my picture is 700x600. I'd like to specify a max width/height of 100x100, and it will resize to 100x86 rather than transforming it to 100x100.

    Thanks!
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here's a little function offhand:
    PHP Code:
    function resize_image($src_file$dest_file $wd$hg){
            list(
    $width$height$type$attr) = getimagesize($src_file);
            
    $img=imagecreatefromjpeg($src_file);        
            if(
    $width>$wd&&$width>$height){
               
    $hg=$height*($wd/$width);
            elseif(
    $height>$hg){
               
    $wd=$width*($hg/$height);
            } else {
               return 
    false;
            }
            
    $thumb=imagecreatetruecolor($wd,$hg);
            
    imagecopyresampled($thumb$img0000$wd$hg$width$height);
            
    imagejpeg($thumb,$dest_file);
            return 
    true;

    Saul

  3. #3
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm looking into imagemagick now...

    I couldn't really find any relevant info on google, so...

    which is faster, based purely on image resizing? ImageMagick or GD?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Evaluate these functions:
    PHP Code:
    $ArrImageType = array("image/jpeg""image/jpg""image/png""image/gif");
        function 
    Create_ThumbNail($ImageType$ImagePath$rW$rH$img$do="thumbnail"){
            
    $full_picmain $ImagePath "/" $img;
            if(
    $do != "thumbnail")
                
    $tsrc "$ImagePath/$img";
            else
                
    $tsrc "$ImagePath/thumbnails/$img";
            
    ////////////// Starting of GIF thumb nail creation///////////
            
    if($ImageType == "image/gif"){
                
    $im imagecreatefromgif($full_picmain);
                
    $width imagesx($im);                // Original picture width is stored
                
    $height imagesy($im);                // Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
    $newimage imagecreatetruecolor($n_width,$n_height);
                
    imagecopyresized($newimage$im0000$n_width$n_height$width,$height);
                if(
    function_exists("imagegif")) {
                    
    header("Content-type: image/gif");
                    
    imagegif($newimage$tsrc);
                }
                elseif(
    function_exists("imagejpeg")) {
                    
    header("Content-type: image/jpeg");
                    
    imagejpeg($newimage,$tsrc);
                }
                
    chmod("$tsrc",0777);
            }
            
    ////// starting of JPG thumb nail creation//////////
            
    if($ImageType == "image/pjpeg" || $ImageType == "image/jpeg" || $ImageType == "image/jpg"){
                
    $im imagecreatefromjpeg($full_picmain);
                
    $width imagesx($im);              // Original picture width is stored
                
    $height imagesy($im);             // Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
    $newimage imagecreatetruecolor($n_width,$n_height);                 
                
    imagecopyresized($newimage$im0000$n_width$n_height$width,$height);
                
    imagejpeg($newimage$tsrc);
                
    chmod("$tsrc",0777);
            }
            
    ////// starting of JPG thumb nail creation//////////
            
    if($ImageType == "image/png"){
                
    $im imagecreatefrompng($full_picmain); 
                
    $width imagesx($im);              // Original picture width is stored
                
    $height imagesy($im);             // Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
    $newimage imagecreatetruecolor($n_width,$n_height);                 
                
    imagecopyresized($newimage$im0000$n_width$n_height$width,$height);
                
    imagepng($newimage$tsrc);
                
    chmod("$tsrc",0777);
            }        
        }
        function 
    GetImageExtention($type){
            if(
    $type == "image/jpeg" || $type == "image/jpg" || $type == "image/pjpeg")
                return 
    "jpg";
            elseif(
    $type "image/png")
                return 
    "png";
            elseif(
    $type == "image/gif")
                return 
    "gif";
        } 
    And see the example here:
    PHP Code:
            if(is_uploaded_file($_FILES['picFile']['tmp_name'])){
                if(
    $_FILES['picFile']['size'] >= 1048576){//not greater then 1 Mb
                    
    $_SESSION['PicError'] = "Picture size exceeded MB.";
                    
    header("Location:./addpictures.php?PID=$PID&mode=Edit");
                    exit();
                }
                elseif(!
    in_array($_FILES['picFile']['type'], $ArrImageType)){
                    
    $_SESSION['PicError'] = "Picture format is invalid.";
                    
    header("Location:./addpictures.php?PID=$PID&mode=Edit");
                    exit();
                }
                else{
                    
    $imgExt GetImageExtention($_FILES['picFile']['type']);
                    
    $PicName "prjpic_" $PID "." $imgExt;
                    
    $PicFullPathName $picpath "/" $PicName;
                    if(
    move_uploaded_file($_FILES['picFile']['tmp_name'], $PicFullPathName)){
                        
    Create_ThumbNail($_FILES['picFile'], $picpath9360$PicName"thumbnail");
                        list(
    $width,$height) = getimagesize($PicFullPathName);
                        if(
    $width 640){
                            
    $wd $width 640$nh $height $wd;
                            
    Create_ThumbNail($_FILES['picFile'], $picpath640$nh$PicName"resize");
                        }
                        
    header("Location:./picturemgmt.php");
                        exit();
                    }
                    else{
                        
    $_SESSION['PicError'] = "Picture was not uploaded. Please try again.";
                        
    header("Location:./addpictures.php?PID=$PID&mode=Edit");
                        exit();
                    }
                }
            } 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,392
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I have some imagemagick examples on my site triexa - link in my signature.

    There is also a site with loads of Imagemagick examples http://www.imagemagick.org/Usage/

    The code would be a lot shorter but I do not know about the speed. I am rebuilding that area of my site and I may try a couple of images and see what the difference is.

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,392
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Thats strange; the code for GD in the example I tried is:
    PHP Code:
    $canvas imagecreate200100 );

    $black imagecolorallocate$canvas00);
    $white imagecolorallocate$canvas255255255 );

    imagefilledrectangle$canvas9918989$white );

    $font "verdana.ttf";
    $text "Title";
    $size "30";

    $box imagettfbbox$size0$font$text );
    $x = (200 - ($box[2] - $box[0])) / 2;
    $y = (100 - ($box[1] - $box[7])) / 2;
    $y -= $box[7];

    imageTTFText$canvas$size0$x$y$black$font$text );

    imagejpeg$canvas"Label_GD.jpg" );

    ImageDestroy$canvas ); 
    The code for ImageMagick was:
    PHP Code:
    $text "Title";
    exec("/usr/local/bin/convert -background white -fill black -pointsize 30 -font verdana.ttf -gravity Center -size 180x80 label:$text -mattecolor black -frame 10x10+0+0 Label_IM.jpg"); 
    The run time for the GD code was 0.003138 seconds and the time for ImageMagick was 0.111307 seconds. So GD was 35 times faster ? although the code was 14 lines and the ImageMagick was 2 lines !

  7. #7
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    Thats strange; the code for GD in the example I tried is:
    PHP Code:
    $canvas imagecreate200100 );

    $black imagecolorallocate$canvas00);
    $white imagecolorallocate$canvas255255255 );

    imagefilledrectangle$canvas9918989$white );

    $font "verdana.ttf";
    $text "Title";
    $size "30";

    $box imagettfbbox$size0$font$text );
    $x = (200 - ($box[2] - $box[0])) / 2;
    $y = (100 - ($box[1] - $box[7])) / 2;
    $y -= $box[7];

    imageTTFText$canvas$size0$x$y$black$font$text );

    imagejpeg$canvas"Label_GD.jpg" );

    ImageDestroy$canvas ); 
    The code for ImageMagick was:
    PHP Code:
    $text "Title";
    exec("/usr/local/bin/convert -background white -fill black -pointsize 30 -font verdana.ttf -gravity Center -size 180x80 label:$text -mattecolor black -frame 10x10+0+0 Label_IM.jpg"); 
    The run time for the GD code was 0.003138 seconds and the time for ImageMagick was 0.111307 seconds. So GD was 35 times faster ? although the code was 14 lines and the ImageMagick was 2 lines !
    Length of code has nothing to do with efficiency, although at best it could be a hint of lack of efficiency.

    From what I've read so far is seems ImageMagick is best for image manipulation while GD is best for image creation
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,392
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I have also been thinking about this and GD is built into php whereas ImageMagick is an external program called form php. This may explain some of the speed difference.
    Also unless you are processing a lot of images I do not suppose you will ever notice the difference.

    I will try resizing some images later and see what that comes out like.

  9. #9
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,392
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    I have a couple of problems at the moment and so I have just knocked together some code to resize an image.
    GD method
    PHP Code:
    // Temporary upload image name
    $original_image 'flowers.jpg';
    // Get the image dimensions
    $size=GetImageSize$original_image );
    // Maximum image width
    $max_width "100";
    // Maximum image height
    $max_height "100";
    // Resize the image and save
    $src_img ImageCreateFromJPEG$original_image );
    $thumbnail ImageCreateTrueColor$max_width$max_height );
    ImageCopyResampled$thumbnail$src_img0000$max_width$max_height$size[0],$size[1] );
    ImageJPEG$thumbnail'flowers_GD_time.jpg' );
    ImageDestroy$thumbnail ); 
    ImageMagick method
    PHP Code:
    // Temporary upload image name
    $original_image 'flowers.jpg';
    // Get the image dimensions
    $size=GetImageSize$original_image );
    // Maximum image width
    $max_width "100";
    // Maximum image height
    $max_height "100";
    // Resize the image and save
    exec("/usr/local/bin/convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height flowers_IM_time.jpg"); 
    GD did the resize in 0.162674 seconds and Image magick took 0.082352 seconds. So in this case ImageMagick was 50% faster.

  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by triexa View Post
    Frankly I'm too lazy to make my own so can anyone recommend a good php class to resize images? I only need to resize JPG the only important thing is

    say my picture is 700x600. I'd like to specify a max width/height of 100x100, and it will resize to 100x86 rather than transforming it to 100x100.

    Thanks!
    I am not sure if you already solved your image resizing issue, but for you and all other devs like you looking for a way to transform images on the fly, you might want to check the open-source projectAsido: there are a lot of useful features (http://www.asido.info/about/features/), including various types of image resize - like fit resize (resize if bigger) and frame resize (force-fit), watermarking, image copy, cropping, etc. It can use various platforms: not only GD but ImageMagick too (either via shell or via any of the php extensions like php_magickwand.dll or php_imagick.dll). Here's the list of drivers: http://www.asido.info/about/drivers/

    It seems that what you need is the frame resize: http://www.asido.info/about/features/#frame
    This type of resize will attempt to resize your image and fit inside another "proportions frame" nevertheless the image proportions and the "frame" proportions are not the same. The blank areas left can be filled with a custom color (instead of the default white).


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
  •