How to upload using PHP image in link BLOB?

Asked

Viewed 603 times

3

I’m using 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 PHP from file to server, since apparently the file is local?

Dice:

Github project that originated question: https://github.com/xkeshi/image-compressor

What I’ve already 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 showing

Warning: file_get_contents(blob:http://localhost/945f825f-054a-4170-9d79-ac1dba593d23): 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

  • your problem is in the image path you provide as parameter

  • 1

    Consider adding the snippet of code you are using/instantiating the library. It’s hard to imagine how you got one objectURL once the library only returns File or Blob

  • Well, now I see where you got this objectURL...you took it off the page DEMO. Note that the repository is not public, the maintainer uses Vue and what it does is use the library to generate the Blob() and uses URL.createObjectURL() to generate this link and assign to an element <a> with the attribute download. You should consider the answer presented to you, it is the most recommended way to upload the object that the library returns to you.

1 answer

6


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/session of browser, that spawned them.

Therefore, it is evident that PHP does not have access to URL maid.

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

See the example on 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 this is done, just save the uploaded file to PHP in a similar way to saving a normal upload.

Upload a file with AJAX

POST METHOD uploads

  • Fix me if I’m wrong bad, in this example the file is coming from a <input type="file"> which is from the user’s file system and not from a blob?

  • I will correct myself here: your answer is correct, out of curiosity I tested the library and it returns only File or Blob I can’t imagine how he got a objectURL

  • @Lauromoraes is what the library works internally. This is what most libraries do/are. They encapsulate and facilitate native functionality, providing a more developer-friendly interface/API.

  • 2

    @Lauromoraes my turn to fix me, I thought you were the author of the question. If you want to download the compressed file, one can use the promise returned, get the generated URL and start the download. I believe this was the starting point of the author of the question.

  • 2

    But this is not what is returned to the user. The objectURL is used internally to supply an image and a canvas subsequently (within the Promise result chain) extracts the dataRI of this canvas converts into buffer and supplies a Blob(). This yes in turn is what is returned as a result. See on the lines 231 -> 235

  • 2

    @Lauromoraes is also possible that the author has debugged the code to validate where the image is created.

  • That’s exactly why I can’t imagine where he got one from objectURL...maybe it was "debugging".

  • If you look at the Github project, its objective is to compress images, when selecting an image by an input of type file, the file is compressed where it will be possible to download the image by the link blob I mentioned. As I said, all this happens in the browser, there is no physical file, just a download button that directs to the link.

  • 2

    @RORSCHACH believe the question in the air is, the proposed code solved its problem?

Show 4 more comments

Browser other questions tagged

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