Strict Standards: Declaration of blogRouter::parseRoute() should be compatible with birdyRouteHelper::parseRoute($queryParts, $possible = Array) in /home/chrismic/public_html/templates/Diverso/routers/blog.php on line 7
How to get an image out of a given URL with PHP

Warning: mb_detect_encoding() expects parameter 1 to be string, array given in /home/chrismic/public_html/birdy/db.php on line 19

Warning: mb_detect_encoding() expects parameter 1 to be string, array given in /home/chrismic/public_html/birdy/db.php on line 19

by


Warning: mb_detect_encoding() expects parameter 1 to be string, array given in /home/chrismic/public_html/birdy/db.php on line 19

In: Web Development

No Comments

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!";
?>


Strict Standards: Non-static method birdyComments::submitComment() should not be called statically in /home/chrismic/public_html/birdy/helpers/comments/inc_rate.php on line 166

Warning: mb_detect_encoding() expects parameter 1 to be string, array given in /home/chrismic/public_html/birdy/db.php on line 19

Leave a Reply   

The Sinner In Me (Chris Vulture Mix) by Chris Vulture on Mixcloud