Reham Fahmy
Reham Fahmy

Reputation: 5063

Saving thumbnails images to certain directory

I'm willing to use thumbnails into my website which is mainly like websites directory. I've been thinking to save url thumbnails into certain directory !

Example :-

I'm going to use free websites thumbnails service that gives me code to show thumbnail image of any URL as follow

<img src='http://thumbnails_provider.com/code=MY_ID&url=ANY_SITE.COM'/>

This would show the thumbnail of ANY_SITE.COM

i want to save the generate thumbnail image into certain directory my_site.com/thumbnails

Why i'm doing this ?

in fact my database table is like my_table {id,url,image} where i'm going to give the image thumbnail random name and store its new name into my_table related to its url then i can call it back anytime and i know how to do it but i don't know how to save it into certain directory.

any help ~thanks

Upvotes: 0

Views: 154

Answers (2)

Paul Dessert
Paul Dessert

Reputation: 6389

Using cURL should work for you:

$file = 'the URL';

$ch = curl_init ($file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);

$fullpath = 'path to destination';

$fp = fopen($fullpath);
fwrite($fp, $rawdata);
fclose($fp);

Upvotes: 1

Crashspeeder
Crashspeeder

Reputation: 4311

You could use curl to fetch the remote image. You can save it with curl_setopt($handler, CURLOPT_FILE, '/my/image/path/here.jpg');. The id could be something simple like a hash of the original URL. Obviously you'd have to check to make sure the directories exist before you save the file (using is_dir() and creating them with mkdir() if they don't).

Upvotes: 1

Related Questions