Copy images in Google Drive to own server using PHP - php

I have a folder in Google Drive with product images (the folder is public). On request I want these images to be uploaded to my website.
Is there a simple way to copy all images to my own server using PHP, whitout having to set my mind into Google Drive API?
This gives me nothing to work with:
<?php
$url = "https-link-to-the-public-folder";
$html= file_get_contents($url);
print_r($html);
?>

The most straightforward method for uploading a file is by making a simple upload request. This option is a good choice when:
The file is small enough to upload again in its entirety if the connection fails.
There is no metadata to send. This might be true if you plan to send metadata for this resource in a separate request, or if no metadata is supported or available.
To use simple upload, make a POST or PUT request to the method's /upload URI and add the query parameter uploadType=media. For example:
POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media
The HTTP headers to use when making a simple upload request include:
Content-Type. Set to one of the method's accepted upload media data types, specified in the API reference.
Content-Length. Set to the number of bytes you are uploading. Not required if you are using chunked transfer encoding.
The following example shows the use of a simple upload request for the Drive API.
POST /upload/drive/v3/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: image/jpeg
Content-Length: number_of_bytes_in_file
Authorization: Bearer your_auth_token
JPEG data

Related

How can I tell the filetype of images without extension? How do they work?

I am dealing with images on the web that come without a file extension, like this:
https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w
Images like these can be found, e.g., on websites made with squarespace, like this demo: https://bedford-demo.squarespace.com/
I'm trying to download these images and store them on my server, using PHP. But how can I find out the actual URL of those images? How does this work? And how can I tell the filetype of this image? What is this sorcery?
Any hints are appreciated!
Quick answer:
To find out the Content-Type returned for any URL, look at this answer:
Get Content-Type of requested URL in PHP
Why you need the Content-Type:
Just like how not every webpage on the internet has an URL that ends with .html, images are not required to have an "extension" in their URL either.
What determines whether the browser will treat the data retrieved from the URL as an image is the Content-Type header in the HTTP response.
The URL you posted returns the following HTTP headers:
For HTML documents the Content-Type is text/html. You can inspect the headers as you browse by opening the Network tab of the developer console in your browser. Look for the "response headers".
You can get the mime type of the file with getimagesize:
<?php
$size = getimagesize("https://static1.squarespace.com/static/52a74d9ae4b0253945d2aee9".
"/t/52ed63b1e4b04368c021b921/1463088116168/?format=500w");
print_r($size["mime"]);
?>
Prints:
image/jpeg

Laravel 5.1 returning error 301 on AngularJS post containing base64 encoded image

Help, because I'm losing my mind.
I built an API using Laravel 5.1, and my app uses AngularJS (1.4.5) that communicates with the API. FabricJS plugin creates a Base64 encoded image from a canvas, and I want to send it using AJAX post method to a Laravel post route.
My post data in Javascript looks something like this:
var postData = {
image: "data:image/png;base64,iVBORw0KGgoAAAA(...)",
data: (some arrays and objects)
Now, when I post the data, it's about 2 MB with the image data. On post to Laravel route (post route, no trailing slashes), Laravel returns a 301 Moved Permanently (in Firefox 40.0.2), and in Google Chrome, Network tab displays 301 error followed by error 405 Method Not Allowed with an error in Laravel's RouteCollection.php on line 201.
Post JSON is valid (checked), and post works fine when I don't post the image data (parameter is set to image: null).
Server PHP version is 5.6.12.
Error seemingly gets output when I upload large JPEG files (like, compression 100 or 98). PHP settings says my upload limit is 20MB.
On the other hand, that same post works when the image is a JPEG, full HD, saved from Photoshop with Save for Web..., compression 80 with progressive option enabled.
WORKAROUND
Colleague and I tried this and it worked for us: for some reason, Laravel doesn't like full image data as a string, example:
data:image/png;base64,iVBORw0KGgoAAAA(...)
Solution is to break down that to actual base64 encoded string (in this case iVBORw0KGgoAAAA(...)) and from first part, and pass the file type as another parameter.
So the Javascript object (from top of post) would be something like this:
var postData = {
image: "iVBORw0KGgoAAAA(...)",
imageType: "png",
data: (some arrays and objects)
Using PHP, we reassemble the entire image string (if "png" prepend "data:image/png;base64," to image) and (in our case) use Intervention Image plugin and save image to a directory on a server.
I had an issue passing sterilized XML.
Try passing the string in a query string and then str_replace(' ', '+', $base64string);

Content types for file upload

I wanted to upload a file(any format) to an api. If I upload the file using multipart/form-data then the file gets uploaded.
I wanted to upload the file using application/json as the Content-type in the header.
Could you'll tell me if this method is possible/allowed?
Which are Content-type besides multipart/form-data which supports file upload?
Is there a single standard for content type which can be used for get, put, post etc.
Thanks in Advance.
Only multipart/form-data can trigger the population of the PHP $_FILES global, otherwise you're going to need to nest your attachment data in your API request if using something like JSON or XML (base64_encoding comes to mind)
You could also process a PUT request - http://www.php.net/manual/en/features.file-upload.put-method.php
RFC 4627:
The MIME media type for JSON text is application/json

drag drop file upload with file api server side code

I am trying to make drag drop file upload using FILE api and folowing this tutorial
https://developer.mozilla.org/en/Using_files_from_web_applications
its works fine. when i see ajax request "POST" data with firebug i see something like this
--30000 Content-Disposition: form-data; name='fileId'; filename='header.jpg' Content-Type: application/octet-stream ÿØÿà �JFIF��H�H��ÿÛ�C� $.' ",#(7),01444'9=82<.342ÿÛ�C
How can i save this data in a image format at server end ??
You can check live demo # http://www.amitpatil.me//demos/in_progress/gmail_fupload/file.html
You need to use the global $_FILES. It contains all the information about the uploaded Data.
Have a look at http://www.php.net/manual/en/features.file-upload.post-method.php.
In your case you will get the tmp. uploaded-file by using $_FILES['fieldID']['tmp_name'].

Reading Image from AJAX response

I have a url that when requested will return an image. I want the url to be requested through an AJAX request. The ajax request returns binary data which is what is being deplayed. but i want the actual image displayed not the binary data. Am using php on the server side and i have set the below headers:
header("Cache-Control: no-cache, must-revalidate");
header ("Content-type: text/jpg; charset=windows-1251");
Please i need advise on what i need to do.
You need to send an url to this Photo, even if its just a PHP file with an ID
For example
<img src='thumbGenerator.php?id=1337' />
And then this PHP file outputs the binary data.
You need to read up on data urls for binary image data. Keep in mind this method isn't supported in Internet Explorer. If you need cross-browser compatibility then you will need to store the files somewhere on the server and link to them using the images src attribute. Or link the src attribute to a php script that generates the images and serves them correctly.

Categories