Sometimes there are URLs which point to images. While it's easy to just
<img src="URL" />
in HTML, there are cases where this is not possible, or will not work.
In such cases, this handy PHP method will come to the solution:
<?php function remoteImage($URL) { $remoteImage = htmlentities($URL); $imginfo = exif_imagetype($remoteImage); $image = false; switch ($imginfo) { case "IMAGETYPE_JPEG": $image = imagecreatefromjpeg($remoteImage); $imginfo = "image/jpeg"; $type = "imagejpeg"; $filename = time().mt_rand().'.jpg'; // if you want to save it locally break; case "IMAGETYPE_PNG": $image = imagecreatefrompng($remoteImage); $imginfo = "image/png"; $type = "imagepng"; $filename = time().mt_rand().'.png'; // if you want to save it locally break; case "IMAGETYPE_GIF"; $image = imagecreatefromgif($remoteImage); $imginfo = "image/gif"; $type = "imagegif"; $filename = time().mt_rand().'.gif'; // if you want to save it locally break; } if ($image) { ob_start(); header( "Content-type: ".$imginfo ); $type( $image, NULL, 100 ); imagedestroy( $image ); $i = ob_get_clean(); return "data:" . $imginfo . ";base64," . base64_encode( $i ); } else return false; } ?>
And you can call it like this:
<?php $remoteImage = remoteImage("http://www.example.com/somefile_that_generates_an_image"); if ($remoteImage) echo "<img src='". $remoteImage ."' />"; else echo "Image not found!"; ?>