Re: New image already cached. (SOLVED)

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

 



Richard:

Thank you very much for your detailed explanation -- I finally got it.

However, the solution to my problem was much easier than I had hoped.

My problem was in creating an image on the fly and then using the same file name each time. As such, some browsers cache the image while others treat it as a new image.

So, the problem was how to change the name of the image such that all browsers would see it as a new image and not use a cached image..

The solution:

<img src="images/the_image.png?id=<?php rand(); ?>">

Neither the image tag nor the file cares if there is a random number attached to the file's url. But, by doing this, most (perhaps all) browsers think the image name is unique.

Doe anyone see any problems with this?

tedd

---- previously said ---


At 4:29 PM -0500 4/20/06, Richard Lynch wrote:
Here's the crucial info you are missing:

The URL does not have to *END* in the name of the PHP file.

The PHP file could be in the MIDDLE of the URL.

In other words, Apache is perfectly happy to take a url like this:
http://example.com/program.php/whatever_you-want=to+put_here

Example setup:

~/public_html/program
~/public_html/.htaccess

.htaccess:
<Files program>
  ForceType application/x-httpd-php
</Files>

program:
<?php
  //This stuff should really be in an include file...
  $pathinfo = $_SERVER['PATHINFO'];
  $parts = explode('/', $pathinfo);
  foreach($parts as $part){
    list($key, $value) = explode('=', $part);
    if (1) echo "DEBUG: Setting _PATH[$key] to $value<br />\n";
    $_PATH[$key] = $value;
  }

  //create your image, using:
  //$_PATH['width'] from the URL, max width of image
  //$_PATH['copyright'] from the URL, watermark with copyright or not
  //$_PATH['filename'] which image file to start with as 'source'
  $image = imagecreatefromjpeg($_PATH['filename']);
  //etc
?>

So when your URL is this:
http://example.com/program/width=200/copyright=1/filename=source.jpg

Your PHP script in 'program' is called, and Apache gives you all the
other stuff in the URL in 'PATHINFO' which you can use as your
arguments.

It's really just replacing:

?width=200&copyright=1&filename=source.jpg
and using $_GET with:

/width=200/copyright=1/filename=source.jpg
and using $_PATH (aka $_SERVER['PATHINFO'])

Variants:
To keep the browser from using 'filename=source.jpg' if the user
right-clicks, you could change the URL to just end in the filename,
and you'd have to handle that differently than the explode('=', $part)

To keep the browser from caching your image, embed some random number
in the URL, and your program will just IGNORE that key/value in $_PATH

The whole point of this exercise is to provide the browser with a URL
that it cannot distinguish from a valid static image URL, so the
browser can't screw up.  Cuz it will if it can.

On Thu, April 20, 2006 9:06 am, tedd wrote:
 Richard:

 I'm confused -- let's get back to the basic problem.

 I'm using an image tag (<img href="/path/to/an/image.png"> in my html.

 Some browsers are caching the image because it's the same url (same
 "image.png" name) each time. However, the image is something I
 generate each time it's called, so it's new.

 Now, you and examples have shown me that I could use .htaccess and
 ForceType to make the browser load anything regardless of it's name.

 You suggested using a random string and using that in the url to
 trick the browser into loading the image each time. OK, I can see how
 that could work, but I don't see how I can use .htaccess and
 ForceType to firce the browser to load the image without rewriting .
 htaccess each time.

 That's where I'm confused.

 tedd

 ---



$image_file = 'foo.jpg';
$random = mt_rand(1, 2000000000);
$url = "http://example.com/program/$random/$image_file";;
echo "<img src=\"$url\">

In program, if you echo out $_SERVER['PATHINFO'] you will see:
/454398574395/foo.jpg

The number will change, of course.
 >>
You can tear that apart and get the foo.jpg part to do whatever you
 want.

Or, you could have the URL sometimes have 'copyright' in it, and
sometimes not, and merge only when the URL has copyright in it.

You could even pass is paraeters like this:
http://example.com/program/width=200/2343242343/foo.jpg

Take the width=200 and scale the image to be 200 pixels wide.

Here is a real live example where I do this:
http://uncommonground.com/artist_profile/Ellen+Rosner

Right-click on that image of Ellen in the chair and check out that
 URL.

thumbnail is my PHP script.
target is a max width/height (I scale it proporationally and 200 is
max for both)
credits is the photographer credits from my database.
The rest is the URL of the actual real image I'm dinking with.

Feel free to play with the URL and change the 200 to, say, 100, or
make believe YOU took that photograph.

All just from the URL which, as far as the browser can tell, is a
perfectly valid set of directory paths on my machine, so the browser
can't possibly screw up.  (Rule #1: Never trust the browser.)

And in the main link, artist_profile is the PHP script, and if you
look at the links to the other artists at the bottom of the page,
you'll see that it also uses PATH_INFO to either show an artist by ID
or to search for their name in our database.

If multiple matches are found, the user is asked which one they
 meant:
http://uncommonground.com/artist_profile/Ellen

If I have the ID, then I know exactly which one to display.

I actually would suggest you go with:

/width=200/ though, instead of /width/200/ as it's easier to code and
have optional parameters using = and / as an arg separator.

I haven't bothered to fix that old script, but that's how I do it
nowadays.

On Wed, April 19, 2006 8:19 am, tedd wrote:
You're also better off embedding the parameters in the URL so that
 it
"looks like" a directory to the browser:

http://example.com/actual_script/57823642346963/copyright/whatever.png

The PHP scritp is actual_script.

You can use .htaccess and ForceType to make Apache run it.

$_SERVER['PATH_INFO'] will have all the parameters you need.

The embedded parameters pretty much give the browser NO opportunity
  to
screw up.



  Richard:

  > Far out ! -- I never saw .htaccess and ForceType used before. But
 it
  works slick in dropping the extension of "program.php" to
 "program".
  (Now if I can only get my editor to still treat the document as
 php,
  but that's a different matter).

  However, I don't see how I can generate a random number for a url
  each time an image is needed and have .htaccess and ForceType make
  Apache run it -- do I rewrite. htaccess each time, or what?

  Also, $_SERVER['PATH_INFO'] (or in my case
  $_SERVER['PATH_TRANSLATED'] ) gives me the path -- so how does
 that
  fit in?

  Please explain.

  Thanks.

  tedd

  --

--------------------------------------------------------------------------------
  > http://sperling.com



--
Like Music?
http://l-i-e.com/artists.htm

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


 --

--------------------------------------------------------------------------------
 http://sperling.com



--
Like Music?
http://l-i-e.com/artists.htm

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


--
--------------------------------------------------------------------------------
http://sperling.com

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