Jump to content

Thumbnails


Schlo_50

Recommended Posts

I need to have page of items displayed as thumbnails which when clicked, go to another page which then shows the items details.

 

I have a database and am currently storing the full size images URL's instead of the actual image. All i need now is the code the display the images as scaled down thumbnails when the URL stored in my database is called.

 

Anyone got some examples?

Link to comment
Share on other sites

Create a php page e.g image.php and insert this code:

 

<?
   $pic = $_GET['pic'];
   $width = $_GET['width'];
   if (strstr ($pic, "jpg")){
     $im = imagecreatefromjpeg($pic);
   }else{
     $im = imagecreatefromgif($pic);
   }
  $px    = (imagesx($im) - 7.5 * strlen($string)) / 2;
  $old_x=imageSX($im);
  $old_y=imageSY($im);

  $new_w=(int)($width);
  if (($new_w<=0) or ($new_w>$old_x)) {
    $new_w=$old_x;
  }

  $new_h=($old_x*($new_w/$old_x));
  $thumb_w=$new_w;
  $thumb_h=$old_y*($new_h/$old_x);

  $thumb=ImageCreateTrueColor($thumb_w,$thumb_h);
  imagecopyresampled($thumb,$im,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

  imagejpeg($thumb,"",90);
  imagedestroy($thumb);

  header("Content-type: image/jpeg");
?>

 

You can then resize images like:

 

http://mywebsites/image.php?pic=images/somepic.jpg&width=130

 

Added: This requires the GD library compiled into PHP :)

Link to comment
Share on other sites

Ever consider HTML?

 

<a href="link.ext"><img src="thumb.ext" border="0" width="20" height="30"></a>

 

Or if you wanted to echo it

 

echo "<a href='link.ext'><img src='thumb.ext' border='0' width='20' height='30'></a>";

 

 

 

 

I Think He Meant How To Create A Thumbnail Image Of A File Using PHP...

Link to comment
Share on other sites

Ever consider HTML?

 

<a href="link.ext"><img src="thumb.ext" border="0" width="20" height="30"></a>

 

Or if you wanted to echo it

 

echo "<a href='link.ext'><img src='thumb.ext' border='0' width='20' height='30'></a>";

 

 

 

 

I Think He Meant How To Create A Thumbnail Image Of A File Using PHP...

 

Could be, but he never specified.

What ever gets the job done!

 

Why is there "[/url]" in my post!? I never typed that....  :-\

Link to comment
Share on other sites

Working with resize of an actual image can really mess with the aspect of it.  If you just set the width to 20 then it will keep the aspect of them all at 20 while some may be a differnt height but will make the image dispalyed looking nicer not warped.

 

Creating thumbnails is a better approach since becasue you are resizing the orginal image it is loading the full size of that file for display.  So having some odd 100 images you are now making users download 3 MB in images just to have them resized for viewing.  While you could have that same 100 images in thumbs and only be sending less then 1 MB of images to download.

Link to comment
Share on other sites

Right Ive Found Both Of These (Both Are Untested By Me)

1.

<?
   //calculate thumb size
           $ow = imagesx($im);
           $oh = imagesy($im);
           $maxh = 100;
           $maxw = 150; 
           $new_h = $oh;
           $new_w = $ow;

           if($oh > $maxh || $ow > $maxw){
               $new_h = ($oh > $ow) ? $maxh : $oh*($maxw/$ow);
               $new_w = $new_h/$oh*$ow;
           }

           //create dst image
           $dst_img = ImageCreateTrueColor($new_w,$new_h);
           //resize and copy image
           ImageCopyResized($dst_img, $im, 0,0,0,0, $new_w, $new_h, ImageSX($im), ImageSY($im));    
           $function_image_new($dst_img,$galdir.$file);
?> 

 

 

<?php
$file = "test.txt"; //FILEADDRESS
$size = "100"; //IMAGE SIZE
$data = file($file);
$im = @ imagecreate($size, $size) or die();
$bc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);
$lines = (int) $size / 10;
for ($t = 0; $t < $lines; $t++)
{
   imagestring($im, 3, 4, 4, $data[$t], $tc);
   imagestring($im, 3, 4, 4 + ($t * 10), $data[$t], $tc);
}
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
?>

 

 

 

Thnx,

Tarun

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.