Show image using file_get_contents
Do i need to modify the headers and just echo it or something?
exactly.
Send a header("content-type: image/your_image_type");
and the data afterwards.
file_get_contents displaying image incorrectly
Try this:
<?php
$img = 'http://images.itracki.com/2011/06/favicon.png';
$info = getimagesize($img);
header('Content-type: ' . $info['mime']);
readfile($img);
?>
You should use readfile()
instead of file_get_contents()
in this situation because readfile()
will send the file contents directly to the browser rather than storing it in memory for a later retrieval, which is what file_get_contents()
does.
Also note that the content-type
is retrieved dynamically so that you don't have to worry about specifying it incorrectly in the header.
Some images may have an extension that doesn't match its content-type
or mime-type
, so this corrects for those type of issues.
Fetch image from url using file_get_contents
@h2ooooooo actually bro im not looking for a single image ... i just want to save this 9 images in a folder
<?php
for ($i = 2 ; $i <= 10; $i++) {
$imageName = "rate-bar-bg-" . $i . ".jpg";
$imageContent = file_get_contents("http://www.lafourchette.com/p-3.3.0/default/" . $imageName);
file_put_contents($imageName, $imageContent);
}
?>
Remote image display is not working (now working using file_get_contents and curl)
You are trying to send remotely inlined data... It's a weird idea, and i'm not sure the browser understand what are you trying to achieve...
imageserver.php
<?php
$url = 'http://google.com/images/srpr/logo11w.png';
function data_uri($file)
{
$contents = file_get_contents($file);
$finfo = new finfo(FILEINFO_MIME);
$mime = $finfo->buffer($contents);
return array($mime, $contents);
}
list($mime, $content) = data_uri($url);
header('Content-type: '.$mime);
die($content);
?>
file_get_contents or readfile for displaying filesystem image
If you don't need to manipulate the images (resizing, adding watermarks,...) readfile()
will be the best choice as it writes the file directly to the output buffer. file_get_contents()
will read the file into memory instead - requiring a lot of memory with large files.
Get Image With file_get_contents it return Not Found Error
Try this.
There is problem with URL Special character.then you have to decode some special character from url basename.
$imgfile = 'http://www.lagrolla.com.au/image/m fr 137 group.jpg';
$destinationPath = '/path/to/folder/';
$filename = basename($imgpath);
$imgpath = str_replace($filename,'',$imgpath).rawurldecode($filename);
copy($imgfile,$destination_path.$filename);
Image is not downloading using file_get_contents
What if you change the fopen() to:
$file = fopen($destination, "wb+"); // binary writing
Let me know.
EDIT
Here's the code that will work:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 0);
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:23.0) Gecko/20100101 Firefox/23.0\r\n" .
"Referer: http://www.funnyjunk.com\r\n"
)
);
$image_url = 'http://static.fjcdn.com/pictures/The+smarterest.+Wow+a+description+I+could+say+anything+I_298f02_4766792.jpg';
$doc_root= $_SERVER['DOCUMENT_ROOT'];
$image_upload_path = $doc_root . '/';
$image_name2 = 'img-' . time() . '.jpg';
$destination = $image_upload_path . $image_name2;
$data = file_get_contents($image_url, false, stream_context_create($opts));
//$file = fopen($destination, "w+");
//fputs($file, $data);
//fclose($file);
header("Content-type: image/jpeg");
echo $data;
?>
stream_context_create() with the referrer to the original site did the trick.
Related Topics
Get in Woocommerce Cart the Product Id of a Cart Item
Use PHP to Set Cron Jobs in Windows
Any Way to Return PHP 'Json_Encode' With Encode Utf-8 and Not Unicode
Get Difference Between Associative Rows of Two 2-Dimensional Arrays
Symfony2 Conceptual Issue: General Bundles Vs. Specific Ones
Localhost Vs. 127.0.0.1 in MySQL_Connect()
Find Out How PHP Is Running on Server (Cgi or Fastcgi or Mod_PHP)
Inserting Data to Table (MySQLi Insert)
Add 'Watermark' to Images With PHP
How to Access PHP With the Command Line on Windows
A Problem Occurred Somewhere in the Ssl/Tls Handshake
If Statement in the Middle of Concatenation
Stop People Uploading Malicious PHP Files Via Forms
In PHP With Pdo, How to Check the Final SQL Parametrized Query
Json_Decode Returns Json_Error_Syntax But Online Formatter Says the Json Is Ok