Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 canvas - Sharing saved image to social media

Thank you for taking your time to read this post.

i have looked through endless posts on this forum, but still are unable to achieve what am after.

I have created a html5 canvas that save users drawings and images. When the user press publish, he/she will be prompted with a popup frame (lightbox) previewing her image, with the option of sharing the image on social networks using Addthis.com.

This is what I have accomplished so far. 1. In my canvas I have a button called #btnPreview

$("#btnPreview").click(function (event) {
    canvas.deactivateAll();
    var strDataURI = canvas.toDataURL("png");
    var proporcao = 1;
    var proporcaoW = 500 / canvas.width;
    var proporcaoH = 400 / canvas.height;
    if (proporcaoW < proporcaoH) {
        proporcao = proporcaoW;
    } else {
        proporcao = proporcaoH;
    }
    $("#addthis_shareable").width(canvas.width * proporcao).height(canvas.height * proporcao).attr("src", strDataURI);
    //  $("#imgPreview").width(canvas.width*proporcao).height(canvas.height*proporcao).attr("src", strDataURI);
    $("#modalPreview").modal("show");
});

that opens up a lightbox popup window.

  1. The image is stored in <img id"imgPreview" src"data:image/png;base64,...."> when am trying to share the image with description on facebook, twitter etc it doesnt work.

I know what the problem it, but am unable to create the javascript and php script required for this task.

what I want to achieve is to ask this forum the best practice for this concept.

My idea is to create a save.Php script that saves the .png file without prompts on the server or creating a fake url www.example.com/image.png when I press #btnPreview

once the popup window loads my img url will be

<img id"imgPreview" src"www.example.com/images/md5_timestamp.png">

This is the closed example i have found
Example 1 that leads to example 2
Example 2 save base64
Example 3 Saving canvas as JPG or PNG
Example 4 Very Close to what am after - The bottom answer

I hope i explained it correctly.

Looking forward to your responds.

like image 206
Creativefly Avatar asked Feb 13 '13 06:02

Creativefly


People also ask

Can you save a HTML canvas image?

Saving HTML canvas as an image is pretty easy, it can be done by just right-clicking on the canvas and save it as an image.

Is html5 canvas fast?

On the one hand, canvas was fast enough on simple functions like pencil drawing due to native implementation of basic drawing methods. On the other hand, when we implemented classic Flood Fill algorithm using Pixel Manipulation API we found that it is too slow for that class of algorithms.

How do I load an image into canvas in HTML?

Importing images into a canvas is basically a two step process: Get a reference to an HTMLImageElement object or to another canvas element as a source. It is also possible to use images by providing a URL. Draw the image on the canvas using the drawImage() function.


1 Answers

Check this: Fabric.js canvas.toDataURL() sent to PHP by Ajax As you can see, you can send your PNG base64 data of your canvas and generate the image in PHP in server side. This is the code that you can use in PHP in server side: https://gist.github.com/rodrigopandini/2149853

like image 122
rodrigopandini Avatar answered Sep 28 '22 01:09

rodrigopandini