How to use the image generated in html on another page

Asked

Viewed 30 times

0

I’m converting a string to image to be used in a bank account. In view of this need, I have as an example the following variable:

$string = 10499.10657 01500.110045 00002.710038 9 79320000000000

Obs.: these of the variable are in UTF-8 string

Code:

$font  = 5;
$width  = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);

$image = imagecreate($width, $height);

$bg = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 65, 65, 65);
imagestring($image, 5, 0, 0, $string, $textcolor);
header("Content-type: image/png");
imagepng($image);

And returns me an image in the tab with the value:

inserir a descrição da imagem aqui

In another file, I have an html template that I need to insert, but how do I perform this passage via variable in the tag <img src="<?php ?>"> to just show where I want, not running on this tab as is occurring?

I read in some topics in English that it is necessary to convert to Base64 according to my understanding, in fact it is necessary? Reference: link

1 answer

1


you can do the following

<img src='arquivo_de_boleto.php'>

or you can turn your image into a base64

$font  = 5;
$width  = imagefontwidth($font) * strlen($string);
$height = imagefontheight($font);

$image = imagecreate($width, $height);

$bg = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 65, 65, 65);
imagestring($image, 5, 0, 0, $string, $textcolor); 
ob_start();  
imagepng($image);
$imagedata = ob_get_clean();

and at the bottom page in php vc printaria so

<?php echo '<img src="data:image/png;base64,'.base64_encode($imagedata).'">';

--- To modify the source ----

it would be something like that

$font = imageloadfont('./fonte.ttf');
imagestring($image, 5, 0, 0, $string, $textcolor);

gives a read on the doc. https://www.php.net/manual/en/function.imagestring.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.