0
Good afternoon Devs o/
I have a question, I need to insert an image to <canvas>
. But that’s not all, I’m putting together a blog creation system for Instagram, so I have several templates in the database, I need to put it inside the <canvas>
and so far I’ve put the photo of the template, user, name of the same and position. Getting this result:
We can see that everything is going well, but the real problem is in selecting the employee that the user wants to do the post. At the time of print the selected name is Bruna, but no photo was loaded. And I say that the problem is not in PHP.
I did not put the connection information with the bank and table, because I am solving the problem in production
header("Content-type: image/gif");
if($row=mysqli_fetch_object($result)) {
echo "
var canvas = document.getElementById('canvas');
context = canvas.getContext('2d');
img4 = new Image(115,115)
img4.src = 'data:image/png;base64,'
img4.onload = function(){
context.drawImage(img4, 201, 128, img4.width, img4.height)
context.font = '11px Montserrat';
context.fillText('".$row->nome_consultor."', 222, 258);
}
"
}
When going to the console and placing img4, it returns the tag img
with all attributes I requested for PHP, but html is not 'showing' the content inserted in <canvas>
.
Does anyone know if the canvas
have post-loading limitations? Or actually have some syntax problem?
Thanks in advance <3
Come on, it makes perfect sense what you said, but I make an ajax request to recover the template image. After that it puts everything I need to leave the template ready, as seen in the first image, after that the user needs to select the employee she wants and then on
onChange
ofselect
i perform another ajax request to return me the image of the selected employee and until then was to add the image along the name to the context ofcanvas
. All JS that is 'inside' of PHP will be added in a tag<script>
– user179947
That is, when he puts the content inside the
canvas
it is not visible, but it does exist! When I recover the variableimg4
in the console it returns exactly theimg
with the parameters defined by PHP, such assrc
,width
andheight
, but for some reason it does not exist in theDOM
ofcanvas
– user179947
If you are going to return a JS that must be executed when responding to the ajax object, you must set the correct mime-type in the header, which if I’m not mistaken is text/javascript. Then, upon receiving the reply, it uses the Eval() method. It would be Eval(responseText).
– Tiago Luz
The others
header
are in this same format and both show the image I need, but at some point theDOM
ofcanvas
loses theimg4
and puts only the template, user photo, name and position of the same.– user179947
I imagine that after defining the context of
canvas
in another request causes other contexts added in another plane not to exist– user179947
I’ll try to change some things and I’ll get back to you
– user179947
I’m not sure I’m 100% right, but it was exactly what I said, I passed the same parameters to the ajax that requests the employee’s photo, what it does is simple, search the template again, (inserts the user photo, name and position, "ALLOCATED IN $_SESSION") All right, just like I thought! I appreciate your help anyway, I really needed to hear what you said to give up what I was doing and redo it all again
– user179947
Any doubt, we’re there
– Tiago Luz