Archive

Posts Tagged ‘image’

PHP Script To Turn Image Into ASCII Text

October 6th, 2008 No comments

Use the following snippet to convert any jpeg image into the equivalent image in ASCII format. It works by loading an image using the PHP GD2 library function ImageCreateFromJpeg() and then figures out the height and width of it. It then uses these values to loop through every pixel in the image and figures out the colour of that pixel. It uses this value to create a <span> element that uses the text colour of a # to change the colour of the text.

An additional time (and space) saver for this function is that it detects any pixels that are just off white and simply displays a &nbsp; character instead.

$img = ImageCreateFromJpeg('logo.jpg');
 
// get height and with of the image.
$width = imagesx($img);
$height = imagesy($img);
 
echo '<pre style="font-size:1px;">';
for($h=0;$h<$height;$h++){
 for($w=0;$w<=$width;$w++){
  if($w == $width){
   echo '<br>';
   continue;
  }
  // get image at pixel location
  $rgb = ImageColorAt($img, $w, $h);
  // convert colour into usable format
  $r = ($rgb >> 16) & 0xFF;
  $g = ($rgb >> 8 ) & 0xFF;
  $b = $rgb & 0xFF;
  // check for white/off-white colour
  if($r > 200 && $g > 200 && $b > 200){
   echo '&nbsp';
  }else{
   echo '<span  style="color:rgb('.$r.','.$g.','.$b.');">#</span>';
  }
 }
}
echo '</pre>';

As an example, take the following, quite recognisable, image.

This is transformed into the text on the following page.

The Google logo in ASCII format

Beware that although this works will for small image sizes if you try to convert a very large image you will find that the script takes a long time. This is because it looks at every pixel in turn and converts the colour into something usable, so if there are a lot of pixels it takes a long time to look at every one. Not only this, but you will also find that the size of the page generated will grow significantly due to all of the span elements being created.

Categories: PHP Tags: , , , , ,

Add Effects To Images Using Image Filters With PHP

July 24th, 2008 2 comments

The imagecreatefromjpeg() function has been part of the PHP library since version 4, allowing programmers to load an image directly from a file. However, the imagefilter() has only been available since version 5 and adds a nice little set of effects to an already useful function.

The effects are created using the function in conjunction with a set of constants. Take the following image taken from the free stock images site.

Default Electronics image

I chose this images because it has lots of colour and will show the techniques working well. To create the effects you need to use the following bit of code, in this case I am using the negative filter.

header("content-type: image/png");
$image = imagecreatefromjpeg("tech03250023.jpg");
imagefilter($image,IMG_FILTER_NEGATE);
imagepng($image);
imagedestroy($image);

You can also use imagecreatefromgif() and imagecreatefrompng(), but I have found mixed results with these functions. I think this is because it is possible to create a custom pallets for these formats, which causes some of the filters not to work properly. It isn’t possible to get a custom pallet for a JPG image, so they are probably the best image format to use.

Here is a list of the filters that you can use with this function. In each case the code used to generate the image and the image produced are displayed.

IMG_FILTER_NEGATE : This filter reverses all of the colours in the image, making it look like a photograph negative.
imagefilter($image,IMG_FILTER_NEGATE);
Default Electronics image IMG_FILTER_NEGATE

IMG_FILTER_GRAYSCALE :
This converts the image to black and white.
imagefilter($image,IMG_FILTER_GRAYSCALE);
Default Electronics image IMG_FILTER_GRAYSCALE

IMG_FILTER_BRIGHTNESS :
This changes the brightness of the image. In this instance the imagefilter() function takes an additional 3rd parameter, which is the level of brightness. This number ranges from -255 (black) to 255 (white). Any number higher or lower than this would cause the filter to be ignored.
imagefilter($image,IMG_FILTER_GRAYSCALE,150);
Default Electronics image IMG_FILTER_BRIGHTNESS

IMG_FILTER_CONTRAST :
Changes the contrast of the image. In this case the imagefilter() function takes an additional 3rd parameter, which is the level of contrast, much like the IMG_FILTER_BRIGHTNESS filter.
imagefilter($image,IMG_FILTER_CONTRAST,150);
Default Electronics image IMG_FILTER_CONTRAST

IMG_FILTER_COLORIZE :
With this filter you can alter the saturation of a colour within your image. The function takes an additional 3 parameters, which are the level of red, blue and green, in that order. Each level goes from 0 to 255.
imagefilter($image,IMG_FILTER_COLORIZE, 0, 255, 0);
Default Electronics image IMG_FILTER_COLORIZE

