Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you use PHP's Image Functions to...

Status
Not open for further replies.

monkle

Programmer
Feb 11, 2004
132
0
0
US
Can you use PHP's image functions to create a function that will automatically generate image thumbnails on the fly? I have the code that will resize the image, but the function pretty much kills whatever page it's being used on. The resized image is the only thing that is displayed.

Can you give me a reference to more information about this? I've spent a few days researching it without much luck.
 
Sure, many people do that already.

What does your code look like?

Basically what you have to do is have your code write out the headers for the image and then the image. Invoke your code from the HTML <img> tag.

Code:
<img src="mythumb.php?file=xyz">

Ken
 
Have a look at these function at php.net:

ImageCreate()
ImageCreateFromJPEG()
ImageCopyResized()
ImageJPEG();


Jakob
 
This is what I have so far:

Code:
<?php
// This function is used to create thumbnail images on the fly

function thumb($upc, $width, $height)
{
  $url = '[URL unfurl="true"]http://m4s-s1/images/';[/URL]
  $filename = $upc.'_a.jpg';
  $filepath = $url.$filename;
  #echo $filepath;
  #exit;
  // Content type

  // Get new dimensions
  list($width_orig, $height_orig) = getimagesize($filepath);

  // Resample
  $image_p = imagecreatetruecolor($width, $height);
  $image = imagecreatefromjpeg($filepath);
  imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  // Output
  return imagejpeg($image_p, null, 50);
}
#thumb('741914010265', $width = 96, $height = 130);
?>
 
I have a simple, but yet, very efficcient script.

On upload, the script meerly uploads the image, stores reference to the pic in a db, stores picture information, text, owner-id, etc.

When the user then goes back to the "picture index", I have a code like this:

Code:
if (!is_file("tn_{$row['picture']}"))
  {
    makeThumb("tn_{$row['picture']}");
  }

I also use this for the browsing area for visitors.
Then, inside the makeThumb, I check the main picture..if it is corrupt, it does not try to make thumb or try to display it.

I show pictures and thumbs via a picture script, which has two actions: list thumbs / list fullsize

I gather them like:
<img src="picture.php?pid=229823" />

I however generate the <img> with title, etc. also a variable inside the src that is not needed. Why? I use it to optimize for google.

eg: if you save your files as <random number>.jpg, google will not index them good.. that's why you do like this:

<img src="page.php?google=man_check_out_this_picture&amp;pid=3123123" alt="man%20check%20out%20this%20picture" />

I hope you are following me

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top