Jump to content

Best Way: Image to Thumbnail


SharkBait

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?

Link to comment
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?

Link to comment
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 :)
Link to comment
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!
Link to comment
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.
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.