-1

is it possible to Warp an PHP Image in a HTML Markup and send it to Client side

<?php
 $a = "A";
 $ima = imagecreate(100, 30);
 $bg = imagecolorallocate($ima, 255, 255, 255);
 $textcolor = imagecolorallocate($ima, 0, 0, 255);
 imagestring($ima, 5, 0, 0, '$a, $textcolor);
 imagepng($ima);
 imagedestroy($ima);

something like

echo '<img src='+ $ima+' alt=' ' />';

Update

<?php
$a = "A";
$b = "B";
$c = "C";
$n = "8";

$ima = imagecreate(100, 30);
$bg = imagecolorallocate($ima, 255, 255, 255);
$textcolor = imagecolorallocate($ima, 0, 0, 255);
imagestring($ima, 5, 0, 0, '$a, $textcolor);
header('Content-type: image/png');
imagepng($ima);
imagedestroy($ima);

$imb = imagecreate(100, 30);
$bg = imagecolorallocate($imb, 255, 255, 255);
$textcolor = imagecolorallocate($imb, 0, 0, 255);
imagestring($imb, 5, 0, 0, '$a, $textcolor);
header('Content-type: image/png');
imagepng($imb);
imagedestroy($imb);

$imc = imagecreate(100, 30);
$bg = imagecolorallocate($imc, 255, 255, 255);
$textcolor = imagecolorallocate($ima, 0, 0, 255);
imagestring($imc, 5, 0, 0, '$a, $textcolor);
header('Content-type: image/png');
imagepng($imc);
imagedestroy($imc);

$imn = imagecreate(100, 30);
$bg = imagecolorallocate($imn, 255, 255, 255);
$textcolor = imagecolorallocate($ima, 0, 0, 255);
imagestring($imn, 5, 0, 0, '$a, $textcolor);
header('Content-type: image/png');
imagepng($imn);
imagedestroy($imn);

echo '<img src="script.php?id=1" />';
echo '<img src="script.php?id=2" />';
echo '<img src="script.php?id=3" />';
echo '<img src="script.php?id=4" />';
?>
Behseini
  • 6,066
  • 23
  • 78
  • 125

2 Answers2

1

It is. What you need to do is reference your PHP script in the image tag.

<img src="yourPhpScript.php" />

Make sure that in your PHP script, you are outputting the correct Content-Type header.

header('Content-Type: image/png');

Alternatively, you could base64-encode the image data, but this is not recommended as it leads to a 33% increase in download size.

Also, you have a stray quote mark ' on your third from last line.

Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
  • @Behseini Echo out four image tags. ` etc.` – Brad Oct 05 '15 at 19:07
  • Instead of echo, I believe you could make the script die with the output, sort of return outside of a function – Wargog Oct 05 '15 at 19:09
  • @Behseini Your update still doesn't stop you from having four image tags. – Brad Oct 05 '15 at 19:09
  • @Behseini You want four images on a page, right? Create four image tags. Each image tack is going to reference a separate PHP script that outputs an image. That image appears, just like any other image. If that isn't making sense then elaborate on what your question is. – Brad Oct 05 '15 at 19:19
  • Thanks again but can you take a look at the end of update? `echo '';echo '';echo '';echo '';` but this is just exporting first image! – Behseini Oct 05 '15 at 21:41
  • @Behseini Well of course it is, if that's what's in your script. Your script should take input from the query string and generate the image accordingly. – Brad Oct 05 '15 at 23:00
  • well this exactly my question! how Can I do that? – Behseini Oct 06 '15 at 00:04
  • @Behseini Your question says you want to use a dynamically generated image in HTML. I have shown you how to do that. If you want to read from the query string, just use `$_GET`. `imagestring($ima, 5, 0, 0, $_GET['text'], $textcolor);` Then call your script with `something.php?text=Your%20Text%20Here`. – Brad Oct 06 '15 at 01:14
0

You could use output buffer capturing.

Like

 $a = "A";
 $ima = imagecreate(100, 30);
 $bg = imagecolorallocate($ima, 255, 255, 255);
 $textcolor = imagecolorallocate($ima, 0, 0, 255);
 imagestring($ima, 5, 0, 0, $a, $textcolor);
 ob_start();
 imagepng($ima);
 $obContents = ob_get_contents();
 ob_end_clean();
 imagedestroy($ima);
 echo('<img src="data:image/png;base64,' . base64_encode($obContents) . '" />');
  • This method is not ideal, as it forces you to flush any other output buffering and base64 encoding has about a 33% overhead. – Brad Oct 05 '15 at 19:23
  • Of course, but this is the preferred method to generate image and HTML code from the same PHP file without writing to disk. And you can also use gzip compression. Another would be to have a `switch` statement and two different content-types in the same file, but it might not be preferable in case of sequential image rendering requirement. – Victor - Reinstate Monica Oct 05 '15 at 19:24
  • Or... shared memory, Redis, etc. The best option here is to pass in a parameter for what that text should say since that's the only thing that's changing. I only point out that this method isn't ideal so that folks don't assume it's the only way to do it. Every time I explain to people how to do this, they immediately latch on to the idea that base64 encoding is the way to go because they're using to handling all their data in a single script and have difficulty thinking more broadly. They need to understand that there are tradeoffs for this method. – Brad Oct 05 '15 at 19:26
  • True that, I did augment my comment. – Victor - Reinstate Monica Oct 05 '15 at 19:28