SitePoint Sponsor

User Tag List

Results 1 to 25 of 139

Hybrid View

  1. #1
    ********* Articles ArticleBot's Avatar
    Join Date
    Apr 2001
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Article Discussion

    This is an article discussion thread for discussing the SitePoint article, "Simplified Image Resizing with PHP"

  2. #2
    SitePoint Zealot chrisdpucci's Avatar
    Join Date
    Dec 2006
    Location
    On the internets
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is actually a php function that does this exact thing, (create dynamic thumbnails in real-time), although the way it does it is way more efficient. It's called phpthumbs and it works very well.

    http://phpthumb.sourceforge.net/

  3. #3
    SitePoint Evangelist Dave Morton's Avatar
    Join Date
    Sep 2003
    Location
    Carson City, NV
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up I don't know if this will help, but here goes.

    I've seen a lot of "I can't get the script to work", and "But that doesn't actually resize the image!", and I got to thinking about what I've worked on recently using the GD libraries, and I found this very small random image rotation script that I wrote for my personal site. The code is listed below, and it allows for setting the width, height and quality of the image. I'm sure that you can easily modify it to show a specific image with little effort. ^ ^

    PHP Code:
    <?php
          
    //rotate.php - Random Image Rotation Script -
          
    $folder './my_gallery';
          
    $fileArray glob("$folder/*.jpg");
          
    $w 150;       // default width.
          
    $quality 80;  // default quality.
          
    $h = -1;        // just sets the variable, in case the query string doesn't include it.
          
    srand();
          
    $idx rand(0,count($fileArray) - 1);
          
    $fileName $fileArray[$idx];
          
    $time date ("l, F jS, Y g:i:s A");
          foreach (
    $_GET as $key => $value) {
            $
    $key stripslashes($value);
          }
          list(
    $srcWidth$srcHeight) = getimagesize($fileName);
          
    $srcImg ImageCreateFromJPEG($fileName);
          
    $aspectRatio =  $srcWidth $srcHeight;
          
    $h = ($h != -1) ? $h : ($w $aspectRatio); // Makes sure that the image is properly proportioned
          
    $dstImg imagecreatetruecolor($w$h);
          
    imagecopyresampled($dstImg$srcImg0000$w$h$srcWidth$srcHeight); // I've noticed that on larger images, resampling is a bit slower than simple resizing, but this produces a MUCH better image.
          
    header("Content-type: image/jpeg");
          
    imagejpeg($dstImg,"",$quality);
          
    imagedestroy($srcImg);
          
    imagedestroy($dstImg);
    ?>
    /*
      The image is called with the following HTML tag:
      <img src="rotate.php?w=100&q=80">
      where w is the images width, and q is the images JPEG quality.
      
      Note that I left off any width or height quantifiers in the HTML tag. While poor coding practice, it proves the resizing of the image indeed works.
    */
    Below is the final product, in triplicate:
    75 px width, 100% quality
    100 px width, 67% quality
    125 px width, 33% quality

    Hope this helps. ^ ^
    Making a difference, one little psychotic episode at a time
    Geek Cave Creations
    Beta testers needed for pChat
    Dave's Gallery

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Thanks for sharing Dave but why is the script called 'rotate'?!!!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Evangelist Dave Morton's Avatar
    Join Date
    Sep 2003
    Location
    Carson City, NV
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mostly because I'm not very good at keeping to proper naming conventions. I came VERY close to naming it "Fred".
    Making a difference, one little psychotic episode at a time
    Geek Cave Creations
    Beta testers needed for pChat
    Dave's Gallery

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For all of you looking for a way to transform images on the fly, you might want to check the open-source project Asido: 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/

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    hi ansys and welcome to the forums

    If you look at Dave's pst #57 he gives a script that uses GD to resize the images, it uses the following functions:

    ImageCreateFromJPEG
    imagecreatetruecolor
    imagecopyresampled

    Read through the manual pages about these functions and look at the user added notes. It should give you the required information.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Hi etgen (and anyone else!)
    The script above will work but will give you very poor image quality.
    I have changed the script slightly to add imagecreatetruecolor instead of imagecreate.

    To use this on more than one image, you will need to save it as a separate php file eg: thumbnail.php and call it in the <img src> tag

    thumbnail.php
    PHP Code:
    <?php
    // File and new size
    $filename $_GET['img'];
    $percent 0.5;

    // Content type
    header('Content-type: image/jpeg');

    // Get new sizes
    list($width$height) = getimagesize($filename);
    $newwidth $width $percent;
    $newheight $height $percent;

    // Load
    $source imagecreatefromjpeg($filename);
    $thumb imagecreatetruecolor($newwidth$newheight);


    // Resize
    imagecopyresized($thumb$source0000$newwidth$newheight$width$height);

    // Output
    imagejpeg($thumb);
    ?>


    You pass the name of the image (and path) directly to the script and you can also easily change it to take a thumbnail width.

    usage:

    Code:
    <img src="simpleImgResize.php?img=images/myimage.jpg" />
    <img src="simpleImgResize.php?img=images/myotherimage.jpg" />

    HTH

    Spike
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot Spike! Works great!

  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Hi Rusk, Animated gif's cant be resized in PHP without being 'flattened' into a single image. Not sure if ImageMagick can retain the data or not but you might want to look into it as an option
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    @Daquan ~ Check as the Anonymous Coward suggested, if it's still not playing - register and start a thread

    @aUser ~ what doesnt output anything? The original script? the script I or Mendez wrote? Be specific!

    @Adam Coppard ~ To save the image instead of serving it as a thumbnail, change the arguments on the imagejpeg function:
    PHP Code:
    /* Save the thumbnail
        **
        ** $thumb = generated image
        ** $filename = location of final image eg 'images/thumbnails/myThumb.jpg
        ** $quality = Quality of the thumbnail; 0 - 100 */

    imagejpeg($thumb$filename$quality); 
    @Manuel ~ The images look choppy because the browser is doing the resizing and browsers aren't supposed to do that. The rendering engine in IE isnt designed to resize images but does the best it can.

    Either manually resize them in photoshop or use the thumbnailing script above
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  12. #12
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    468
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Not my code!!

    Hi

    I have "inherited" a web site to manage. This site has a problem with the image upload not creating thumbnails but the original designer left no comments as to how he wrote his code.

    I think the problem lies in the following somewhere but I am at a loss as to exactly where. The site stores in a database both reduced size and thumbs for display on a single page.

    Any helpful comments most appreciated.

    PHP Code:
    <?php 

    if($_POST[sbmt]):
                 for(
    $i=0;$i<count($_FILES[foto][tmp_name]);$i++):
                if(
    $_FILES[foto][tmp_name][$i]):
                   
    $r=mysql_query("insert into " $prev "property_pics(id) values (\"" $_REQUEST[id] . "\")");
                    
    $pid=mysql_insert_id();
                   
    $e=explode(".",$_FILES[foto][name][$i]);
                   
    $ext=substr($_FILES[foto][name][$i],-3,3); 
                   
    $size=$foto_size;
                   
    $ext=strtolower($e[1]);
                   if(
    $ext=="peg"){$ext="jpg";}
                   
    copy($_FILES[foto][tmp_name][$i],"../pics/" $pid "." $ext);
                   
    copy($_FILES[foto][tmp_name][$i],"../pics/thumb-" $pid "." $ext);               
                   
    mysql_query("update " $prev "property_pics set file='" .  $pid "." $ext "',thumb='thumb-" $pid "." $ext "' where pid=" $pid);                
                   
    $nw=145;$nh=133;
                   
    $img=$tpath "/thumb-" $pid "." $ext;
                   
    $dimensions GetImageSize($img);
                   
    $thname $img;
                   
    $w=$dimensions[0];
                   
    $h=$dimensions[1];
                   if(
    $ext=="jpg"):
                       
    $img2 ImageCreateFromJpeg($img);
                   elseif(
    $ext=="gif"):
                      
    $img2 ImageCreateFromGif($img);
                   elseif(
    $ext=="png"): 
                      
    $img2 ImageCreateFromPng($img);
                   endif;    
                   
    $thumb=ImageCreateTrueColor($nw,$nh);
                   
    $wm $w/$nw;
                   
    $hm $h/$nh;
         
                    
    $h_height $nh/2;
                   
    $w_height $nw/2;
                   if(
    $w $h)
                   {
                       
    $adjusted_width $w/$hm;
                       
    $half_width $adjusted_width 2;
                       
    $int_width $half_width $w_height;
                       
    ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
                       if(
    $ext=="jpg"):
                            
    ImageJPEG($thumb,$thname,95); 
                       elseif(
    $ext=="gif"):
                            
    ImageGIF($thumb,$thname,95); 
                       elseif(
    $ext=="png"): 
                            @
    ImagePNG($thumb,$thname,95); 
                       endif;    
                    }
                    elseif((
    $w $h) || ($w == $h))
                    {
                        
    $adjusted_height $h $wm;
                        
    $half_height $adjusted_height 2;
                        
    $int_height $half_height $h_height;
                        
    ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
                        if(
    $ext=="jpg"):
                            
    ImageJPEG($thumb,$thname,95); 
                        elseif(
    $ext=="gif"):
                           
    ImageGIF($thumb,$thname,95); 
                        elseif(
    $ext=="png"): 
                           
    ImagePNG($thumb,$thname,95); 


                        endif;    
                    }
                    else
                    {
                          
    ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h);     
                        if(
    $ext=="jpg"):
                           
    ImageJPEG($thumb,$thname,95); 
                        elseif(
    $ext=="gif"):
                           
    ImageGIF($thumb,$thname,95); 
                        elseif(
    $ext=="png"): 
                             
    ImagePNG($thumb,$thname,95); 
                        endif;    
                     }
                     
    imagedestroy($img2);
                     
                       
    $target=420;
                       
    $img=$tpath "/" $pid "." $ext;
                       
    $dimensions GetImageSize($img);
                       
    $thname $img;
                       
    $w=$dimensions[0];
                       
    $h=$dimensions[1];
                       
    $width=$w;
                       
    $height=$h;
                       if((
    $width>$target && $height>$target) || $width>$target):
                           
    $cent=100-round((($width-$target)/$width)*100);
                           
    $width=round($width*$cent/100);
                           
    $height=round($height*$cent/100);
                       elseif(
    $height>$target):
                           
    $cent=100-round((($height-$target)/$height)*100);
                           
    $width=round($width*$cent/100);
                           
    $height=round($height*$cent/100);
                       endif;
                       if((
    $width>$target && $height>$target)|| $width>$target):
                            
    $cent=100-round((($width-$target)/$width)*100);
                            
    $width =round($width*$cent/100);
                            
    $height=round($height*$cent/100);
                       elseif(
    $height>$target):
                            
    $cent=100-round((($height-$target)/$height)*100);
                            
    $width =round($width*$cent/100);
                            
    $height=round($height*$cent/100);
                       endif;
                       
    $nw=$width;$nh=$height;
                       
                       if(
    $ext=="jpg"):
                           
    $img2 ImageCreateFromJpeg($img);
                       elseif(
    $ext=="gif"):
                          
    $img2 ImageCreateFromGif($img);
                       elseif(
    $ext=="png"): 
                          
    $img2 ImageCreateFromPng($img);
                       endif;    
                       
    $thumb=ImageCreateTrueColor($nw,$nh);
                       
    $wm $w/$nw;
                       
    $hm $h/$nh;
             
                        
    $h_height $nh/2;
                       
    $w_height $nw/2;
                       if(
    $w $h)
                       {
                           
    $adjusted_width $w/$hm;
                           
    $half_width $adjusted_width 2;
                           
    $int_width $half_width $w_height;
                           
    ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
                           if(
    $ext=="jpg"):
                                
    ImageJPEG($thumb,$thname,95); 
                           elseif(
    $ext=="gif"):
                                
    ImageGIF($thumb,$thname,95); 
                           elseif(
    $ext=="png"): 
                                
    ImagePNG($thumb,$thname,95); 
                           endif;    
                        }
                        elseif((
    $w $h) || ($w == $h))
                        {
                            
    $adjusted_height $h $wm;
                            
    $half_height $adjusted_height 2;
                            
    $int_height $half_height $h_height;
                            
    ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
                            if(
    $ext=="jpg"):
                                
    ImageJPEG($thumb,$thname,95); 
                            elseif(
    $ext=="gif"):
                               
    ImageGIF($thumb,$thname,95); 
                            elseif(
    $ext=="png"): 
                               
    ImagePNG($thumb,$thname,95); 
                            endif;    
                        }
                        else
                        {
                              
    ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h);     
                            if(
    $ext=="jpg"):
                               
    ImageJPEG($thumb,$thname,95); 
                            elseif(
    $ext=="gif"):
                               
    ImageGIF($thumb,$thname,95); 
                            elseif(
    $ext=="png"): 
                                 
    ImagePNG($thumb,$thname,95); 
                            endif;    
                         }
                         
    imagedestroy($img2);
                    endif;
                endfor;
     
                    echo
    "<script>alert('Photo successfully Updated');window.location.href='property.pictures.php?id=" $_REQUEST[id] . "';</script>";        
             endif;      
          
          
    $r=mysql_query("select * from " $prev "property where id="$id);
          
    $d=@mysql_fetch_array($r);
          
    ?>

  13. #13
    SitePoint Member
    Join Date
    Jun 2008
    Location
    Stockholm
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great code and works like it should.

  14. #14
    SitePoint Member jellen's Avatar
    Join Date
    Jul 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I usually use programm to resize my pictures,then upload the pictures to my host! Picture Resize Genius is a good choice, I usually batch resize my photos,it is fit for me;

  15. #15
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  16. #16
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to use this picture resizing tool It works well! This is site softorbits.com/batch_picture_resize/

  17. #17
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    please note the date that this was first published: Jul 23, 2004

    Alot has changed since then!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •