Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

498
Visualizações
How do I save fabric.js image(s) details to mysql and php?

I'm new to Fabric.js and I've been able to create an application in PHP/MYSQL that loads preselected images into a predefined canvas. It works well and the user is able to move around the images they selected within the canvas. I need a way for them to save their "project" so that once they close and come back to open their project. Everything is exactly where they left them. I imagine I need to capture the width, height, left, top details of each object and save them in a table in json format so that it loads the items as they were.

I really don't know how to go about it. Here's a snippet of my code. HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script>

<canvas id="canvas"> </canvas>

<script>
   
   
      // Initiate a Canvas instance and add backgroundColor
      var canvas = new fabric.Canvas('canvas', {
         backgroundColor: '#fff'
      });
      

      <?php

$query = "SELECT * FROM products p, project_letters l WHERE l.product_id =p.product_id and l.project_id = $p ORDER BY l.letter DESC";
$mres = mysqli_query($Connection, $query);
$NumOfimages = mysqli_num_rows($mres);

if ($NumOfimages > 0) {
    while ($row= mysqli_fetch_array($mres)) {
?>
function addImage<?php echo $row['pl_id']; ?> () {
    fabric.Image.fromURL('images/products/<?php echo $row['image']; ?>', function (img) {
        img.scale(0.1).set('flipX', false);
        canvas.add (img);
    }, {
    left: 10,
    top: 10
});
}

addImage<?php echo $row['pl_id']; ?>();

<?php }}?>
      canvas.setWidth(<?php echo $WIDTH ?>);
      canvas.setHeight(<?php echo $HEIGHT ?>);
      
      var link = document.createElement('a');
    link.innerHTML = 'Export as image';
link.addEventListener('click', function(ev) {
    link.href = canvas.toDataURL();
    link.download = "<?php echo $project['project_title'] ?>.png";
}, false);
document.body.appendChild(link);
      
</script>

Please note the width and height of the canvas are already predefined with PHP. I'm using PHP to loop through the images for that particular project. The images appear successfully on the canvas and can be moved. So now I'd like to be able to:

1- Dynamically load the width, height, left and top of every image
2- Capture the new width, height, left and top if the user moves the image on the canvas and save it to mysql table
3 - Save the entire canvas as an image with the new coordinates.

Any help will be appreciated. I'm not very good with JavaScript.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda