Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-08-2005, 02:47 AM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Dynamic Image Resizing

This script is usefull for dynamicaly thumbnailing images. Its pretty simple. One page that you run your images through. Example

Code:
<img src="thumbit.php?image=http://www.mysite.com/images/myphoto.jpg">
Here is the code.

PHP Code:
<?php

    
// Input
    
$s_image $_GET['image']; // Image url set in the URL. ex: thumbit.php?image=URL
    
$e_image "error.jpg"// If there is a problem using the file extension then load an error JPG.
    
$max_width 100// Max thumbnail width.
    
$max_height 250// Max thumbnail height.
       
$quality 100// Do not change this if you plan on using PNG images.

    // Resizing and Output : Do not edit below this line unless you know what your doing.

    
if (preg_match("/.jpg/i""$s_image"))  {

    
header('Content-type: image/jpeg');
      list(
$width$height) = getimagesize($s_image);
    
$ratio = ($width $height) ? $max_width/$width $max_height/$height
    if(
$width $max_width || $height $max_height) { 
    
$new_width $width $ratio
    
$new_height $height $ratio
    } else {
    
$new_width $width
    
$new_height $height;
    } 
      
$image_p imagecreatetruecolor($new_width$new_height);
    
$image imagecreatefromjpeg($s_image); 
    
imagecopyresampled($image_p$image0000$new_width$new_height$width$height);
    
imagejpeg($image_pnull$quality); 
    
imagedestroy($image_p); 

    }
       elseif (
preg_match("/.png/i""$s_image"))  {

    
header('Content-type: image/png');
      list(
$width$height) = getimagesize($s_image);
    
$ratio = ($width $height) ? $max_width/$width $max_height/$height
    if(
$width $max_width || $height $max_height) { 
    
$new_width $width $ratio
    
$new_height $height $ratio
    } else {
    
$new_width $width
    
$new_height $height;
    } 
    
$image_p imagecreatetruecolor($new_width$new_height);
    
$image imagecreatefrompng($s_image); 
    
imagecopyresampled($image_p$image0000$new_width$new_height$width$height);
    
imagepng($image_pnull$quality); 
    
imagedestroy($image_p); 

    }
       elseif (
preg_match("/.gif/i""$s_image"))  {

    
header('Content-type: image/gif');
      list(
$width$height) = getimagesize($s_image);
    
$ratio = ($width $height) ? $max_width/$width $max_height/$height
    if(
$width $max_width || $height $max_height) { 
    
$new_width $width $ratio
    
$new_height $height $ratio
    } else {
    
$new_width $width
    
$new_height $height;
    } 
    
$image_p imagecreatetruecolor($new_width$new_height);
    
$image imagecreatefromgif($s_image); 
    
imagecopyresampled($image_p$image0000$new_width$new_height$width$height);
    
imagegif($image_pnull$quality);
    
imagedestroy($image_p); 

    }
       else {

    
// Show the error JPG.
    
header('Content-type: image/jpeg');
    
imagejpeg($e_imagenull$quality); 
    
imagedestroy($e_image); 

    }

?>
I hope this is usefull. If you want to have JPG and GIF images at different qualities and automatically changing PNG to 100% quality then replace the quality variable with the fallowing code.

