You are here: Re: [PHP] Generating images on the fly, linking via symlink? « PHP « IT news, forums, messages
Re: [PHP] Generating images on the fly, linking via symlink?

Posted by Rasmus Lerdorf on 09/04/05 00:13

Dan Trainor wrote:
> Dan Trainor wrote:
>
>> Hello, all -
>>
>> This is a question that could depend on a completely different (yet,
>> relayed) subject, so I'm sending this email to both php-general@ and
>> httpd@users.apache.org. I thank you in advance for your understanding.
>>
>> I am currently generating some images on the fly using some of PHP's
>> image generation and rendering functions. I'm having loads of
>> success, and like the results that I see.
>>
>> What I'd like this script to do is, to create symlinks to the
>> origional image, and then when the script is done running, the
>> symlinks are deleted. Basically trying to make it so that the
>> origional image is not known to the client or browser.
>>
>> So I'm taking, say, image1.jpg. I'm creating a symlink via:
>>
>> $linkname = md5(rand());
>>
>> or something similar. I'd then like to return $linkname to the client
>> or browser. Then, when the browser has completed rendering the page
>> to the client or browser, the symlink is then deleted.
>>
>> What I'm curious as to right now is if I do this, the client will see
>> the link to $linkname via HTML's "img src=" specification. What
>> happens if this is sent to the client or browser, and the symlink is
>> deleted immediately after the name is sent to the client or browser?
>> Would the web server (in this case, Apache) cache the image in memory
>> until the client has downloaded said image, and then delete it from
>> memory when the page is done rendering or being sent? Will PHP
>> totally disregard the web server's request to "hold" the image, and
>> render nothing to the browser? This is something I'm confused about.
>>
>> Thanks!
>> -dant
>>
>
> Hello -
>
> Don't suppose anyone has any pointers for me with this one, do ya?

I don't quite see how you know when the browser has fetched the image.
Browsers make requests for images in a separate connection, so on the
page where you generate this random symlink you are just adding an <img
src=... /> tag which will prompt the remote browser to at some point
open a separate connection and ask for that image.

I think a better idea may be to forget about symlinks and just keep a
local mapping table of random ids to images and in your page have
something like:

<img src="/view/123456789.jpg" />

Then make /view a PHP script. In your Apache config you can do that by
adding:

<Location "/view">
ForceType application/x-httpd-php
</Location>

And in your /view script look at $_SERVER['PATH_INFO'] which should be
/123456789.jpg. Pick out the image id, look up in your local lookup
table which actual image that refers to and serve it up with a
readfile() call. Remember to set the the appropriate
header('Content-type: image/jpeg") at the top.

You can also do a similar thing by using an ErrorDocument 404 /view.php
but the big downside of this is that Apache will log a 404 error on
every image view.

You do take a bit of a hit by going through php to serve up the image as
opposed to using your symlink approach where the actual request will go
directly via Apache's path_translate hook to the image you want. The
problem is that without any PHP involvement on the actual image request
you don't any way of knowing when the request has come in for the image
and thus you have no way to know when to delete the symlink. You could
of course set up a cron job that goes through and deletes all symlinks
older than 5 minutes or something like that, but that may not meet your
requirements.

-Rasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация