Search code examples
phpjavascriptblobfileapi

BLOB URL to file with PHP


I'm trying to create a file preview system.

When the user click in a upload button and select files, I use the HTML5 FILE API to get all the file info and create a thumbnail for the file.

The user can cancel the upload of some of thoose files and add more files too.

What I have now is this:

<p><input id="blob" name="blob" type="file" size="75" multiple /></p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input id="blobtype" name="blobtype" type="text" size="20" />
<input id="bloburl" name="bloburl" type="text" size="50" />
<input id="salvar" name="salvar" type="submit" value="salvar" />
</form>
<p id="result"></p>

And my javascript:

function cbu(file)
{
if (window.URL){ return window.URL.createObjectURL(file);}
else if (window.webkitURL){ return window.webkitURL.createObjectURL(file);}
else if (window.createObjectURL){ return window.createObjectURL(file);}
else if (window.createBlobURL){ return window.createBlobURL(file); }
}

document.getElementById("blob").onchange = function(event)
{
var result = document.getElementById("result");
var i = 0, j = event.target.files.length;
for (i = 0; i < j; i++)
{
    var url = cbu(event.target.files[i]);
    var img = document.createElement("img");
    img.onload = function(){ }
    result.appendChild(img);
    img.src = url;
    document.getElementById("blobtype").value = event.target.files[i].type;
    document.getElementById("bloburl").value = url;
    }
};

It genarate a thumbnail like this:

<img src="blob:b9a8e310-05b3-48ba-ba24-9ca7cf287f71">

This value blob:b9a8e310-05b3-48ba-ba24-9ca7cf287f71 is send to a PHP file when user submit the form.

So my question is, how can I transform this blob url into a file so I can save it in my app?

There is a better way of doing this?

Thank for your help!


Solution

  • well, I keep looking for solutions and I found this one: Canvas.

    Canvas element have the toDataUrl() method that returns a Data URL for the image that it contains.

    So I keep doing the same thing but when I submit the form I use the following function:

    function putImage(){
        var url = document.getElementById("img1").src; // 'img1' is the thumbnail - I had to put an id on it
        var canvas = document.getElementById("MyCanvas");
        if (canvas.getContext) {
            var ctx = canvas.getContext("2d");
    
            var img = new Image();
            img.src = url;
            img.onload = function () {
                // Desenha a imagem no canvas...
                ctx.drawImage(img,0,0);
    
                // Grava o Data URL na imagem...
                var myImage = canvas.toDataURL("image/png");
                document.getElementById("dataurl").value = myImage;
            }
        }
    };
    

    I create one more element on the page for canvas:

    <canvas id="MyCanvas">This browser or document mode doesn't support canvas</canvas>
    

    and one more element on the form to save the Data URL:

    <input id="dataurl" name="dataurl" type="text" size="50" />
    

    well, now I can get it all in PHP and do something like this:

    if($_POST)
    {
        //echo '<pre>'; print_r($_POST); echo '</pre>';
    
        $blob = $_POST['bloburl'];
        $type = $_POST['blobtype'];
        $data = $_POST['dataurl'];
    
        $contents_split = explode(',', $data);
        $encoded = $contents_split[count($contents_split)-1];
        $decoded = "";
        for ($i=0; $i < ceil(strlen($encoded)/256); $i++) {
            $decoded = $decoded . base64_decode(substr($encoded,$i*256,256)); 
        }
    
        $fp = fopen('imagembaixada.jpg', 'w');
            fwrite($fp, $decoded);
            fclose($fp);
        }
    }
    

    Now my image is saved.

    It works, but looks pretty inefficient.

    If someone knows a better way to create a preview for images before upload them, please tell me!