How to create a Blob from an URL? - php

I have a URL like that (basic URL) :
"http://blablablbalbalka.jpg"
which allows me to retrieve an image on a website.
I would like with PHP, retrieve that image and create a BLOB for an insert in a database.
How can i do that ?
Do I need to use function like file_get_contents ? fopen ?
Thanks :)

You could use as you say file_get_contents (depending on security on the webserver - see manual for info) or you could use CURL to fetch the image.
$img = file_get_contents("theurl");
Curl:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "theurl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$img = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
then insert $img into a BLOB field...

Related

How to display remote image in php webpage

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.example.com/xxx.png');
curl_setopt($curl, CURLOPT_REFERER, 'http://www.example.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
header('Content-type:image/PNG');
echo $result;
curl_close($curl);
The function header() didn't work, it always dispalyed binary data.
Maybe because I used these codes in the middle of webpage what has existed.
The webpage outputed some texts before header(), so it didn't work.
I want to get image by url, and display image directly, no need save file to disc.
So how can I do ? Please help me !!!
—————————
I need set referer, so I used curl.
Use the image directly as follows:
<img src ='http://www.example.com/xxx.png'>

How to figure out type of the image before downloading it?

I know I can save an image using the following method:
$input = 'http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com';
$output = 'google.com.jpg'; // << How to save the image with proper extension?
file_put_contents($output, file_get_contents($input));
But what if I don't know the format of the downloaded image? What if it's "png"? How can I figure out the type of target image before saving it?
The best thing to do is just download it and rename the file later. That way, you only have to make one request.
Another thing you can do however is make an HTTP HEAD request. This gets all of the response headers, including the Content-Type header, so you can decide if you want that data or not before you download the file. However, not all servers support HEAD requests.
In any case, cURL is the easiest way to do this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/something');
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$headers = curl_exec($ch);

PHP, image from FTP to BLOB

I have an image on my FTP, here : /home/www/myImage.jpg
I need, in PHP, to retrieve this image and create a BLOB.
I use CURL and i don't know how do to that.
I have try this :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "ftp://MY_IP/home/www/myImage.jpg");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "LOGIN:PASSWORD");
$myBlobImage= curl_exec($ch);
curl_close($ch);
But with this, i retrieve nothing... Any ideas ? Thanks !
If You are running the code on the same server as the file is stored at, then You simply could do:
file_get_contents('PATH/TO/MyFile.Ext');
which loads the file's content. Now You could easily store it to the database to a BLOB column. You could base64_encode the data first or instead of file_get_contents use a byte stream to read stream of bytes...

Can I use a URL as the source for imagecreatefromjpeg() without enabling fopen wrappers?

I know it’s possible to use imagecreatefromjpeg(), imagecreatefrompng(), etc. with a URL as the ‘filename’ with fopen(), but I'm unable to enable the wrappers due to security issues. Is there a way to pass a URL to imagecreatefromX() without enabling them?
I’ve also tried using cURL, and that too is giving me problems:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.../image31.jpg"); //Actually complete URL to image
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
$image = imagecreatefromstring($data);
var_dump($image);
imagepng($image);
imagedestroy($image);
You can download the file using cURL then pipe the result into imagecreatefromstring.
Example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // good edit, thanks!
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // also, this seems wise considering output is image.
$data = curl_exec($ch);
curl_close($ch);
$image = imagecreatefromstring($data);
You could even implement a cURL based stream wrapper for 'http' using stream_wrapper_register.
You could always download the image (e.g. with cURL) to a temporary file, and then load the image from that file.

How to know size of flv size from url?

If I know flv video,
for example:
www.domain.com/video.flv
I can't use "filesize" because it doesn't able to check size from external url, right?
So, how can I know his size?
You can use CURL to get the headers of a remote file, including the size of a file.
$url = 'http://www.domain.com/video.flv';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$headers = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
Setting CURLOPT_NOBODY to true, makes CURL not download the body of a requested URL.
You could use CURL to send a HTTP HEAD request to the URL you want to know the file size of. The server should send back a content-length header in its response, listing the filesize in octets (bytes).
The server isn't necessarially going to actually send the content-length header though. If it doesn't, then your only option is to actually download the file in full.
I can't use "filesize" because it doesn't able to check size from external url, right?
Yes it can do that, but it's a bit ridiculous to do so, because the url wrappers will first download the file and then check its size.
You could do a HEAD request, which will ask the server for the file size without requesting the file data.
Relevant snippet here:
http://snippets.dzone.com/posts/show/1207

Categories