Useful functions

Checking the statistics of the blog, I noticed the user were searching for some information I’ve put time ago.

As all my information were a mess, I decide to delete all the old posts and start again (sorry for that).

I’ve noticed the people were looking for two similar things:

  1. Convert between image formats in PHP
  2. Desaturate an image (and other functions)

I will talk on this and create a new category: Code Snippets

Convert between image formats in PHP (converting png images into jpeg, gif)

First you need the php-gd extension installed, if you write:

You need to search something like this:
PHP GD Installation

With this, you will know you have php-gd2 (version 2 is surely better and the php5 version of php-gd2 seems to be better with new filters).
You also know with this, you can save on GIF, JPG, PNG, WBMP and XBM, and also you can handle Freetype Fonts (TTF)

To convert a file is really easy:

$image_png = "your_image.png";
$image_output = "your_output_image.jpg";
$image = imagecreatefrompng($image_png);
imagejpeg($image, $image_output);

So simple!, if you want to open a jpeg and save into gif, you need only this:

$image_jpeg = "your_image.jpg";
$image_output = "your_output_image.gif";
$image = imagecreatefromjpeg($image_jpeg );
imagegif($image, $image_output);

Easy, all the php-gd2 which handle with formats ends with the format type (jpeg, gif, png, wbmp, etc).

More information:

Desaturate Image

One of the most common changes to an image is to set as grayscale, to make this, you need to convert each possible (256^3 colors) into a 256 color (which is the grayscale).
In fact, if you put the same color into the three channels (RGB) you will have a grayscale image.

To make this, there are usual paths:

  1. Set the color to the average of the three colors: (R + G + B) / 3
  2. Convert through decimals to each color, for example: R*0.299 + G*0.587 + B*0.114, you need to assure the sum of the decimals make one, or you are going to have black “holes” (0.299 + 0.587 + 0.114 = 1.0)
  3. Desaturate, the RGB model is a computer-friendly model; but the classical model for colors is HSV which means: Hue, Saturation, Value.
    In this model, you have a cone, like the image of below, the hue is the “color” in fact (a 360 continous color), the Saturation means how much pigmented will be the color (0 means no pigment and 1.0 means full pigment) and the value is how much light the color will have (0 means black and 1 means white).
    So Desaturate in fact is eliminate the saturation value, since it represents the pigmentation, to achieve this you need to convert into HSV model, make 0 to Saturation value, and convert again into RGB model and save.

HSV Cone Model

The most used is option 2), since a good example is the Photoshop Mix Channels filter, you can make get your own values to mix the colors from Photoshop.

To achieve this in PHP, I’ve found a good way in posted by

function imagetograyscale($im)
if (imageistruecolor($im)) {
imagetruecolortopalette($im, false, 256);

for ($c = 0; $c < imagecolorstotal($im); $c++) {
$col = imagecolorsforindex($im, $c);
$gray = round(0.299 * $col['red'] + 0.587 * $col['green'] + 0.114 * $col['blue']);
imagecolorset($im, $c, $gray, $gray, $gray);

What this filter does is simple convert each color in the image for the new gray value, in a traditional way, you need to go to each pixel and convert to the new color.

In a later post I will tell you how to change to HSV model and how to perform really desaturation.

More Information:
HSV Model: