How to upload using PHP image in BLOB link?

Posted on

Question :

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?


GitHub project that originated question: link

What I’ve tried

//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


  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


Answer :

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,;

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

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


Leave a Reply

Your email address will not be published. Required fields are marked *