How can I create a blob of an image in PHP and return to JS?
I've already tried using file_get_contents()
returning to jQuery and creating an objectURL but it obviously did not work.
How can I create a blob of an image in PHP and return to JS?
I've already tried using file_get_contents()
returning to jQuery and creating an objectURL but it obviously did not work.
If you are converting the URL image to your regular version you may be including the "data:image/*;base64,"
header, 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 base64 string: substr(imageURL, strpos(imageURL, ',') +1)
, and maybe it can be solved.
Another possible error: You may be receiving the content of this image in the wrong way. URL.createObjectURL
requires the first argument as an instance of Blob
. Maybe the response type of your XHR
(if you're using) is not "blob"
, so just do something like (xhr.responseType = "blob")
, before xhr.send();
, hence: URL.createObjectURL(xhr.response)
.
Answering the question, try:
(PHP)
<?php
/* responde o client-side com o conteúdo binário da imagem */
echo file_get_contents("image.png");
(JavaScript (% of jQuery% does not provide jqXHR
))
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();