Jump to content

[SOLVED] How Would I Prevent PHP GD Cache?


phpQuestioner

Recommended Posts

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);

?>

Link to comment
https://forums.phpfreaks.com/topic/72071-solved-how-would-i-prevent-php-gd-cache/
Share on other sites

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?..............

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);

?>

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);

?>

<?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

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.

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);

?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.