Render PHP File as an Image (JPG, PNG, or GIF)

render_image_with_php
December 14th, 2011
|

I recently had to do this for a project and want to post it here as a resource for myself. For this example you pass the url to the image in through the URL, i.e.

http://www.example.com/?imgurl=/image.png

You can download the php file if you want to see it in your own editor.

<?php
	// Set the path to the image (I'm using a WordPress theme)
	$themepath = dirname(__FILE__);
	$rootpath = $themepath . '/../../..';
	$imgpath = $rootpath . $_GET['imgurl'];
	
	// Get the mimetype for the file
	$finfo = finfo_open(FILEINFO_MIME_TYPE);  // return mime type ala mimetype extension
    $mime_type = finfo_file($finfo, $imgpath);
	finfo_close($finfo);
	
	switch ($mime_type){
		case "image/jpeg":
			// Set the content type header - in this case image/jpg
			header('Content-Type: image/jpeg');
			
			// Get image from file
			$img = imagecreatefromjpeg($imgpath);
			
			// Output the image
			imagejpeg($img);
			
			break;
	    case "image/png":
			// Set the content type header - in this case image/png
			header('Content-Type: image/png');
			
			// Get image from file
			$img = imagecreatefrompng($imgpath);
			
	        // integer representation of the color black (rgb: 0,0,0)
	        $background = imagecolorallocate($img, 0, 0, 0);
			
	        // removing the black from the placeholder
	        imagecolortransparent($img, $background);
			
	        // turning off alpha blending (to ensure alpha channel information 
	        // is preserved, rather than removed (blending with the rest of the 
	        // image in the form of black))
	        imagealphablending($img, false);
			
	        // turning on alpha channel information saving (to ensure the full range 
	        // of transparency is preserved)
	        imagesavealpha($img, true);
			
			// Output the image
			imagepng($img);
			
	        break;
		case "image/gif":
			// Set the content type header - in this case image/gif
			header('Content-Type: image/gif');
			
			// Get image from file
			$img = imagecreatefromgif($imgpath);
			
	        // integer representation of the color black (rgb: 0,0,0)
	        $background = imagecolorallocate($img, 0, 0, 0);
			
	        // removing the black from the placeholder
	        imagecolortransparent($img, $background);
			
			// Output the image
			imagegif($img);
			
			break;
	}
	
	// Free up memory
	imagedestroy($img);
?>
Tags: , , , , ,

2 Comments:

  1. In this example, will links lose functionality when the URL is parsed as a image in the browser?

  2. Could you maybe explain your question a bit more? There aren’t really any links (anchor tags) in the example. If you would want the image in a link you would do so like this:

    <a href="http://www.example.com/?imgurl=/image.png">
    	<img src="http://www.example.com/?imgurl=/image.png" alt="" />
    </a>
    

Comments have been closed for this post. If you would like to ask a question or need help, please post in the forums.