View previous topic :: View next topic |
Author |
Message |
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
Posted: Wed Jun 14, 2006 5:01 pm Post subject: Image Question |
|
|
Hi there.
I am requesting help for a very easy topic. I'm sure most of you know the answer to this question.
I'm new to PHP, but have good experience in Perl. I'm rewriting a Perl script in PHP.
Here's the Perl code: Code: | sub show_image {
my ($img_name) = @_;
open(FILE, "$img_name.gif");
binmode(FILE);
my $filesize = -s FILE;
read(FILE, my $image, $filesize);
close(FILE);
return $image;
} |
The Perl script sends an image header to the browser and the snippet above actually sends the image.
MY PROBLEM:
I want to send an image to the browser that already exists.
I know PHP can send dynamic images, but I just want it to send the image as is. I'm looking for the most concise code to accomplish this.
Thanks. _________________ Stephen
Need a LitlURL?
http://CodeBin.yi.org |
|
Back to top |
|
 |
AbyssUnderground -
Joined: 31 Dec 2004 Posts: 3855
|
Posted: Wed Jun 14, 2006 5:25 pm Post subject: |
|
|
Just make the filename echo to a <img src tag from a variable. Easy :-) _________________ Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk |
|
Back to top |
|
 |
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
Posted: Wed Jun 14, 2006 6:07 pm Post subject: Don't quite have it yet... |
|
|
Thanks for the quick response Andy!
I tried this:
Code: | <?php
header('Content-type: image/gif');
$handle = fopen("image.gif", "r");
print $handle;
fclose($handle);
?> |
Here's what results: "Resource id #2"
Not quite what I was hoping for... :) _________________ Stephen
Need a LitlURL?
http://CodeBin.yi.org |
|
Back to top |
|
 |
AbyssUnderground -
Joined: 31 Dec 2004 Posts: 3855
|
Posted: Wed Jun 14, 2006 6:08 pm Post subject: |
|
|
Code: | <?php
header('Content-type: image/gif');
$handle = fopen("image.gif", "r");
print "<img src='$handle'>";
fclose($handle);
?> |
Try that. _________________ Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk |
|
Back to top |
|
 |
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
|
Back to top |
|
 |
AbyssUnderground -
Joined: 31 Dec 2004 Posts: 3855
|
Posted: Wed Jun 14, 2006 6:18 pm Post subject: |
|
|
Cant connect to your localhost from here! _________________ Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk |
|
Back to top |
|
 |
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
Posted: Wed Jun 14, 2006 6:20 pm Post subject: |
|
|
Temporary URL:
URL no longer available _________________ Stephen
Need a LitlURL?
http://CodeBin.yi.org
Last edited by pkSML on Fri Feb 02, 2007 3:25 am; edited 1 time in total |
|
Back to top |
|
 |
AbyssUnderground -
Joined: 31 Dec 2004 Posts: 3855
|
Posted: Wed Jun 14, 2006 6:25 pm Post subject: |
|
|
It looks like "fopen()" isnt compatable in making it output the file contents in this way. _________________ Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk |
|
Back to top |
|
 |
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
Posted: Wed Jun 14, 2006 6:38 pm Post subject: Figured it out! |
|
|
Thanks for all your help Andy.
I searched php.net and found file_get_contents() to be "binary-safe".
I also read an informative article at http://www.onlamp.com/pub/a/php/2002/12/12/php_foundations.html
Here's the code that works!
Code: | <?php
header('Content-type: image/gif');
$handle = file_get_contents("image.gif");
print "$handle";
fclose($handle);
?> |
_________________ Stephen
Need a LitlURL?
http://CodeBin.yi.org |
|
Back to top |
|
 |
MonkeyNation -
Joined: 05 Feb 2005 Posts: 921 Location: Cardiff
|
Posted: Wed Jun 14, 2006 9:28 pm Post subject: |
|
|
The reason fopen() alone doesn't work is because all fopen does is create a handle.
You need to fread() or something in conjunction with fopen() to get it to display the contents of the stream. _________________
 |
|
Back to top |
 |
 |
cmxflash -
Joined: 11 Dec 2004 Posts: 872
|
Posted: Thu Jun 15, 2006 12:41 am Post subject: |
|
|
Code: | <?php
header("content-type: image/png");
readfile("yourimage.png");
?> |
Pretty easy. |
|
Back to top |
|
 |
pkSML -
Joined: 29 May 2006 Posts: 955 Location: Michigan, USA
|
|
Back to top |
|
 |
aprelium -
Joined: 22 Mar 2002 Posts: 6800
|
Posted: Thu Jun 15, 2006 11:09 am Post subject: Re: Figured it out! |
|
|
pkSML wrote: | Code: | <?php
header('Content-type: image/gif');
$handle = file_get_contents("image.gif");
print "$handle";
fclose($handle);
?> |
|
He is an reduced version of the above code:
Code: | <?php
header('Content-type: image/gif');
readfile("image.gif");
?> |
A slightly modified version which purpose is speeding HTTP requests when using a dumb client (or an old browser):
Code: | <?php
$filename = "image.gif";
header('Content-type: image/gif');
header('Content-length: ' . filesize($filename));
readfile($filename);
?> |
_________________ Support Team
Aprelium - http://www.aprelium.com |
|
Back to top |
|
 |
|