Custom-sizing PHP thumbnail generator code (ThumbFly)

9 posts / 0 new
Last post
puravida's picture
puravida
Jedi Warrior
Offline
Joined: 09/01/2007
Visit puravida's Website

I wrote this piece of code recently in order to show thumbnails on-the-fly (using PHP) for a user uploaded gallery. It is VERY simplistic but may be ideal for those of you who want to simply show a good-looking, quick-loading thumbnail in the perfect size without spending any money or writing code. Wink

To avoid confusion, let me say this is NOT for showing website thumbnails. It is only good for generating thumbnails of images (local or remote) on your website. To show website thumbnails on-the-fly, please join our main service for free at https://shrinktheweb.com

Here's the PHP thumbnail generator code:

<?php
/************************************************************
  ThumbFly:	v.1-0-0
  
  Purpose:	Outputs a thumbnail image of a jpeg image file
		     Local, Remote, or Dynamic (w/ mods)

   Author:	Brandon Elliott
   URL:		https://shrinktheweb.com/content/custom-sizing-php-thumbnail-generator-code-thumbfly.html
************************************************************/
	$Size=$_GET['Size'];
	$filename=$_GET['filename'];
	$w=$_GET['w'];

	if($Size) {$filename=$filename."&Size=".$Size;}
	// Content type
	header('Content-type: image/jpeg');
	
	// Get new dimensions
	list($width, $height) = getimagesize($filename);
	$ratio=$w/$width;
	$new_height = $height * $ratio;
	
	// Resample
	$image_p = imagecreatetruecolor($w, $new_height);
	$image = imagecreatefromjpeg($filename);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $new_height, $width, $height);
	
	// Output
	imagejpeg($image_p, null, 100);
?>

To use this code, you would use:

<img src=http://www.abc.com/thumb.php?filename=path/to/file.jpg&w=width>

You will need to replace abc with your domain, the path/to/file.jpg with the relative path to the image on your server and then, you will just put in whatever width you want. For instance, I use w=150 for a 150 pixel wide thumbnail (output is proportionate to original).

Note in my example that I called the file "thumb.php."

My code is written only to support jpegs, but if you have a little PHP experience, you could easily modify it to support gif, bmp, png, etc.

I hope this is helpful to some of you!

Best regards,

Brandon

puravida's picture
puravida
Jedi Warrior
Offline
Joined: 09/01/2007
Visit puravida's Website

It just occurred to me and I should mention that this code will, show a thumbnail of images on remote servers as well! So, it could be used to show custom-sized thumbnails of images hosted by free image hosts or any remote server.

The quality is retained well and it even scales "up" very well -considering. So, you could even call it with a size larger than the original and it will look better than if you just set the browser dimensions to "stretch" the image.

Enjoy!

Brandon

puravida's picture
puravida
Jedi Warrior
Offline
Joined: 09/01/2007
Visit puravida's Website

I had an epiphany and realized that the way the URL is broken up when calling the STW images is still passing the variables.

So, if you simply add:
if($Size) {$filename=$filename."&Size=".$Size;}
to the very top of the file (just after <?php), then it will rebuild the query and you can actually show STW thumbnails in a custom-size on-the-fly!

The only drawbacks are that enlarging the image is not as good as capturing at a specific size and this could produce a bit of a strain on your server if you have a lot of thumbnails on a page (i.e. a directory or search engine). But, if you only show 1 or 2 thumbnails on a given page, then you can just use this file on your server and not even have to pull custom-sized thumbnails from ShrinkTheWeb!

However, if speed or quality are crucial, then you probably still want to upgrade to PRO and just get the perfect sized thumbnail efficiently from our servers.

Cheers,

Brandon
p.s. I modified the original post above for clarity

Anonymous

brandon

Thanks for the code snippet. It was just exactly what I was looking for!

P.S. I like the forum, especially the security for creating an account.

puravida's picture
puravida
Jedi Warrior
Offline
Joined: 09/01/2007
Visit puravida's Website
the_driver_79 wrote:
Thanks for the code snippet. It was just exactly what I was looking for!

Great! Glad it could help you out. Wink

Quote:
P.S. I like the forum, especially the security for creating an account.

Hehe... thanks. The forum was filling up with spammers and was completely unusable until I added that!

Anonymous

Working great!

I know that is OT, but do you have any suggestions for frameworks? I looked at Ruby on Rails, but the installation looked bulky and complicated. (btw: ubuntu 7.04, apache, mysql...p/p/p)

-Derrick

puravida's picture
puravida
Jedi Warrior
Offline
Joined: 09/01/2007
Visit puravida's Website

Looks great.

As far as frameworks, I don't use one. I've heard lots of great things about Ruby but haven't had the chance to really take advantage of it.

Sorry I cannot be more helpful in that regard.

-Brandon

Anonymous

Hey Brandon,

wonder how to add a randomizer to this?

Thanks

Robert

puravida's picture
puravida
Jedi Warrior
Offline
Joined: 09/01/2007
Visit puravida's Website

Hi Robert,

I'm not sure I follow what you are asking...

If you want to randomize the display of thumbnails using "thumbfly", that is beyond the scope of my publishing it for free use. Wink

Cheers,

Brandon

ShrinkTheWeb® (About STW) is another innovation by Neosys Consulting
Contact Us | PagePix Benefits | Learn More | STW Forums | Our Partners | Privacy Policy | Terms of Use

Announcing Javvy, the best crypto exchange and wallet solution (coming soon!)

©2018 ShrinkTheWeb. All rights reserved. ShrinkTheWeb is a registered trademark of ShrinkTheWeb.