简体   繁体   中英

Trying to display an echoed image from a PHP script in and `<img src=“…”` tag

The objective:

1) I have a images folder outside web root where all images from the web app are placed (each image path is saved in a DB table);

2) I want to get that image upon a request from the user.

Simple, right?

I did:

a) Create a class to reads the image (with all security features and safeguards that I can remember). It returns (successfully) the image encoded (base64) with the respective mime type.

b) I've created, for demonstration purposes, a index.php, which has:

<img src="agivenscript.php?img=name.jpeg">

My only objective here is to call from any other place in my web app a script that outputs the base64 encoded image with the respective mime type.

c) Created a script "agivenscript.php" that receives the image name by GET, instantiates my class and gets, when all goes right, the base64 encoded image. It then echoes the image.

What happens:

  • When I do the output - using echo '<img src="'. $output64encodedwithmimetype. '">'; - in agivenscript.php it works like a charm;
  • Moreover, if I take the $output64encodedwithmimetype contents and place the string directly in the src tag of index.php, it also works.
  • However, it does not work when I try <img src="agivenscript.php?img=name.jpeg"> .

As the base64 encoded image is obviously fine (and has the mime type), what am I missing? Any idea?

Thank in advance.

data: scheme URLs can be Base64 encoded.

HTTP responses of the image/jpeg (or png or whatever) can't . You need to serve up the actual image data without encoding it as text.

readfile is probably all you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM