Welcome, Guest. Please login or register.

Author Topic: A question for the web developers here...  (Read 1896 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16882
  • Country: gb
  • Thanked: 6 times
    • Show only replies by Karlos
A question for the web developers here...
« on: June 23, 2004, 03:59:44 PM »
Hi!

I'm wondering if the talented folk who maintain this place can help me with a wee php coding task I'm looking into.

I'm presently working on upgrading the admin site (developed in house before I started) for the gallery where I now work.

The server has an image repository for artwork images. These are rather large (300DPI) which are used for printing purposes.

However, the same images are also used within the actual site front end itself - simply left up to the browser to scale down. Naturally, this ties up a lot of bandwidth and its getting slow as hell even over the LAN.

So, I want to write an automated thumbnail generation system for it. I have examined the php GD library and can see that it has what I need by way of image resampling and so on. Furthermore, our server is set up with it already, so its ready to use.

However, all the examples I looked at are geared towards simply sending the scaled thumbnail direct to the browser. This isn't quite what I need.

What I want to do is to save the thumbnail back onto the server. The idea is, we want a function eg getThumbnail($image) which works as follows

1) check if $image has a corresponding thumbnail in the thumbnails folder

2) if false, generate a thumbnail and save to the thumbnails folder

3) if true, check the date of the image and thumbnail. If the date of the image > thumbnail, generate a new thumbnail to overwrite the old one.

4) return the path to the thumbnail image


So we use getThumbnail() like this within an image tag

img src = "< ?php getThumbnail($image); ? >"

where $image is the path to the full size image.

In short, I want a server side thumbnail cache ;-)

I can figure it through except for one thing. How do I save the generated thumbnail back on the server?

I assume that fwrite($file, $thumbnail) isn't quite sufficient...

Any tips?
int p; // A
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16882
  • Country: gb
  • Thanked: 6 times
    • Show only replies by Karlos
Re: A question for the web developers here...
« Reply #1 on: June 23, 2004, 04:51:06 PM »
@self

Never mind, I found the answer...
int p; // A
 

Offline CannonFodder

  • Hero Member
  • *****
  • Join Date: Sep 2003
  • Posts: 1115
    • Show only replies by CannonFodder
Re: A question for the web developers here...
« Reply #2 on: June 23, 2004, 05:57:13 PM »
Oooh the mysterery ...............:-P

Err I mean....mind sharin the anwser ?

Might be usefull to someone:-D  
People are hostile to what they do not understand - Imam Ali ibn Abi Talib(AS)
 

Offline lorddef

  • Hero Member
  • *****
  • Join Date: Apr 2002
  • Posts: 1149
    • Show only replies by lorddef
    • http://
Re: A question for the web developers here...
« Reply #3 on: June 23, 2004, 07:50:01 PM »
Well I should think he discovered the imageCreateJpeg() and imageCreateTrueColor() functions :-D
Restraining orders are just another way of saying I love you!
 

Offline Bezzen

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 637
    • Show only replies by Bezzen
    • http://www.streammountain.se
Re: A question for the web developers here...
« Reply #4 on: June 23, 2004, 08:00:51 PM »
@Karlos

What a coinsidence. I was just planning to do pretty much the exact same thing tomorrow for a web project of mine.  :-)
 

Offline KarlosTopic starter

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16882
  • Country: gb
  • Thanked: 6 times
    • Show only replies by Karlos
Re: A question for the web developers here...
« Reply #5 on: June 23, 2004, 11:00:21 PM »
Hi,

Well, the code I came up with is something like so:

Code: [Select]
[size=x-small]
/*
array createResampledJpeg(dest, source, maxW, maxH, [quality=80])

Generates a resampled JPEG image from an existing one on the server

dest:    destination image (server path/file - not the url)
source:  original image (server path/file - not the url)
maxW:    maximum width of destination image
maxH:    maximum height of destination image
quality: quality for jpeg encoding (default = 80)

returns: array of final width/height/path of resampled image
Requires php gd library

Thumbnail folder must have write permission!
*/

function createResampledJpeg($dstImgPath, $srcImgPath, $maxW, $maxH, $quality=80)
{
$srcProps = getimagesize($srcImgPath);
$srcAspect = $srcProps[0]/$srcProps[1];
$frmAspect = $maxW/$maxH;
$finalW = $maxW;
$finalH = $maxH;
if ($frmAspect >= $srcAspect) {
$finalH = $maxH;
$finalW = $maxH * $srcAspect;
}
else {
$finalW = $maxW;
$finalH = $maxW / $srcAspect;
}

$srcImg = imagecreatefromjpeg($srcImgPath);
$dstimg = imagecreatetruecolor($finalW,$finalH);
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $finalW, $finalH, $srcProps[0], $srcProps[1]);
imagejpeg($dstImg, $dstImgPath, $quality);
imagedestroy($srcImg);
imagedestroy($dstImg);
return array($finalW, $finalH, $dstImgPath);
}
[/size]


Code: [Select]
[size=x-small]
/*
array getThumbnail(source, thumb, name, maxW, maxH, [quality=80])

Returns a cached thumbnail of the source image, or generates a fresh one when there one is not available or has become stale (older than the source image file).

source:  destination path for soource image (the server folder!)
thumb:   source path for thumbnail image (the server folder!)
name:    source image name
maxW:    maximum width of destination image
maxH:    maximum height of destination image
quality: quality for jpeg encoding (default = 80)

returns: array of final width/height/path of thumbnail image
*/
function getThumbnail($imageFolder, $thumbFolder, $name, $defImg, $maxW, $maxH, $q=80)
{
$srcName = $imageFolder . $name;

// change this for fancier thumbnail names ;-)
$thmName = $thumbFolder . $name;

// Source image must exist - else return $defImage (a spacer)
if (!file_exists($srcName))
{
return array($maxW, $maxH, $defImg);
}

// Does thumbnail exist?
if (file_exists($thmName))
{
// Is source image older than the thumbnail?
if (filectime($srcName) <= filectime($thmName))
{
// return the existing thumbnail properties
$thmProps = GetImageSize($thmName);
return array($thmProps[0], $thmProps[1], $thmName);
}
}
// Create a new thumbnail and return it's properties
return createResampledJpeg($thmName, $srcName, $maxW, $maxH, $q);
}
[/size]


The above isnt totally robust but it relies on the fact that it is never fed bad parameters within its intended use.

It seems to achieve what I set out to do, but I will definately have to test it more closely ;-)
int p; // A