How to upload image canvas

Asked

Viewed 864 times

0

I get the result of an image through this function this.canvas.toDataURL('png'), this function generates the image normally, the dots are:

  • How to upload this to the PHP server using jQuery?
  • Is it the right way to upload? It won’t harm me because it’s not a normal upload, with form input etc..?

2 answers

1

How to upload this to the PHP server using jQuery?

It’s quite simple: Convert your canvas to blob and send an Ajax, with a small modification in the settings.

 function ajaxUpload(options) {
      options = $.extend({}, {contentType: false, cache: false, processData: false}, options);
      return $.ajax(options);
 }

 canvas.toBlob(function (blob) {

     var data = new FormData();
     data.append('imagem', blob);

     ajaxUpload({url: '/upload.php', data: data}).success(function () {
        console.log('upload concluído com sucesso');
     });
 });

Is it the right way to upload? It won’t harm me because it’s not a normal upload, with form input etc..?

Well, the only difference I see is that you will always have to use ajax. Besides, with input, usually in the upload submission, the file name is sent, different from the blob, that does not have the name of the source file, but that in the case could be solved in a simple way: Placing the third parameter in the call of .append:

  data.append('imagem', blob, 'imagem.jpg')

Moreover, there are no "problems", but differences.

  • Need: does not lock the page, because the blob will be built on 2° plane. Otherwise I would say that this callback is useless.

  • #toBlob probably duplicates the canvas data, while #toDataURL does a lot more. If callback is pq has obviously overhead, zzzzz. I’m not saying it’s worse to blobar than to base64ear at any time, dear.

0

How to upload this to the PHP server using jQuery?

First you need a file .php with function to decode the 64 base string of the image to add it in a file on the server. Then you request or post data to that file .php, with the image in Base64. Success.

~~ Example ~~

jQuery :

$.post({

    data: {

        // Manda a imagem em base 64
        base64: canvas.toDataURL()

    },

    // O arquivo .php com a função citada
    url: "addimage.php"

});

PHP :

<?php


    // Ignora todos os caracters até a primeira vírgula depois de ";" (semicolon)

    $png = substr(
                $_POST['base64'],
                strpos($_POST['base64'], ";") + 1
    );

    $png = substr(
                $png,
                strpos($png, ",") + 1
    );

    // Cria (ou substitui, caso existente) a imagem no servidor
    file_put_contents('img.png', base64_decode($_POST['base_64']));

?>

Is it the right way to upload? It won’t harm me because it’s not a normal upload, with form input etc..?

It is common to send data encrypted with Base64. If your string is too gigantic it may slow down on the server, but this is rare and difficult to do.

  • 1

    Note: Base64 will increase the file size by approximately 4 times, unless mistaken.

  • @Wallacemaxters You’re right. It would be much more efficient to send the code of the compressed image. If I’m not mistaken this.canvas.toDataURL('png') returns Base64, then is due undo/decrypt Base64 first. Another possibility may be to read the canvas data and turn it into a PNG (using DataView or Uint8Array) manually, Too.

  • I worked on a system that needed to do a transformation operation in Blob multi-image. Turn into Base64, large-scale, crashes the browser. The Blob nay.

  • @Wallacemaxters I think you read my comment in a hurry. I was saying the method #toDataURL returns encrypted data, but I was not thinking that you could convert canvas to a blob. Tbm I’ve worked a little with blob, but it was in my HTML5 experiments that today ñ do more.

  • 1

    Hello @kai, so there is no encryption, Base64 is not encryption, is encoding, Blob is advantageous to work with window.URL or with payload (upload via Ajax for example), Base64 on toDataURL I believe it works until today, even though little needed for the same reasons that other technologies exist, specific situations that Blob will not meet and prevent older Base64-based applications from breaking, should they remove such functionality from the native API.

  • @Guilhermenascimento Yes, I understand that Base64 ñ is cryptography, but blz

Show 1 more comment

Browser other questions tagged

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