0

Basically I am in the process of creating a very simple one page .php template that I can easily use for multiple sites. I do not want to have to drag along extra files and such. I am aware of the imagecreate feature and know how to implement it. However, everything I have seen so far involves creating an img.php file and then calling for it on the page you want the image displayed. In my case, I want to eliminate the img.php page and some how just include the code directly on the spot where I want php to create the image.

For example:

<?php
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
  $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );

header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
?>

I would need to save this code into a file. Let's call it: myimpage.php. Then in order for php to create the image I would need to place this code on the page I want the image to be displayed:

<img src="myimpage.php" alt="Image created by a PHP script" width="200" height="80">

I might as well throw in real image files because this negates what I am trying to achieve. So my question is this: How can I use imagecreate without the need of creating a second php page to call from?

The reason why I need to have this code on the same page is because I want to pass some variables to it.

<h2><a href="./" title="<?php echo $title; ?>"><?php echo $title; ?></a></h2>
THIS IS HERE THE IMAGE GOES (THe IMAGE IS A BASIC BOX THAT DISPLAYS THE CONTENTS OF $IMAGE)

Basically, I want the image to display the contents of $title. So I can't do that if the code is being pulled from another page.

miken32
  • 42,008
  • 16
  • 111
  • 154
Garry
  • 251
  • 2
  • 13

2 Answers2

1

You can use the Data URI scheme to create the image in a page and embed it inline in the same page:

<img src="data:image/png;base64,*base_64_data_from_imagecreate*" alt="Image">

To capture the output, you would have to do:

ob_start();
imagepng($my_img);
$imagedata = ob_get_contents();
ob_end_clean();

$imagedata = base64_encode($imagedata);

echo '<img src="data:image/png;base64,' . $imagedata . '" alt="Image" />';

Hope that's what your looking for and helps.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • That is close but doesn't quite work. When I right click on the image to view it in a new tab it displays an image of text saying, The Image"data:image/png;Base64,asdklfjasdl;fkjasl;kfjas;kdfjask etc... etc.. etc... cannot be displayed because it contains errors. – Garry Jul 30 '12 at 22:46
  • Yeah that won't work because the image doesn't exist anywhere but in the page itself as an embedded piece of data. I think you're probably stuck having to use the external script. – drew010 Jul 30 '12 at 22:59
  • I think so too... I dug around a little more on the site to see if others asked similar and that seems to be the case. That's totally fine, I can deal with that. What I am trying to figure out now is how to edit the text that says: thesitewizard.com all on one line and edit it to say three words but each word is centered and on its own line. – Garry Jul 30 '12 at 23:07
0

Please keep in mind that although you can use Data URI scheme to save image data in your html this is not necessarily a clever idea because browser will not be able to cache the picture but have to download it again every time the site is called.

Browsers usually download an image once and later only send a "small" request to the server checking whether their image cache is still up-to-date. So if your image is smaller than the http overhead of such a request it will be fine. Otherwise i would not recommend it.

Fabian
  • 11
  • 3