IMG_FILTER_EDGEDETECT :
This filter uses edge detection to highlight the edges in the image.
imagefilter($image,IMG_FILTER_EDGEDETECT);
Default Electronics image IMG_FILTER_EDGEDETECT

IMG_FILTER_EMBOSS :
Adds an emboss to the image.
imagefilter($image,IMG_FILTER_EMBOSS);
Default Electronics image IMG_FILTER_EMBOSS

IMG_FILTER_SELECTIVE_BLUR :
Blurs the image using a non-gaussian method.
imagefilter($image,IMG_FILTER_SELECTIVE_BLUR);
Default Electronics image IMG_FILTER_SELECTIVE_BLUR

IMG_FILTER_GAUSSIAN_BLUR :
This adds a gaussian blur to the image.
imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR);
Default Electronics image IMG_FILTER_GAUSSIAN_BLUR

IMG_FILTER_MEAN_REMOVAL :
Uses mean removal to achieve a "sketchy" effect.
imagefilter($image,IMG_FILTER_MEAN_REMOVAL);
Default Electronics image IMG_FILTER_MEAN_REMOVAL

IMG_FILTER_SMOOTH :
This runs a smooth filter over the image. The additional parameter in this case is the level of smoothing.
imagefilter($image,IMG_FILTER_SMOOTH,50);
Default Electronics image IMG_FILTER_SMOOTH

Combinations
It is also possible to combine the effects together and layer filters on top of each other. The image below is created through a combination of different filters.

imagefilter($im,IMG_FILTER_BRIGHTNESS,150);
imagefilter($im,IMG_FILTER_EMBOSS);
imagefilter($im,IMG_FILTER_BRIGHTNESS,150);
imagefilter($im,IMG_FILTER_GAUSSIAN_BLUR);
imagefilter($im,IMG_FILTER_SELECTIVE_BLUR);
imagefilter($im,IMG_FILTER_MEAN_REMOVAL);
imagefilter($im,IMG_FILTER_SMOOTH,50);

Default Electronics image Combination

You might not see much difference with the blur filters, but I think it matters what image is chosen for it. The image selected for this test is slightly blurred anyway, so adding blur to this seems not to do very much.

Categories: PHP Tags: , , , , ,

Preventing Image Bandwidth Theft With .htaccess

April 21st, 2008 No comments

When people link to your images from their own site they are essentially using your bandwidth to show images on their site, this is also known as hotlinking.

The simplest way of preventing people from doing this is to add a .htaccess file to only allow locally linked images to be served. This checks the domain that is linking to your images by using the referrer and if the domain does not equal you own site then a different image is served, in this case blank.jpg.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?talkincode\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?google\.co\.uk/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?google\.com/ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/blank.jpg [L]

You can also prevent hotlinking from high traffic sites like myspace by using the following .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myspace\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?blogspot\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?livejournal\.com/ [NC]
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/blank.jpg [L]

Instead of returning a blank image you could produce a 403 Forbidden error by using the F RewriteRule flag.

RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

Another way is to use the mod_setenvif module to figure out what the referrer name is. You will first need to go into your Apache httpd.conf file and make sure that the mod_setenvif module is enabled.
LoadModule setenvif_module modules/mod_setenvif.so
If it isn’t enabled then uncomment the line and restart Apache. This module is normally turned on by default so it should be enabled on most hosts.

Next, upload the following .htaccess file to your root directory, replacing the domain name with your own.

SetEnvIfNoCase Referer "^http://www.talkincode.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://www.talkincode.com$" locally_linked=1
SetEnvIfNoCase Referer "^http://talkincode.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://talkincode.com$" locally_linked=1
SetEnvIfNoCase Referer "^$" locally_linked=1
<FilesMatch "\.(bmp|gif|png|jpe?g)$">
 Order Allow,Deny
 Allow from env=locally_linked
</FilesMatch>

This method simply stops the image being served, rather than presenting a different image.

However, there is nothing you can do to stop people downloading images from your site and using them on their own site. If your images are copyrighted in anyway then you will need to contact the site directly to get them to remove your images.

Categories: Apache Tags: , , ,

Capture A Website As Image With PHP

April 7th, 2008 1 comment

