Just wondering if there is any difference with the two below code examples:
$image = 'http://www.example.com/image.jpg'
$photo = file_get_contents($image);
ob_start();
header("Content-type: image/jpeg");
print($photo);
ob_end_flush();
or…
$image = 'http://www.example.com/image.jpg'
$photo = file_get_contents($image);
ob_start();
header("Content-type: image/jpeg");
readfile($photo);
ob_end_flush();
readfile‘s parameter is a filename, not the content itself. Therefore, you’d call it like this:Since
readfilereads and write chunks at a time, its memory consumption will be constant, whereas when you store the result offile_get_contentsinto$photo, you’ll need to have enough memory to store the image.In your case, the output buffering makes the
file_get_contentsvariant demand twice as much memory as the image’s size. For a large image,readfiletherefore halves the memory requirements. Note that your output buffering means that the download will be delayed. If you don’t need it for anything else, you will get better performance (both in actual speed and server memory requirements) if you simply disable it: