Solved

Displaying an image by reading the file and parsing byte code?

Posted on 2006-07-23
9
271 Views
Last Modified: 2008-02-07
Hi, the way I wrote the question probably is a dead give away that I dont know what I'm talking about!

Background: security on the webserver is set up so that images in a particular folder cannot be browsed and displayed in a webbrowser unless they have a username and password on the folder, and this is the way it needs to stay.

What I need to do is this:  display the image file by some other means.  The webserver has access to the file by name.  I believe you can read the image file and display it somehow.  Can someone please help me with the code for this?

Thanks heaps,
Heather.
0
Comment
Question by:hmaloney
  • 5
  • 3
9 Comments
 
LVL 3

Expert Comment

by:gileze33
ID: 17163017
You can find out the exact path of the server to the folder, I'll call it $path.

Then create a file such as images.php, then when you want say image.jpg, call images.php?img=image.jpg.

SOURCE OF images.php:

<?php

$img = $_GET['img'];
$path = "\path to images folder leaving in trailing slash\";

$instr = fopen($path.$img,"rb");
$bytes = fread($instr,filesize($path.$img));

header("Content-type: image/jpeg");
echo $bytes;

?>

Hope this helps :)

Gileze33.
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 17163406
or use imagejpeg()

http://uk2.php.net/imagejpeg
0
 
LVL 3

Expert Comment

by:gileze33
ID: 17163416
Yes but however, the imagejpeg() function needs GD to be installed.

Gileze33.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:hmaloney
ID: 17164252
Thanks heaps gileze.

One thing I didn't mention is that I am displaying the image amongst lots of other text, so I don't think the headers line will work for me.

I've tried just printing out the bytes without that, however, and it wont work.  Can you please help?

Thanks again.
0
 
LVL 3

Expert Comment

by:gileze33
ID: 17166643
Yes thats understandable.

You need to treat the images.php file as though it were an image - so simply use the HTML <img> tag to load in the image to a separate HTML page.

E.g.

<html>
<body>
  Hi. Below is an image!
  <br>
  <img src="images.php?img=whatever.jpg" alt="wow - an image with a .php extension!">\
</body>
</html>

Sorry about the cheesy example :)

Gileze33.
0
 

Author Comment

by:hmaloney
ID: 17166831
Sorry, how does that work with the other code you gave me?
0
 
LVL 3

Accepted Solution

by:
gileze33 earned 250 total points
ID: 17166875
Ok.

Make images.php with the source at the top, replacing the absolute path with one to your images folder.

Next, create file.html, with the source I put above (making sure that you change the source for the image to be one that actually exists).

Hope this helps :)

Gileze33.
0
 

Author Comment

by:hmaloney
ID: 17167016
Ah!  You're a star.  I finally understood what you meant, and it worked like a gem.  Thank you!
0
 
LVL 3

Expert Comment

by:gileze33
ID: 17167263
No problem, glad to be of help.

Gileze33.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question