One thing that comes in useful when linking to other websites is to use a little thumbnail of the site as part of the link. Although it is possible to do this it usually involves having command line access to the server so that you can install various different programs and extensions.

The simplest mechanism accomplish this is to use third party sites to create the thumbnail for you. Here are a few examples of free thumbnail services.

www.thumbshots.org

Thumbshots provides access to small images (thumbnail size) of many different websites. However, they are very small, with no way to change this, and can often be weeks out of date if the site has had a redesign. You can access Thumbshots from any webpage by using an image with a source that reads from the Thumbshots website.

<img src="http://open.thumbshots.org/image.pxf?url=http%3A%2F%2Fwww.talkincode.com%2F" alt="Site image" />

This produces the following image for Talk In Code.
Site image

Websnapr

Websnapr is a slightly more customisable system as you can select from four different image sizes. These are t for thumbnail, s for small, m for medium and l for large.

Here is an example code for a small image of Talk in Code.

<img src="http://images.websnapr.com/?url=http%3A%2F%2Fwww.talkincode.com%2F&size=s" alt="Site image" />

This produces the following image.

Site image

Art Viper

Art Viper website thumbnails is a more complicated mechanism of getting images of websites. Here is a list of the parameters you can pass to the script.

  1. q: The quality of the image, about 90 seems like a good bet.
  2. h: The hight of the image.
  3. w: The width of the image.
  4. sdx: The width of the rendering browser in pixels.
  5. sdy: The height of the rendering browser in pixels.
  6. url: The URL that the image is to be created from.

So putting these things together you can create the following source.

<img src="http://www.artviper.net/screenshots/screener.php?url=http%3A%2F%2Fwww.talkincode.com%2F&q=90&h=280&w=340&sdx=1024&sdy=768" alt="Site image" />

This produces the following image.

Site image

Saving The Images

Printing off images from other peoples websites is good, but it can lead to certain sections of the site slowing down as the browser goes off to fetch the image from a different server. The ideal situation is to save the image to your server and then link to it from there.

Take any of the above examples. It is possible to extract the information from the images served and store them on your own site. This can be accomplished with curl using the following code.

$url = urlencode('http://www.talkincode.com/');
$image_url = 'http://images.websnapr.com/?url='.$url.'&size=s';
 
$ch = curl_init();
$timeout = 0;
curl_setopt($ch,CURLOPT_URL,$image_url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
 
// Getting binary data
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);
 
$image = curl_exec($ch);
curl_close($ch);
 
// output to browser
header("Content-type: image/jpeg");
$imageFile=fopen('websiteimage.jpg','w');
fwrite($imageFile,$image);
print $image;

This saves the image and then prints it out.

Categories: PHP Tags: , , , , ,

PHP Easter Eggs

March 26th, 2008 No comments

There are a couple of hidden features that you can see on just about any PHP driven website. You can get either an image or a list of credits for PHP by appending one of the following strings to the end of the URL.

?=PHPE9568F34-D428-11d2-A769-00AA001ACF42
?=PHPE9568F35-D428-11d2-A769-00AA001ACF42
?=PHPE9568F36-D428-11d2-A769-00AA001ACF42
?=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000

Here is a quick explanation of each code.

  • PHPE9568F34-D428-11d2-A769-00AA001ACF42 : Produces a standard PHP logo, defined by PHP_LOGO_GUID. Can also be accessed by the function call php_logo_guid(), which returns this number.
  • PHPE9568F35-D428-11d2-A769-00AA001ACF42 : Produces a Zend engine logo, defined by ZEND_LOGO_GUID. Can also be accessed by the function call zend_logo_guid(), which returns this number.
  • PHPE9568F36-D428-11d2-A769-00AA001ACF42 : Produces the easter egg, defined by PHP_EGG_LOGO_GUID. This image varies between PHP versions and operating systems but you will see either:
    • A crayon drawing of the PHP logo
    • A picture of a dog
    • A picture of a rabbit.
    • A picture of a guy with a pencil up each nostril.
  • PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000 : A list of credits for PHP, defined by PHP_CREDITS_GUID. This list can also be accessed by calling the function phpcredits(), which works in the same way as phpinfo().

This works on the Talk In Code website, so for your convenience I have put the eggs into a list of links.

These don’t work on all websites as the PHP team have been sensible enough to not allow these things to show when the expose_php directive in the php.ini file it set to Off.

Categories: PHP Tags: , , , , , ,