Reputation: 21
I try to have a php script output a generated image as .jpg file:
$url = 'http://www.photopost.com/photopost/showfull.php?photo=7541';
file_put_contents('image.jpg',file_get_contents($url));
$page = file_get_contents($url);
echo $page;
The echo displays a correct image in the browser. but image.jpg is not saved.
How can I make this work ?
Upvotes: 1
Views: 2720
Reputation: 2900
Change your url from
http://www.photopost.com/photopost/showfull.php?photo=7541 //html document
to
http://www.photopost.com/photopost/watermark.php?file=7541 //downloadable url
Then use code from other answers or use imagecreatefromjpeg
http://php.net/manual/en/function.imagecreatefromjpeg.php
Upvotes: 0
Reputation: 545
Getting image from url using curl :-
$profile_Image = 'http://www.photopost.com/photopost/showfull.php?photo=7541'; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = ''; // your saving path
$ch = curl_init($profile_Image);
$fp = fopen($path . $userImage, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
Getting image from url using file_get_contents :-
$profile_Image = 'http://www.photopost.com/photopost/showfull.php?photo=7541'; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = ''; // your saving path
$thumb_image = file_get_contents($profile_Image);
if ($http_response_header != NULL) {
$thumb_file = $path . $userImage;
file_put_contents($thumb_file, $thumb_image);
}
Upvotes: 0
Reputation: 3242
You need to output a Content-Type header with a proper MIME type for the browser to be able to understand what kind of file the server is sending.
header('Content-Type: image/jpeg');
Refer to http://php.net/manual/en/function.header.php and https://www.sitepoint.com/web-foundations/mime-types-complete-list/ for a list of valid MIME types.
Upvotes: 1