phpQuestioner Posted October 6, 2007 Share Posted October 6, 2007 How can I prevent my PHP GD code below from caching in browser? I guess that I would use a header; but I am not sure where to put it; it tried to put it before "imagejpeg($image)", but that did not help. The problem is that when I use a pop-up the image will display from cache and then be displayed again from PHP GD script. <?php $imagesource = $_GET['filename']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = @imagecreatefrompng($imagesource); if (!$image) die(); $watermark = @imagecreatefromgif('soldv2.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); // divide by "2" to center watermark $startheight = (($imageheight - $watermarkheight)/2); // divide by "2" to center watermark imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/ Share on other sites More sharing options...
trq Posted October 6, 2007 Share Posted October 6, 2007 Place this at the top of your script. <?php header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 01 Jul 2000 01:00:00 GMT"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363166 Share on other sites More sharing options...
phpQuestioner Posted October 6, 2007 Author Share Posted October 6, 2007 ok - i did it like this: <?php header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 01 Jul 2000 01:00:00 GMT"); ?> <?php $imagesource = $_GET['filename']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = @imagecreatefrompng($imagesource); if (!$image) die(); $watermark = @imagecreatefromgif('soldv2.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); // divide by "2" to center watermark $startheight = (($imageheight - $watermarkheight)/2); // divide by "2" to center watermark imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?> results = picture did not display at all and i did it like this: <?php header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 01 Jul 2000 01:00:00 GMT"); $imagesource = $_GET['filename']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = @imagecreatefrompng($imagesource); if (!$image) die(); $watermark = @imagecreatefromgif('soldv2.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); // divide by "2" to center watermark $startheight = (($imageheight - $watermarkheight)/2); // divide by "2" to center watermark imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?> results = still displayed picture twice so what to do?.............. Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363174 Share on other sites More sharing options...
phpQuestioner Posted October 6, 2007 Author Share Posted October 6, 2007 this problem only seems to be happening in FireFox; IE does not cache the image. Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363185 Share on other sites More sharing options...
heckenschutze Posted October 6, 2007 Share Posted October 6, 2007 I don't see where you send the header of the content type... that it's jpeg header("Content-type: image/jpeg"); Or something Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363188 Share on other sites More sharing options...
phpQuestioner Posted October 6, 2007 Author Share Posted October 6, 2007 your right i didn't, but now i did and it did not help either <?php header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header('Pragma: no-cache'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', FALSE); header("Content-Type: image/jpeg"); $imagesource = $_GET['filename']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = @imagecreatefrompng($imagesource); if (!$image) die(); $watermark = @imagecreatefromgif('soldv2.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); // divide by "2" to center watermark $startheight = (($imageheight - $watermarkheight)/2); // divide by "2" to center watermark imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?> Also Tried This (But It Did Not Work Either): <?php $imagesource = $_GET['filename']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = @imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = @imagecreatefrompng($imagesource); if (!$image) die(); $watermark = @imagecreatefromgif('soldv2.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); // divide by "2" to center watermark $startheight = (($imageheight - $watermarkheight)/2); // divide by "2" to center watermark imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); imagejpeg($image); header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header('Pragma: no-cache'); header('Cache-Control: no-store, no-cache, must-revalidate'); header('Cache-Control: post-check=0, pre-check=0', FALSE); header("Content-Type: image/jpeg"); imagedestroy($image); imagedestroy($watermark); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363194 Share on other sites More sharing options...
trq Posted October 6, 2007 Share Posted October 6, 2007 Start by remving all of the error supressors @ to see if your getting any errors. make sure error_reprting and display errors are on also. Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363210 Share on other sites More sharing options...
trq Posted October 6, 2007 Share Posted October 6, 2007 Your also sending the same header twice, that can't be good. Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363211 Share on other sites More sharing options...
phpQuestioner Posted October 6, 2007 Author Share Posted October 6, 2007 ok - i tried to do it like you said thorpe; but the image is still getting cached - not sure how to use error_reporting(). <?php $imagesource = $_GET['filename']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = imagecreatefrompng($imagesource); if (!$image) die(); $watermark = imagecreatefromgif('soldv2.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); // divide by "2" to center watermark $startheight = (($imageheight - $watermarkheight)/2); // divide by "2" to center watermark imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); imagejpeg($image); header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Content-Type: image/jpeg"); imagedestroy($image); imagedestroy($watermark); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363241 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 <?php $imagesource = $_GET['filename']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = imagecreatefrompng($imagesource); if (!$image) die(); $watermark = imagecreatefromgif('soldv2.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); // divide by "2" to center watermark $startheight = (($imageheight - $watermarkheight)/2); // divide by "2" to center watermark imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Content-Type: image/jpeg"); imagejpeg($image); imagedestroy($image); imagedestroy($watermark); ?> if this still caches then just add a random number on the URL ie image?rand=637126378 Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363245 Share on other sites More sharing options...
phpQuestioner Posted October 6, 2007 Author Share Posted October 6, 2007 MadTechie - I tried the code you provided; but it still loaded image twice in FF (once from cache and once from script). Next I tried your suggestion about the unique random number at the end of the jpg's src. Here Is Part of The Pop-Up Code <?php $borigin = rand(100000,999999); $epurl = $_GET['epurl']; // would be query string "mygdpic.php?filename=somephoto.JPG" - which was sent by javascript pop-up code from previous/main page. ?> <img src="<?php echo "$epurl?unique=$borigin"; ?>" width=500 height=400 galleryimg=no style="background:url(tngbsslm.gif) center center no-repeat" title="" alt="Image Failed To Load"> But that did not work either; I have no idea why it's doing this. Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363279 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 this should work.. <?php $borigin = rand(100000,999999); $epurl = $_GET['epurl']; // would be query string mygdpic.php?filename=somephoto.JPG ?> <img src="<?php echo "$epurl?unique=$borigin"; ?>" alt="Image Failed To Load"> <?php $imagesource = $_GET['filename']; $filetype = substr($imagesource,strlen($imagesource)-4,4); $filetype = strtolower($filetype); if($filetype == ".gif") $image = imagecreatefromgif($imagesource); if($filetype == ".jpg") $image = imagecreatefromjpeg($imagesource); if($filetype == ".png") $image = imagecreatefrompng($imagesource); if (!$image) die(); $watermark = imagecreatefromgif('soldv2.gif'); $imagewidth = imagesx($image); $imageheight = imagesy($image); $watermarkwidth = imagesx($watermark); $watermarkheight = imagesy($watermark); $startwidth = (($imagewidth - $watermarkwidth)/2); // divide by "2" to center watermark $startheight = (($imageheight - $watermarkheight)/2); // divide by "2" to center watermark imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); switch($filetype) { case ".gif": header("Content-Type: image/gif"); imagegif($image); break; case ".jpg": case ".jpeg": header("Content-Type: image/jpeg"); imagegif($image); break; case ".png": header("Content-Type: image/png"); imagepng($image); break; } imagedestroy($image); imagedestroy($watermark); ?> Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363283 Share on other sites More sharing options...
phpQuestioner Posted October 6, 2007 Author Share Posted October 6, 2007 MadTechie, I tried your gd code; but it did not help either - I'm still stumped. Quote Link to comment https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/#findComment-363578 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.