How to upload using PHP image in BLOB link?

3

I'm making use of a GitHub (xkeshi / image-compressor) project that compresses images using JavaScript. It generates a download of the compressed file with the following link for example blob:http://localhost/945f825f-054a-4170-9d79-ac1dba593d23

Note that the URL starts with blob:

How to upload with PHP from the file to the server, since apparently the file is local?

Data:

GitHub project that originated question: link

What I've tried

<?php
//Arquivo que desejo fazer upload blob:http://localhost/945f825f-054a-4170-9d79-ac1dba593d23
$url = 'blob:http://localhost/945f825f-054a-4170-9d79-ac1dba593d23';
$img = '/p/a.jpg';
file_put_contents($img, file_get_contents($url));
?>

Error displaying

  

Warning:   file_get_contents (blob: link ): failed to open stream: Invalid argument in   C: \ xampp \ htdocs \ 1 \ a \ b \ docs \ p.php on line 5


  

Warning: file_put_contents (/p/a.jpg): failed to open stream: No such   file or directory in C: \ xampp \ htdocs \ 1 \ a \ b \ docs \ p.php on line 5

    
asked by anonymous 04.08.2018 / 03:09

1 answer

5

The protocol blob or Object-URLs can only be generated by browser and these URL's can only be accessed / managed in the same instance / browser , which generated them.

Therefore, it is clear that PHP does not have access to URL created.

On the other hand, you can send the file created by the xkeshi/image-compressor library via ajax ( XMLHttpRequest ).

See the example in manual :

new ImageCompressor(file, {
    quality: .6,
    success(result) {
        const formData = new FormData();

        formData.append('file', result, result.name);

        // Envia a imagem comprimida ao servidor usando XMLHttpRequest.
        axios.post('/path/to/upload', formData).then(() => {
            console.log('Upload success');
        });
    },
    error(e) {
        console.log(e.message);
    },
});

Once you've done this, you just need to save the uploaded file in PHP, similarly to saving a normal upload.

Upload file with AJAX

POST method uploads

    
06.08.2018 / 13:50