[SOLVED] Generate Thumbnails on the fly with JPG/GIF files..

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

[SOLVED] Generate Thumbnails on the fly with JPG/GIF files..

Post by infolock »

deicded to remove this.
Last edited by infolock on Tue Sep 06, 2005 3:54 pm, edited 3 times in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

What if someone use it like
<img src="resize_image.php?image_to_convert=http://www.google.com/">
? Have you tested this snippet against traversal+possible buffer overflow in GD ?

I'm a paranoic, indeed :D
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

decided to remove this.
Last edited by infolock on Tue Sep 06, 2005 3:54 pm, edited 1 time in total.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

If you wish to have it actually resize, instead of to a default 50x50, replace the $height = 50; with:

Code: Select all

$width = 50;
$height = round($current_image_height / ($current_image_width / $resize_width))
or, with width:

Code: Select all

$height = 50;
$width = round($current_image_width / ($current_image_height / $height))
Image Image
Stephanie
Forum Newbie
Posts: 1
Joined: Mon Apr 12, 2004 11:37 am

Using this snippet

Post by Stephanie »

I have tried this snippet on my server and I get a blue image with error on it, could you tell me what I am doing wrong?
Thank you,
Stephanie
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

Your code is a bit off... You should get a ratio of w/h for both your maximum dimensions, and the image in question. If the ratio of your max thumbnail is bigger than that of the image, you need to find out the new width of the thumbnail-to-be. Once you know the ratio and which dimension you need to match, you already have the formula to convert.

The trick with images is to work on ratios of w/h, as opposed to individual dimensions. If you adhere to the ratios, your images never look screwy, which is nice :)

If
ratio = w / h

then a new width for a thumbnail with a max. height of 100 is

w = ratio * h

where h = 100

That method means you get images with the correct aspect ratio, which is ESSENTIAL for photographs. You should also use imagecopyresampled for best results.

Another good idea is to cut down on GD processing. This can be easily achieved by caching the output. Take the image's filename and filemtime, and incorporate those into a filename in a suitable location. Upon invoking the image-resizing script, check to see if the thumbnail exists, and if present output it. If not, run the resize script and save the thumbnail in that location.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

If you have >= GD 2.01, use imagecopyresampled() instead of imagecopyresized(). It makes a world of difference - much smoother.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

the code's not that hard :)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Image Functions in localhost

Post by anjanesh »

For these image functions, all works ok in my site but not on my localhost. Is there anyway to get this worked in localhost ?
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

what version of php are you using? did you install gd ?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Localhost : php 4.3.4
My Website : php 4.3.4

I did not manually install gd since it was mentioned in php.net that 4.3 and up comes with gd intalled.

On my localhost a similar problem is there when including files with parameters - include("a.php?arg1=x&arg2=y");

So basically its the same thing - does not accept arguments on localhost.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you still need to tell it to load the module.. look in your php.ini for

Code: Select all

;extension=php_gd2.dll
or similar... (assuming a windows machine)
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

;extension=php_gd2.dll already exists in php.ini
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I removed the ; and its now extension=php_gd2.dll
I now get the error : Unable to load module php_gd2.dll.
I am using IIS6. Do I have to configure something in IIS ?
Post Reply