Reshi
Reshi

Reputation: 799

PHP: Obtaining image from other PHP script

I'm having kind of controller http://samplesite.com/application/getimage written in php. It is a simple script that responds to GET with some parameters. According to these parameters I find an image in database (image is stored there as a BLOB) and I return it as follows:

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

$image is a BLOB of image obtained from database. When I open this URL http://samplesite.com/application/getimage/?id=200 my web browser displays an image.

Now I want to display this image inside a table in another php script. I want to perform it like

echo "<img src="http://mysite.com/application/getimage/?id=200" ...some params here... />";

but this approach does not work. It displays no image or makes other problems.

What approach should I choose to make this work? I'm new to php normally work with Java EE. Thanks very much.

EDIT: Of course there should be inside the echo \" and not only ". But this approach that I wrote here works fine. Problem was in syntax, I had one extra ).

Upvotes: 1

Views: 124

Answers (1)

Glitch Desire
Glitch Desire

Reputation: 15023

Passing a PHP file AS an image (not recommended)

Your issue is that you haven't escaped speech marks, replace:

echo "<img src="http://mysite.com/application/getimage/?id=200" ...some params here... />";

With:

echo "<img src='http://mysite.com/application/getimage/?id=200' ...some params here... />";

or:

echo "<img src=\"http://mysite.com/application/getimage/?id=200\" ...some params here... />";

However, there is a far better way to do this:

Using image BLOBs properly

You're on the right track -- you can use an image BLOB instead of a file to put an image inline, but there's a little more work to it. First, you need to base64_encode the blob before you pass it. You should omit the header as you're passing text, not an image:

echo base64_encode($image);

Next, you need to set up the image tag like this, declaring that you are using data, the mime type and finally including the output of your getimage script as declared by RFC2557:

<img src="data:image/jpeg;base64,<?php include('http://mysite.com/application/getimage/?id=200'); ?>"/>

That should fix the issue.

Upvotes: 3

Related Questions