note 65141 deleted from function.header by bjori

From: Date: Tue, 25 Apr 2006 08:12:51 +0000
Subject: note 65141 deleted from function.header by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-109642@lists.php.net to get a copy of this message
Note Submitter: bradm at ecps dot com dot au ---- have not noticed or have missed one little simple trick i sort come across when looking at CGI. basiclly a image serving script. in your normal <img> tag you would put the src="" as src="getimage.php?varImg=banner1" ------then in your getimage.php you would have --------- <?php $varImageType = substr($_REQUEST['varImg'], 0, 1); $varImageName = substr($_REQUEST['varImg'], 1, strlen($_REQUEST['varImg'])); if ($varImageType=="1") { $varFile = "http://www.imageplus.net.au/images/" . $varImageName . ".jpg"; } elseif ($varImageType=="2") { $varFile = "http://www.imageplus.net.au/images/" . $varImageName . ".gif"; } elseif ($varImageType=="3") { $varFile = "http://www.imageplus.net.au/images/" . $varImageName . ".png"; } else { // GERATE A IMAGE FOR NOT FOUND USING GD } $headers = apache_request_headers(); if (isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == filemtime($varFile))) { header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($varFile)).' GMT', true, 304); } else { // Image not cached or cache outdated, we respond '200 OK' and output the image. header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($varFile)).' GMT', true, 200); header('Content-Length: '.filesize($varFile)); if ($varImageType=="1") { header('Content-Type: image/jpeg'); } elseif ($varImageType=="2") { header('Content-Type: image/gif'); } elseif ($varImageType=="3") { header('Content-Type: image/png'); } else { } print file_get_contents($varFile); } ?> --------------------------------------------------------- Note that this above script is servers with a little more purpose, not written for speed. just for easy to read. in the src="" you'll notice the number 1 at the front of banner with the varImg= variable. providing you have the images folder set or you send the relative path for the image along with the image name in the src="" then this should work fine. The lower section of the code was from somewhere else here on php.net it just makes sure if the image has been shown if it is cached. so loading time is quicker. but all of this can be controlled with extra variables for the single function file. This way you dont have to worry about headers and inserting code everywhere for a simple image. You can proccess anything with the image and then show it with this method.. for another two lines of code you can make sure it shows images only for your domain/domains.... so on and so forth. soon to be up and running properly, there will be such scripts n idea's avaliable at http://www.easyasweb.com gotta love these post here ;)

« previous php.notes (#109642) next »