2
How can I create a blob of an image in PHP and return to JS?
I’ve tried using the file_get_contents()
returning to jQuery and creating an objectURL but obviously it didn’t work.
2
How can I create a blob of an image in PHP and return to JS?
I’ve tried using the file_get_contents()
returning to jQuery and creating an objectURL but obviously it didn’t work.
0
If you are converting the URL image to its normal version you may be including the header "data:image/*;base64,"
, It will end up preventing the image from being read, as well as being unnecessary. If so, just do something like this before decoding the string in Base64: substr(imageURL, strpos(imageURL, ',') +1)
, and maybe it can be solved.
Another possible error: maybe you might be getting the contents of this image wrong. URL.createObjectURL
requires the first argument as an instance of Blob
. Perhaps the kind of response from your XHR
(if you are using) it is not "blob"
, then just do something like (xhr.responseType = "blob")
, before xhr.send();
, hence: URL.createObjectURL(xhr.response)
.
Answer the question, try:
(PHP)
<?php
/* responde o client-side com o conteúdo binário da imagem */
echo file_get_contents("image.png");
(Javascript (the jqXHR
jQuery does not offer "blob"
))
var xhr = new XMLHttpRequest;
xhr.open('GET', "*.php", true);
/* define a resposta do tipo "blob" */
xhr.responseType = "blob";
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var file = URL.createObjectURL(xhr.response);
} else /* erro de requisição */ ;
}
}; xhr.send();
Browser other questions tagged php javascript
You are not signed in. Login or sign up in order to post.
It would be nice to [Dit] the question and show the code you tried. It may have been missing some small detail. If you post a [Mcve] with the JS part, it’s even better.
– Bacco