PHP Code:
if (!(preg_match("/.png/i""$s_image"))) {
  
$quality 75// Set your quality
} else {
  
$quality 100// Do not change this or PNGs will not load correctly

Element is offline   Reply With Quote
Old 11-09-2005, 02:57 PM   PM User | #2
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
nice, mine is a little faster with the generation but does not handle png.
__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<
rlemon is offline   Reply With Quote
Old 11-09-2005, 10:15 PM   PM User | #3
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by rlemon
nice, mine is a little faster with the generation but does not handle png.
Gerneration time is defined by compression. If the quality is lower it loads in a second for me, even on Dial-Up.
Element is offline   Reply With Quote
Old 11-09-2005, 10:57 PM   PM User | #4
rlemon
Senior Coder

 
Join Date: Apr 2005
Posts: 1,051
Thanks: 0
Thanked 0 Times in 0 Posts
rlemon is on a distinguished road
how would i lower the quality of a gif or jpeg image?

Edit:

nevermind i see it

__________________
public string ConjunctionJunction(string words, string phrases, string clauses)
{
return (String)(words + phrases + clauses);
}
<--- Was I Helpfull? Let me know ---<

Last edited by rlemon; 11-09-2005 at 11:02 PM..
rlemon is offline   Reply With Quote
Old 11-10-2005, 08:57 AM   PM User | #5
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
A few notes here.

The GD libraries use a proprietary format for manipulating the pixel colours which is quite bulky (basically about 9 times the size of a 80% quality jpeg - eg if you put a 20kb jpeg into GD the processor would be working on roughly 180kb of data during the resizing / transformation).

As such, it is never a good idea to create thumbnails at every page load. Instead, a server cached image should be created and the <img tag pointed at that. A script such as the one Element posted could be used just by changing the imagePng($image_p, null, $quality); calls to something like imagePng($image_p, '/home/site/imgs/mysavedimage.png', $quality);

May also like to note that reading the mime header to determine image type is considered more accurate, eg:

PHP Code:
$f_type getimagesize($base_image);
$created_image =     ($f_type[2] < 4)
                        ? (
$f_type[2] < 3)
                            ? (
$f_type[2] < 2)
                                ? (
$f_type[2] < 1) ?
                                
NULL
                            
imagecreatefromgif($base_image)
                        : 
imagecreatefromjpeg($base_image)
                    : 
imagecreatefrompng($base_image)
                    : 
NULL;
if(
$created_image !== NULL)
    { 
// ok to proceed as we have a created image 
On an aside, the libraries only recognize ping compression of -1 through 9 as mentioned at gdImagePngEx so I guess that an 85 would be re-evaluated to the nearest integer after division.

Edit: removed link as it semi goes against the forum ideals
__________________
Ökii - formerly pootergeist
teckis - take your time and it'll save you time.

Last edited by Ökii; 11-11-2005 at 01:58 PM..
Ökii is offline   Reply With Quote
Old 11-14-2005, 02:05 AM   PM User | #6
eksob
New Coder

 
Join Date: Oct 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
eksob is an unknown quantity at this point
The script worked great for me for the first couple of days...

All of a sudden I get errors now, and I didn't change anything. I have done some research on the errors, without any answers...maybe you guys can help me out.

PHP Code:
<br />
<
b>Warning</b>:  getimagesize(http://www.mysite.com/photos/butterfly.gif): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
 
in <b>/home//public_html/reviews/thumbit.php</b> on line <b>53</b><br />
<br />
<
b>Warning</b>:  Division by zero in <b>/home//public_html/reviews/thumbit.php</b> on line <b>54</b><br />
<br />
<
b>Warning</b>:  imagecreatetruecolor(): Invalid image dimensions in <b>/home//public_html/reviews/thumbit.php</b> on line <b>62</b><br />
<br />
<
b>Warning</b>:  imagecreatefromgif(http://www.mysite.com/photos/butterfly.gif): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
 
in <b>/home//public_html/reviews/thumbit.php</b> on line <b>63</b><br />
<br />
<
b>Warning</b>:  imagecopyresampled(): supplied argument is not a valid Image resource in <b>/home//public_html/reviews/thumbit.php</b> on line <b>64</b><br />
<br />
<
b>Warning</b>:  imagegif(): supplied argument is not a valid Image resource in <b>/home//public_html/reviews/thumbit.php</b> on line <b>65</b><br />
<br />
<
b>Warning</b>:  imagedestroy(): supplied argument is not a valid Image resource in <b>/home//public_html/reviews/thumbit.php</b> on line <b>66</b><br /> 
(Directory structure was edited for privacy)

Thanks - besides this error, the script worked awesome
eksob is offline   Reply With Quote
Old 11-14-2005, 04:01 PM   PM User | #7
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
You shouldn't use URL's. It tries to connect from your server to your ferver which is the long way around. Your server gives you the 500 error and without the values getimagesize() sets you get the "division by zero" errors.
The false that is returned from imagecreatefrom***() gives all the other errors.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 11-15-2005, 08:14 AM   PM User | #8
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by marek_mar
You shouldn't use URL's. It tries to connect from your server to your ferver which is the long way around. Your server gives you the 500 error and without the values getimagesize() sets you get the "division by zero" errors.
The false that is returned from imagecreatefrom***() gives all the other errors.
The URL should be contained in quotes if your just putting it in like $s_image = "URL";

And if the image is on your own server it is better to do something like thumbit.php?image=file/image.ext As for using mime types, I have been looking for better way to do it and that seems to work. As for cache images I was told not to use them and use this way, and it works fast and crisply for me so I see no reason to change it if someone wants a fast easy way of doing it with just a remote or local image.
Element is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:29 PM.


Advertisement
Log in to turn off these ads.