Re: More efficient thumbnail script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



BTW: always reply to the list unless asked otherwise.

Jason Pruim wrote:
> 

you don't know what a class is or how to use one do you?
(correct me if I'm wrong)

the idea of my particular class is that it is used in
the *script* that serves all your resampled images.

something *like* (it an overly simplified example):

image.php (called as /image.php?id=100&width=200&height=160
============= 8< =======================
<?php

$ic = new ImageCache('/path/to/your/cached/images');
$ic->genCacheFileName(array('id', 'width', 'height'), 'jpg', 'foo');

$files   = array();
$files[] = '/path/to/your/src/image/that/corresponds/to/id/100.jpg';

if (!$ic->checkCache()) {
	/* generate a resampled version of
         * the relevant source file
         * and save the generated image to the location
	 * that is returned by $ic->getCacheFilePath()
	 */
}

// this code won't even run if ImageCache determines that
// the browser already has an up2date copy of the
// resampled image locally
$ic->showImage();
exit;

>>
>> <?php
>>
>> class ImageCache
>> {
>>     var $validTypes = array('png','gif','jpeg');
>>
>>     var $cacheFileName;
>>     var $cacheFileType;
>>     var $cacheDir;
>>
>>     var $im;
>>
> 
> Would I change $cDir to a real directory or $this->cacheDir to a real
> directory?
> 
>>     /* you must give a valid 'cache' dir  */
>>     function ImageCache($cDir)
>>     {
>>         $this->cacheDir         = $cDir;
>>     }
>>
>>     /* generate a cache file name for the image your are generating
>>      * if your generated output image is dependent on the values of
>> one or more request
>>      * variables then you should add the names to the $args array e.g.
>>      *
>>      * your script take width/height parameter:
>> /image.php?width=200&height=160
>>      *
>>      * $args should be the following array: array('width', 'height');
>>      */
>>     function genCacheFileName($args = array(), $type = '', $prefix = '')
>>     {
>>         /* name/val pair delimiter in the string that results in the
>> hash for the cache id */
>>         $qHashMark = '%~^*';
>>
>>         $qry  = array();
>>         $args = (array)$args; natsort($args);
>>         foreach ($args as $arg) {
>>             if (($val = $this->getR( $arg, false )) !== false) {
>>                 $qry[] = "{$arg}=".str_replace($qHashMark,'',$val);
>>             }
>>         }
>>
>>         $sep  = '-:-';
>>         $hash = md5($_SERVER['HTTP_HOST'] .$sep.
>> $_SERVER['SCRIPT_NAME'] .$sep. join($qHashMark,$qry));
>>
>>         if (!in_array($type, $this->validTypes)) {
>>             if ($type == 'jpg') {
>>                 $type = 'jpeg';
>>             } else {
>>                 $type = 'png';
>>             }
>>         }
>>
>>         $this->cacheFileType = $type;
>>
>>         if (!$prefix) {
>>             $prefix = 'cacheimg';
>>         }
>>
>>         return ($this->cacheFileName = "{$prefix}_{$hash}.{$type}");
>>     }
>>
>>     /* get the fullpath to the location where the cache file is
>> saved/stored */
>>     function getCacheFilePath()
>>     {
>>         return $this->cacheDir . '/' . $this->cacheFileName;
>>     }
>>
>>     /* Return true if the cache file is younger than the source file(s),
>>      * false otherwise.
>>      *
>>      * if this func returns true you can output the relevant cache file,
>>      * if false is returned it's your responsibility to generate the
>> output file
>>      * and save it to the location given by $this->getCacheFilePath()
>>      *
>>      * the (array of) files passed to this function should be complete
>> paths,
>>      * not just filesnames.
>>      */
>>     function checkCache( $files = array() )
>>     {
>>         $cacheState = true;
>>
>>         $cf           = $this->getCacheFilePath();
>>         $mTime        = is_readable($cf) ? filemtime($cf): 0;
>>         $lastModified = gmdate("D, d M Y H:i:s ", $mTime)."GMT";
>>         $files        = (array) $files;
>>
>>         if (!count($files) || !$mTime) {
>>             $cacheState = false;
>>         } else {
>>             foreach($files as $file) {
>>                 if ($mTime < filemtime( $file )) {
>>                     $cacheState = false;
>>                     break;
>>                 }
>>             }
>>         }
>>
>>         if ($cacheState) {
>>             $headers = getallheaders();
>>             if (isset($headers['If-Modified-Since']) &&
>> ($headers['If-Modified-Since'] == $lastModified)) {
>>                 /* The UA has the exact same image we have. */
>>                 header("HTTP/1.1 304 Not Modified");
>>                 exit;
>>             } else {
>>                 unset($headers);
>>                 header("Last-Modified: ".$lastModified);
>>                 return true;
>>             }
>>         } else {
>>             // not cached - or cache invalidated
>>             // must cache the (new) data.
>>             return false;
>>         }
>>     }
>>
>>     function showImage($type = '', $quality = 100)
>>     {
>>         header( "Content-type: image/{$this->cacheFileType}" );
>>         readfile( $this->getCacheFilePath() );
>>         exit;
>>     }
>>
>>     function getR($v = '', $r = null, $t = null)
>>     {
>>         if (!empty($v)) { if (isset($_REQUEST[$v]))
>> {$r=!is_null($t)?$t:$_REQUEST[$v];} }
>>         return $r;
>>     }
>> }
>>
>> ?>
>>
>>>
>>> Would upgrading to PHP 5* be of any help?
>>
>> not specifically in this case, but I would recommended using php5
>> if your starting out and developing new stuff.
>>
>> php4 is not being actively developed (security & bug fixes still occur).
>>
>> it's not wrong to use php4, and many people have no choice, but I
>> recommend
>> trying to stay on top of whats current and given that your not
>> (AFAICT) running
>> any legacy code there is nothing really stopping you.
> 
> Okay, I'll look deeper into upgrading before I go much farther with it
> since I want to learn how to do it right from the beginning :)
>>
>>> I've thought about upgrading
>>> but not knowing much about how to run the server from the command
>>> line(Part of what I'm learning) I didn't want to screw anything up.
>>>
>>>
>>
>> test servers are there to be screwed up, kind of. when your learning
>> and trying things
>> out thing break occasionally. ommelettes, breaking eggs, all that jazz.
>>
>>
> 
> 
> Test servers are a luxury I don't currently have, just a low use
> web/email/file server in my office. I try not to screw it up to much,
> only killed it 3 or 4 times recently.
> 
> 
> -- 
> Jason Pruim
> japruim@xxxxxxxxxx
> Production & Technology Manager
> MQC Specialist (2005 certified)
> 3251 132nd Ave
> Holland MI 49424
> 616.399.2355
> www.raoset.com
> 
> 
> "America will never be destroyed from the outside. If we falter and lose
> our freedoms, it will be because we destroyed ourselves."
>  -Abraham Lincoln
> 
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux