Tuesday, 15 October 2013

PHP file to return image or return 1x1 pixel image

Using php file we can return image .  there are many benefits of this thing . we can keep count that how many times our image is fetched or from which us/ip that image is fetched.also we can change image from php code without changing in the real html file .

see following code

<img src="http://localhost/test.php"/>

if we want that test.php should return any image
following is the code snippet to return that image

test.php :-

$file = file_get_contents("full path to ur image fle");
$contenttype = "gif" ; // it is type or extension of image like jpeg for jpeg images and gif for .gif images
header("Content-type: image/$contenttype");
echo $file;

we need to  take care of some thing in this .
we should not print or echo any other variable or string from the same php file .then it will be showing some error and image will not be returned .

IF we want to return 1x1 image pixel then following is the code snippet .

header('Content-Type: image/gif');
header('Content-Length: 43');

this code will provide us 1x1 image 

No comments:

Post a Comment