Jump to content

resizing image files in PHP into thumbnails - efficiently


anita999

Recommended Posts

I have a list of items with their images displayed in a column.  I would like to resize the images into small fixed size images.  Is there a way to do this efficiently since I have about 10 items per page.  I also do not want resized images to be produced in the file directory?  Any suggestions on how to do this efficiently would be appreciated.

 

Thanks

 

 

Link to comment
Share on other sites

Well to do it efficiently you would want to save the thumbnails in a separate folder so that you wont have to dynamically resize every image on the page every time the image is requested.

 

As for actually doing the resizing, google is a great place to find tutorials...

http://www.google.com/search?q=resizing+an+image+in+php&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

Link to comment
Share on other sites

This should resize your image fairly easy. I'm sure you could come up with something better if you modified it a bit. You could set it up as a function so it'll be easy to use.

<?php
// The file you are resizing
$file = 'yourfile.jpg';

//This will set our output to 45% of the original size
$size = 0.45;

// This sets it to a .jpg, but you can change this to png or gif
header('Content-type: image/jpeg');

// Setting the resize parameters
list($width, $height) = getimagesize($file);
$modwidth = $width * $size;
$modheight = $height * $size;

// Resizing the Image
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

// Outputting a .jpg, you can make this gif or png if you want
//notice we set the quality (third value) to 100
imagejpeg($tn, null, 100);
?>

Link to comment
Share on other sites

Thank you.  I tried the sample code but when the image is printed to the screen, it prints out symbols instead of the image.

 

I tried reading in a gif file and changed all references to the code from jpeg to gif.  But that didn't work either.

Link to comment
Share on other sites

I think your using the code wrong. Create a file called thumbnail.php and place this code in. Now simply use thumbnail.php like it was an image and call it like:

 

<img src="http://www.yoursite.com/thumbnail.php">

 

this should work for you as long as you have an image called yourfile.jpg in the same directory. You could use $_GET to specific which image you wanna thumbnail as well, rather then naming a file within thumbnail.php. Hope that helps!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.