Jump to content

Archived

This topic is now archived and is closed to further replies.

SharkBait

Best Way: Image to Thumbnail

Recommended Posts

Hi,

  I am looking at learning to play with the GD by incorporating it into my little site.

I want to be able to upload images and have the GD create a thumbnail and store it in a thumbnail directory and have the original image stored in another directory.  These entries will also be put into a database.

What should I be looking into?  I can't remember that URL for the GD site, but I am sure someone else can.

When I display an image to someone on my site, can I have the GD put a border around the image as well?

Or is it imagemagik that I should be looking into?

Share this post


Link to post
Share on other sites
For more documentation on using GD see

[url=http://nl2.php.net/gd]http://nl2.php.net/gd[/url]
[url=http://www.phpbuilder.com/columns/index.php3?cat=2&subcat=17]http://www.phpbuilder.com/columns/index.php3?cat=2&subcat=17[/url]

Ronald  8)

Share this post


Link to post
Share on other sites
Thoughs are some decent tutorials, thanks

Is it possible to create the thumbail on the fly?  What I mean is when a page is loaded and images are to be shown that it shows a tempoary thumbnail instead of the full image?  Or is it best to create the thumbnail upon uploading of the image and just store it?

Share this post


Link to post
Share on other sites
I would think you would be better creating the thumbnail when the image is uploaded. That way you are only doing this piece of processing once, rather than doing it every time someone views the image.

Share this post


Link to post
Share on other sites
I've always used lixlpixel's image upload/resize script and create the thumbnails once (at upload time) - http://fundisom.com/phparadise/php/image_handling/image_upload_and_resize

It works 'straight out of the box'.

As for the borders, don't add them to the thumbnails because next week you'll decide you wanted a different colour border. Instead, use some simple CSS to apply padding and a border to the thumbnails when they are displayed.  Then next week, change one line of CSS rather than editing a trillion images :)

Share this post


Link to post
Share on other sites
Simple thumbnail generator:

[code]
<?php
function generate_thumbnail($imagefile, $thumb_max_dimension = 200, $quality = 90, $directory = "", $thumb_prefix = "thumb_")
{
  /* v1.0.1 (by acdx) */
  $filename_arr = explode(".", basename($imagefile));
  $filetype = $filename_arr[1];

  if($filetype == "jpg")
    $filetype = "jpeg";

  if($filetype != "jpeg" && $filetype != "gif" && $filetype != "png")
    return false;

  $original_size = getimagesize($imagefile);

  eval("\$image = imagecreatefrom".$filetype."(\$imagefile);");

  if($original_size[0] > $original_size[1])
  {
    if($original_size[0] > $thumb_max_dimension)
      $thumb_width = $thumb_max_dimension;
    else
      $thumb_width = $original_size[0];
    $thumb_height = $original_size[1]*($thumb_width/$original_size[0]);
  }
  else
  {
    if($original_size[1] > $thumb_max_dimension)
      $thumb_height = $thumb_max_dimension;
    else
      $thumb_height = $original_size[1];
    $thumb_width = $original_size[0]*($thumb_height/$original_size[1]);
  }

  $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
  imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $original_size[0], $original_size[1]);
  imagejpeg($thumb, $directory.$thumb_prefix.basename($imagefile).".jpg", $quality);
  imagedestroy($thumb);
  imagedestroy($image);
}
?>
[/code]

Works with jpg, png and gif files.
Note: thumbnails have double extension!

Share this post


Link to post
Share on other sites
acdx: Ah that works nicely, though it was missing [code=php:0] $image = imagecreatefromjpeg($imagefile); [/code]

AndyB: I was using the one you mentioned but it somehow, didnt resize the image properly.  But I was able to use it and take bits from what acdx posted and now I have a working thumbnail generator.

Thanks :)

Now I gota figure out do I create some sorta inline image uploader, or do I just do it seperately and have my article editor select from the database of images?  Oh well thats another topic I suppose.

Thanks again.

Share this post


Link to post
Share on other sites
Look again...

eval("\$image = imagecreatefrom".$filetype."(\$imagefile);");

Share this post


Link to post
Share on other sites

×

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.