Image Question

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
pkSML
-


Joined: 29 May 2006
Posts: 955
Location: Michigan, USA

PostPosted: Wed Jun 14, 2006 5:01 pm    Post subject: Image Question Reply with quote

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 View user's profile Send private message Visit poster's website
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Wed Jun 14, 2006 5:25 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
pkSML
-


Joined: 29 May 2006
Posts: 955
Location: Michigan, USA

PostPosted: Wed Jun 14, 2006 6:07 pm    Post subject: Don't quite have it yet... Reply with quote

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 View user's profile Send private message Visit poster's website
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Wed Jun 14, 2006 6:08 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
pkSML
-


Joined: 29 May 2006
Posts: 955
Location: Michigan, USA

PostPosted: Wed Jun 14, 2006 6:11 pm    Post subject: Reply with quote

You respond so fast, it's almost creepy!

It generates a 404 error. Here's what it's looking for:
http://localhost/php/printgif/Resource%20id%20#2

It's supposed to be looking for image.gif.

Thanks in advance.
_________________
Stephen
Need a LitlURL?


http://CodeBin.yi.org
Back to top View user's profile Send private message Visit poster's website
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Wed Jun 14, 2006 6:18 pm    Post subject: Reply with quote

Cant connect to your localhost from here!
_________________
Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk
Back to top View user's profile Send private message Visit poster's website
pkSML
-


Joined: 29 May 2006
Posts: 955
Location: Michigan, USA

PostPosted: Wed Jun 14, 2006 6:20 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Wed Jun 14, 2006 6:25 pm    Post subject: Reply with quote

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 View user's profile Send private message Visit poster's website
pkSML
-


Joined: 29 May 2006
Posts: 955
Location: Michigan, USA

PostPosted: Wed Jun 14, 2006 6:38 pm    Post subject: Figured it out! Reply with quote

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 View user's profile Send private message Visit poster's website
MonkeyNation
-


Joined: 05 Feb 2005
Posts: 921
Location: Cardiff

PostPosted: Wed Jun 14, 2006 9:28 pm    Post subject: Reply with quote

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 View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number
cmxflash
-


Joined: 11 Dec 2004
Posts: 872

PostPosted: Thu Jun 15, 2006 12:41 am    Post subject: Reply with quote

Code:
<?php
header("content-type: image/png");
readfile("yourimage.png");
?>


Pretty easy.
Back to top View user's profile Send private message
pkSML
-


Joined: 29 May 2006
Posts: 955
Location: Michigan, USA

PostPosted: Thu Jun 15, 2006 2:25 am    Post subject: Reply with quote

Thanks cmxflash!

That really helps. I was hoping there was a way to print images in PHP that was that simple.

I used it in a script that requests the US Government's Homeland Security status. It displays an image that corresponds with the level.

You can check it out at my homepage (http://stephen.calvarybucyrus.org) or access it directly at http://stephen.calvarybucyrus.org/hls/hls.php?image.

If anybody wants the source, just email me!
_________________
Stephen
Need a LitlURL?


http://CodeBin.yi.org
Back to top View user's profile Send private message Visit poster's website
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Thu Jun 15, 2006 11:09 am    Post subject: Re: Figured it out! Reply with quote

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 View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group