How to display an embedded image generated on the fly using PHP GD

I am trying to create an image on the fly by combining images using PHP GD. I want to know if there is a way to display an image on my web page without having to store it somewhere on the server.

Like, for example, I created the following code to merge images ...

function create_image() { $main_image = imagecreatefrompng("images/main.png"); $other_image = imagecreatefrompng("images/other.png"); imagecopy($main_image, $other_image, 114, 53, 0, 0, 49, 34); imagepng($main_image); imagedestroy($other_image); } 

Now my html code so far has been ...

 <div class="sidebar-avatar"> <img src="avatar_pattern.png" class="pattern1" width="430" height="100" /> </div> 

How can I call the php function so that it displays the image generated in the div that I designated for it.

Update . I found using Content-type: image/png , but that meant that I would need to display the image on a separate page, not on a line.

+4
source share
3 answers

Convert it to Base64 with base64_encode and repeat it as dataURI in the img tag!

Embedded images with data urls

+6
source

You can:

And use the code:

 <img src="display_image.php"> 

With the title and code you provided.

+4
source

<img src="image.php?other_image=(filename)">

and create your image in image.php, output using

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

You can also put the image creation part in the same script:

 if($other_image=$_GET['other_image']) { // create image ... // output image header('Content-Type:image/png'); imagepng($main_image); } else { // default behaviour ... echo '<img src="',basename(__FILE__),'?other_image=',urlencode('images/other.png'),'">'; ... } 

Watch out for injections!

0
source

Source: https://habr.com/ru/post/1336906/


All Articles