I have a document with an image attachment myimg.jpg which I would like to GET using Sag.
In my browser I am able to retrieve this image if I visit this url: http://localhost:5984/mydb/thedocid/myimg.jpg.
Using sag I am able to retrieve documents, but unable to retrieve attachments. I have tried to retrieve the image like so:
$img = $sag->get('thedocid/myimg.jpg')->body;
Instead of retrieving the image PHP seems to become unresponsive. I also thought disabling JSON decode might solve it, but it still causes PHP to become unresponsive.
$sag->decode(false);
$img = $sag->get('thedocid/myimg.jpg');
What am I doing wrong? How does one properly retrieve an attachment using Sag?
EDIT: After quite some time the attachment has been retrieved. Why is it so slow? The attachment is merely 4kb.
I still do not know why my initial code was so unresponsive/extremely slow, but thanks to Dave's comment I got an alternative way to retrieve the document with the attachment:
$doc = $sag->get('thedocid/?attachments=true')->body;
$img = base64_decode($doc->_attachments->{'myimg.jpg'}->data);
Related
I have this line:
move_uploaded_file($_FILES["img"]["tmp_name"], "uploads/" . "$img_name");
That used to work when I received the image through a direct POST (without JSON.stringify the content).
Now I'm sending the ajax with the image inside of an array stringified, so I'm getting the contents of the array in the PHP file in this way:
$unstringified = json_decode(file_get_contents("php://input"), true);
And then I use:
$title = $unstringified["title"];
$content = $unstringified["content"];
$img = $unstringified["img"];
The problem is that now the move_uploaded_file stop working (seems to be no error but the image doesn't appear saved in the folder anymore). I tried some options like these, but didn't work.
move_uploaded_file($unstringified["img"]["tmp_name"], "uploads/" . "$img_name");
move_uploaded_file($_FILES[$unstringified["img"]]["tmp_name"], "uploads/" . "$img_name");
Any idea to solve this? Should I "convert" the image in any format until putting it in the array I will stringify? or I have to get the image in another way in the PHP file?
Thanks a lot in advance.
Leandro.
I wanted to pass the image inside a JSON because always that I've tried to pass and image with other string variables with formdata, I've always got an error, even playing with all posible variables of dataType, contentType , Cache, etc, and researching through lot of answers here. I couldn't find a solution to use form data to pass all togheter.
What I found I can do is to pass all inside within a JSON object without getting errors, so that's why I wanted to receive the image in PHP under this format and save it in this way.
But after trying lot of ways, again, and couldn't find a solution for this, I gave up, and use 2 separate sends, one json object with all the strings inside, and one formdata with only the image (if I don't add another variables like strings, there's no problem).
I'm sure it's not the best way, but it's what I can do.
Thanks all!
I am trying to save to disk an image that is served to me via a JSON result. The returned JSON result property that I am interested in is this:
https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df
Which is the correct image. The problem is that, while the above URL does display the image, it does not allow me to download it, yet I can download it by right-clicking on it.
What I need to be able to do is, using my PHP code, save it to disk.
I have no issues saving results from other sites that give results that link to a direct image extension (.jpg, .gif or .png). But I have not been able to figure out how to programmatically download the image from the above URL.
Is it possible?
This is the code that I use, which works correctly on results that give a URL that has a correct image extension. The URL returned is loaded into the $largeimg variable.
$input = $largeimg;
$output = 'image.jpg';
file_put_contents($output, file_get_contents($input));
How do I achieve this?
file_get_contents() is able to accept raw URI arguments. Your code works perfectly for me, if modified in the way:
$input = 'https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df';
So, file_get_contents() can download the image directly. I think, the problem is your $largeimg variable.
I'm trying to send a .png image to my user via phpmailer. The image is shown when I use <img> tags, but I want it to display as a real attachment that the user can open/save/print (like in this screenshot). I read that I can use $mail->addStringAttachment for this. So I tried this, and it does send an attachment with the email, but when I try to open it, it says that Windows Picture Viewer can't open the file. Also saving to my computer and then opening with Paint doesn't work, it tells me thats not a valid file or something. I think this is because it's no static image, but an image generated by an API, namely:
$qr = 'http://api.qrserver.com/v1/create-qr-code/?data=' . $guid . '&size=250x250';
So this image should be sent as an attachment. Does anyone know how I can make this work?
I got it to work fine as an attachment by doing the following:
$qr = file_get_contents("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example");
$mail->addStringAttachment($qr, "qr.png");
The reason it's failing is that you're trying to attach the URL as image data. You need to fetch the data from the URL first, then attach it to something.
Go one step at a time - make sure that you're getting back valid image before trying to email it - e.g.
file_put_contents('qr.png', file_get_contents($qr));
and make sure you get a valid image saved in there. When you know that's working, then try and email it with
$mail->addStringAttachment(file_get_contents($qr) 'qr.png');
Though perhaps with a bit more error checking!
I actually want to upload an image to a server.
To achieve this, i want the user just paste the image into chrome (the image is a print screen in fact), and then i post the stream to a php page, convert the stream as an image, and then upload it.
How can i achieve this web application ?
Today i have develop some differents parts :
I used this script, and i create the Upload.php page which gets the post variable and try to Create and image.
The problem i have, is that when i post the data, i only get a blob. I would like to get a base64 stream.
Can you help me ?
Thanks in advance.
I'm not sure why you are specifically looking for a "base 64 stream". If you are sending the Blob to your server via ajax, as far as your server is concerned, it's a file. Treat it no different than any other upload server-side. A Blob is a File without a name property. That's perhaps a bit overly-simplistic, but my point is that, again, this is really nothing more than a file as far as your server knows.
Assuming you are sending a multipart-encoded request, I'd like to point out that most user agents will set the filename property of the item's Content-Disposition header in the request to "blob" when the item you are uploading is a Blob instead of a file. It is possible to change this value in some browsers via the 3rd argument in FormData's append method, but I wouldn't rely on this just yet.
Also note that, if you are interested in a library that handles all of this already, I maintain, Fine Uploader which natively supports uploading images via paste in Chrome.
To answer this old question: Posting an image from clipboard with chrome is pretty much the same as posting a dropped file - except that the image/blob doesn't have the properties "name" and "lastModified".
var entry = items[i].webkitGetAsEntry();
if (!entry) entry = items[i].getAsFile();
if (entry instanceof Blob) /** CHROME pastet Bilder als Blob **/
{
entry.isFile = true;
entry.lastModifiedDate = new Date();
entry.name = ""+new Date().getTime()+"."+entry.type.split('/')[1];
}
if (entry.isFile)
{
//handle dropped file
}
Ok, I'm hoping I can explain my situation rather than pasting lines and lines of code.
Currently, JSON sends positional info to my PHP file which in turn uses this data to generate an image, saves it and returns the filename via JSON back to browser. Javascript then refreshes the image on screen.
This all works fine at the moment, but I am wanting to optimise the process and look at the possibility of outputting the image file straight after it's created then save afterwards.
My ideal solution would be something like:
header('Content-Type: image/gif');
echo $this->canvas;
// Save user file
$this->canvas->writeImage( $this->userFile = 'user_img.gif' );
$this->canvas->destroy();
// encode everything and send to browser
echo json_encode(array('misc data back to the browser'));
(I still need to send data back to browser via JSON)
And in my HTML I would have the image laid out like this:
<img src='json-processing-script.php' />
But as usual nothing is ever that simple, so I'd like to hear if anyone can make any pointers.
In your example, the json would be added to the gif, messing up your image. If you want to return these two completely different things from your php script, you would have to encode the image, add it to the json and extract it in the javascript to get the source of your image.
See for example this question.