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

Reshi
  • 799
  • 4
  • 15
  • 32
  • using a return rather than an echo might be the first step. – sjmarshy Jun 07 '13 at 11:23
  • 2
    You need to escape the `"` speech marks within the string you're echoing, e.g. `echo " – Pudge601 Jun 07 '13 at 11:25
  • 1
    @sjmarshy: Or... not? That wouldn't work. – Madara's Ghost Jun 07 '13 at 11:33
  • @Reshi: My answer works, I know, I'm using it. – Glitch Desire Jun 07 '13 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 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](http://php.net/manual/en/) also recommends it. The safer [`htmlspecialchars()`](http://php.net/htmlspecialchars) with the proper character encoding can be automated with a template engine or MVC approach. – PointedEars Jun 07 '13 at 11:45
  • @LightningDust See [Which browsers support data URIs and since which version?](http://stackoverflow.com/a/1766942/855543) and [What is the maximum length of a URL in different browsers?](http://stackoverflow.com/a/417184/855543) as to why it does not. – PointedEars Jun 07 '13 at 12:13

1 Answers1

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.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
  • 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 – Pudge601 Jun 07 '13 at 11:41
  • the problem is when i try this that /getimage does not display anything. It must display an image. the – Reshi Jun 07 '13 at 11:46
  • 1
    −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*. – PointedEars Jun 07 '13 at 11:46
  • the problem obviously is that im echoing "; – Reshi Jun 07 '13 at 11:56
  • See [Which browsers support data URIs and since which version?](http://stackoverflow.com/a/1766942/855543) and [What is the maximum length of a URL in different browsers?](http://stackoverflow.com/a/417184/855543). – PointedEars Jun 07 '13 at 12:16