Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Resize Images(thumbnails) on the fly 1

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
I was searching planetsourcecode for something to resize images into thumbnails on the fly, unfortunatly the only two PHP solutions offered there, only told the HTML the height and width to put in the <IMG> tag, which was kinda useless as you would still be downloading the whole full size of the image, so a little reasearch at php.net, and a bit of help by turning on the php_gd.dll extension in my php.ini file (I have PHP 4.1.2 Win32 binaries as my instalation, older versions may need to be recompiled with GD exentsion) and I came up with this code.


Code:
    <?
    /* Informs the browser the data being sent back is a Jpeg Image */
    Header (&quot;Content-type: image/jpeg&quot;);
    /* loads image passed thru script
    ie: gd.php?img_name=zoom.jpg */
    $src_img = imagecreatefromjpeg($img_name);
    /* desired width of the thumbnail */
    $picsize = 123; 
    /* grabs the height and width */
    $new_w = imagesx($src_img);
    $new_h = imagesy($src_img);
    /* calculates aspect ratio */
    $aspect_ratio = $new_h / $new_w;
    /* sets new size */
    $new_w = $picsize;
    $new_h = abs($new_w * $aspect_ratio);
    /* creates new image of that size */
    $dst_img = imagecreate($new_w,$new_h);
    /* copies resized portion of original image into new image */
    imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));
    /* return jpeg data back to browser 
        include a second parameter of a file name if
        you just want to save the new file to disk*/
    imagejpeg($dst_img);
    ?>

i've seen some examples, loop each time multiplying by .5 until it got close enough to the widh, but I found that to be slow and a waste of time when you could just determine the aspect ratio by deviding them. But in any case, the script above, runs in it's own php file, you call the Php passing it ?img_name=... giving it the image name you want to resize into a thumbnail, so you can use this in an image tag

<img src=&quot;gd.php?img_name=zoom.jpg&quot;> and it'll resize your image and return the resized iamge to the browser, all without having you download the full size of the original image.

Hope this is helpfull

Link to my post on planetsourcecode :
Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Just remember the PHP_GD library can only work with images with 256 colors or less.

I use ImageMagick ( myself and find that it works very quickly without using a lot of the servers resources.
 
um actually, it works with Jpegs just fine. the PHP_GD library doesnt handle images 256 and less (GIF), but does PNG, Jpeg, Windows Bitmaps, etc just fine. Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Also the code that I provided could be adapted so that it does not take server reasources as much, such as the first time it can create the jpeg after it has resized it, but in later executions check to see of the thumb has been created, then just show the new thumbnail. Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Oh looked into what you mentioned. On windows imagecreate works fine with jpeg and other truecolor images, however you move to a linux platform your resized image is greayed or dithered. I also found this command you should use instead of imagecreate.

imagecreatetruecolor

takes the same parameters of imagecreate only , it will allow your images to come back with the correct color, please note you need least GD extension version 2.* to use this command. (for you windows users that means you need to enable php_gd2.dll extension if you want to keep your source the same as your linux copy) Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top