Re: Dynamically Rename Images
| From: | Andrew Hucks | Date: | Sun, 03 May 2009 21:29:08 +0000 |
| Subject: | Re: Dynamically Rename Images | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-292237@lists.php.net to get a copy of this message | ||
Thanks, but I solved the problem another way. http://codepad.org/6juIkECZ.
On Sun, May 3, 2009 at 3:02 PM, Michael A. Peters <mpeters@mac.com> wrote:
> Andrew Hucks wrote:
>>
>> Got this error:
>>
>> Fatal error: Cannot instantiate non-existent class: finfo in <place> on
>> line 6
>
> You need the pecl-FileInfo module.
> You can just comment that out that line and the line after it and (assuming
> all your images are png) change
>
> $mime_type = $fi->buffer(file_get_contents($impath));
>
> to
>
> $mime_type = 'image/png';
>
>>
>> On Sun, May 3, 2009 at 12:58 AM, Michael A. Peters <mpeters@mac.com>
>> wrote:
>>>
>>> Andrew Hucks wrote:
>>>>
>>>> Is it possible to rename images dynamically?
>>>>
>>>> Say that I had something like image1.png, and I don't want to rename
>>>> it on the server. I'm working on an image rotater for a forum that
>>>> doesn't allow anything but image files as signatures.
>>>>
>>>> Here's my code so far:
>>>>
>>>> <?php
>>>>
>>>> //LolRotator
>>>>
>>>> //add images. not too hard.
>>>> $images = array("image1.png", "image2.png",
>>>> "image3.png");
>>>> //which one do we gets?
>>>> $show = rand(0, (count($images)-1));
>>>> //i r got u picture.
>>>> echo '<img src="'.$images[$show].'"/>';
>>>>
>>>> ?>
>>>>
>>>> I used mod_rewrite, which makes it from image.php to image.png. But,
>>>> because the files aren't named image.png, it doesn't work. I need to
>>>> figure out somethign between lines 8 and 10 to change the file name to
>>>> image.png.
>>>
>>> Use mod_rewrite so that request for image.png is handled by a script,
>>> image.php
>>>
>>> image.php then randomly picks a image file from list, sends the
>>> appropriate
>>> image header, and reads the file sending the content to the browser. IE -
>>>
>>> function sendimage($impath) {
>>> // requires fedora rpm : php-pecl-Fileinfo
>>> $fi = new finfo(FILEINFO_MIME);
>>> $mime_type = $fi->buffer(file_get_contents($impath));
>>> $imageOutput = "";
>>> if ($fp = fopen( $impath , 'rb' )) {
>>> while ($l = fgets($fp)) {
>>> $imageOutput .= $l;
>>> }
>>> $outputLen = strlen($imageOutput);
>>> header("Content-Length: $outputLen");
>>> header("Content-type: $mime_type");
>>> print $imageOutput;
>>> } else {
>>> // for whatever reason we failed
>>> die();
>>> }
>>> }
>>>
>>>
>>>
>>>> Is there a way to do this?
>>>>
>>>
>>
>
>