1

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 ).

6
  • using a return rather than an echo might be the first step. Commented Jun 7, 2013 at 11:23
  • 2
    You need to escape the " speech marks within the string you're echoing, e.g. echo "<img src=\"http.... Commented Jun 7, 2013 at 11:25
  • 1
    @sjmarshy: Or... not? That wouldn't work. Commented Jun 7, 2013 at 11:33
  • @Reshi: My answer works, I know, I'm using it. Commented Jun 7, 2013 at 11:35
  • @Pudge601 It might be easier to use ' for the string delimiter, and use concatenation (.) when necessary. I prefer the template approach, though, non-PHP code <?php echo htmlspecialchars($value); ?> non-PHP code as it preserves the markup for syntax highlighting and outline views. It can be written without problems as <?= htmlspecialchar($value); ?> from PHP 5.4 on. The PHP manual also recommends it. The safer htmlspecialchars() with the proper character encoding can be automated with a template engine or MVC approach. Commented Jun 7, 2013 at 11:45

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Don't personally see the advantage of the latter solution over what he's currently doing. In fact, that seems like a pretty bad idea in most situations
the problem is when i try this that /getimage does not display anything. It must display an image. the <img src=\" does not work no matter f i use base64 or not. It displays that there is supposed to be an image but no image is displayed (it displays only frame).
−1. There is absolutely no good reason to use the not well supported data: URI scheme here. If the application prints raw data to stdout, you can simply use the application URI as value of the src attribute. Calling the simpler, more compatible approach “not recommended” is utter nonsense.
the problem obviously is that im echoing <img ... doing echo "<img src='' etc />";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.