Image fun

Here are some examples of image manipulation using the GD library, more specifically these are pixel operations. Pixel operation meaning doing something to a pixel in an image with regards to this pixel only, not taking into account the neighbours.

An example pixel operation is making a negative image. You take every pixel in an image and substitute it with its opposite color pixel.

OK, so how is this thing working. Pretty simple. I take a PNG image, go through every pixel of this image and call a function passing the pixel as a parameter. The callback function returns a new pixel. I take all returned pixels and create a new image.

Pixel class

To get started I have a pixel class. It simply contains three integer values, the values of red, green and blue the pixel has.

<?php
class Pixel {
    function Pixel($r, $g, $b)
    {
        $this->r = ($r > 255) ? 255 : (($r < 0) ? 0 : (int)($r));
        $this->g = ($g > 255) ? 255 : (($g < 0) ? 0 : (int)($g));
        $this->b = ($b > 255) ? 255 : (($b < 0) ? 0 : (int)($b));
    }
}
?>

This class has only one method, the constructor of the class, which takes care to sanitize the RGB values.

To create a red pixel, you simply do:

<?php
$red = new Pixel(255, 0, 0);
?>

The pixel operations class and main method

Then I have a class that will do the actual operations, I call the Image_PixelOperations. I didn't go through making a nice interface for reading and writing different file formats, I was thinking that this class can be developed further and based on PEAR's Image_Transform, which has tools for opening, validating, displaying, writing image files. What I needed the most was a simple method that opens a PNG, goes through every pixel, calls a function, gets a new pixel and writes the pixel to a new image. Hence, the pixelOperation method:

<?php
class Image_PixelOperations {

    function pixelOperation(
            $input_image,
            $output_image,
            $operation_callback,
            $factor = false
            )
    {

        $image = imagecreatefrompng($input_image);
        $x_dimension = imagesx($image);
        $y_dimension = imagesy($image);
        $new_image = imagecreatetruecolor($x_dimension, $y_dimension);

        if ($operation_callback == 'contrast') {
            $average_luminance = $this->getAverageLuminance($image);
        } else {
            $average_luminance = false;
        }

        for ($x = 0; $x < $x_dimension; $x++) {
            for ($y = 0; $y < $y_dimension; $y++) {

                $rgb = imagecolorat($image, $x, $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;

                $pixel = new Pixel($r, $g, $b);
                $pixel = call_user_func(
                    $operation_callback,
                    $pixel,
                    $factor,
                    $average_luminance
                );

                $color = imagecolorallocate(
                    $image,
                    $pixel->r,
                    $pixel->g,
                    $pixel->b
                );
                imagesetpixel($new_image, $x, $y, $color);
            }

        }

        imagepng($new_image, $output_image);
    }
}

?>

The method takes a filename, it is not doing any validation, it assumes it's a valid PNG file. The second parameter is the output filename. The third is the callback function that will be called on every pixel and the last parameter is any additional parameter we want to pass to the callback function.
The average luminance is something specific to the "contrast" operation, so it's no too important for what pixelOrerations does.

Adding noise

Time for writing the first callback, the addNoise method. Adding noise to an image means adding a random value to each channel of a pixel. (If you're wondering, the value of red in a pixel is called a channel, and so are the blue and the green). Here goes the addNoise implementation.

<?php
    function addNoise($pixel, $factor)
    {
        $random = mt_rand(-$factor, $factor);
        return new Pixel(
                    $pixel->r + $random,
                    $pixel->g + $random,
                    $pixel->b + $random
                );
    }
?>

What we have here is generation of a random value in a user-specified range and adding the random value to the pixel. User-specified range is a number between 0 and 255, where 0 means less noise and 255 means more noise. Well, 0 means no noise and 255 means a lot of noise, 255 is not a boundary, you can go above and the more you go the more you get noise only and pretty much nothing of the original image.

Let's test! I have the simplest of HTML forms:

<form method="get">
    <input name="image" />
    <input type="submit" />
</form>

I specify the image name in the form and submit.

Then if something was submitted I create an object on the pixel operation class:

<?php
if (!empty($_GET['image'])) {

    $po =& new Image_PixelOperations();

}
?>

Then I display the original image, call the pixelOperation method and display the result:

<?php
    echo 'Original: <br /><img src="'. $_GET['image'] .'" />';
    echo '<hr />';

    // noise
    $noise = 100;
    $po->pixelOperation($_GET['image'], 'result_noise.png', array($po, 'addNoise'), $noise);
    echo '<br />Add noise (factor '. $noise .'): <br /><img src="result_noise.png" />';
    echo '<hr />';

?>

The result is:

Adding more or less noise gives the following results where the first one has a noise factor of 20 and the second one, 500:

and

Brightness

Next, adjusting brightness. This is nothing but adding the same integer to each channel of each and every pixel. If we add, we brighten the image, if we substract, we darken the image. The callback method is:

<?php
    function adjustBrightness($pixel, $factor)
    {

        return new Pixel(
                    $pixel->r + $factor,
                    $pixel->g + $factor,
                    $pixel->b + $factor
                );
    }
?>

To test this, we do:

<?php
    $brightness = 50;
    $po->pixelOperation($_GET['image'], 'result_bright.png', array($po, 'adjustBrightness'), $brightness);
    echo '<br />Brighten: <br /><img src="result_bright.png" />';
    $brightness = -50;
    $po->pixelOperation($_GET['image'], 'result_dark.png', array($po, 'adjustBrightness'), $brightness);
    echo '<br />Darken: <br /><img src="result_dark.png" />';
    echo '<hr />';
?>

Which gives us:

and

Swap colors

Next, colors swapping. This means for example take the amount of red in a pixel and replace it with the amount of blue in the same pixel. So there are these possibilities for swapping:

  • RGB to RBG
  • RGB to BGR
  • RGB to BRG
  • RGB to GBR
  • RGB to GRB

The method definition is pretty simple:

<?php
    function swapColors($pixel, $factor)
    {

        switch ($factor) {
            case 'rbg':
                return new Pixel(
                            $pixel->r,
                            $pixel->b,
                            $pixel->g
                        );
                break;
            case 'bgr':
                return new Pixel(
                            $pixel->b,
                            $pixel->g,
                            $pixel->r
                        );
                break;
            case 'brg':
                return new Pixel(
                            $pixel->b,
                            $pixel->r,
                            $pixel->g
                        );
                break;
            case 'gbr':
                return new Pixel(
                            $pixel->g,
                            $pixel->b,
                            $pixel->r
                        );
                break;
            case 'grb':
                return new Pixel(
                            $pixel->g,
                            $pixel->r,
                            $pixel->b
                        );
                break;
            default:
                return $pixel;
        }

    }

?>

Testing the different possibilities gives us:
RGB to RBG:

RGB to BGR:

RGB to BRG:

RGB to GBR:

RGB to GRB:

Removing and boosting colors

Next, we have two more methods. One that sets a channel to zero (for example "no red!") the other one maximizes a channel. Or two channels. So we have six options for each method:

  • Removing (or maximizing) red
  • Removing (or maximizing) green
  • Removing (or maximizing) blue
  • Removing (or maximizing) red and green at the same time
  • Removing (or maximizing) red and blue
  • Removing (or maximizing) green and blue

(It doesn't make much sense to remove all three channels or to maximize them. Why?)

The implementation:

<?php
    function removeColor($pixel, $factor)
    {

        if ($factor == 'r' ) {
            $pixel->r = 0;
        }
        if ($factor == 'g' ) {
            $pixel->g = 0;
        }
        if ($factor == 'b' ) {
            $pixel->b = 0;
        }
        if ($factor == 'rb' || $factor == 'br') {
            $pixel->r = 0;
            $pixel->b = 0;
        }
        if ($factor == 'rg' || $factor == 'gr') {
            $pixel->r = 0;
            $pixel->g = 0;
        }
        if ($factor == 'bg' || $factor == 'gb') {
            $pixel->b = 0;
            $pixel->g = 0;
        }

        return $pixel;
    }

    function maxColor($pixel, $factor)
    {

        if ($factor == 'r' ) {
            $pixel->r = 255;
        }
        if ($factor == 'g' ) {
            $pixel->g = 255;
        }
        if ($factor == 'b' ) {
            $pixel->b = 255;
        }
        if ($factor == 'rb' || $factor == 'br') {
            $pixel->r = 255;
            $pixel->b = 255;
        }
        if ($factor == 'rg' || $factor == 'gr') {
            $pixel->r = 255;
            $pixel->g = 255;
        }
        if ($factor == 'bg' || $factor == 'gb') {
            $pixel->b = 255;
            $pixel->g = 255;
        }

        return $pixel;
    }

?>

And the tests:
Remove red:

Remove green:

Remove blue:

Remove red and green:

Remove green and blue:

Remove red and blue:

Maximize red:

Maximize green:

Maximize blue:

Maximize red and green:

Maximize green and blue:

Maximize red and blue:

Negative

This one is pretty easy, negate the channel. The logic is - you have a lot of red? Nah, I'll use the opposite, less red.

<?php
    function negative($pixel)
    {
        return new Pixel(
                    255 - $pixel->g,
                    255 - $pixel->r,
                    255 - $pixel->b
                );
    }
?>

The test gives us:

Greyscale

I don't know if you know it, but the gray is a color that has equal amounts of R, G and B. Darker shades of gray have a log of R, G and B, the lighter shades have less. To greyscale an image we take the average of the amounts of R, G and B and set the three channels to the average.

<?php
    function greyscale($pixel)
    {

        $pixel_average = ($pixel->r + $pixel->g + $pixel->b) / 3;

        return new Pixel(
                    $pixel_average,
                    $pixel_average,
                    $pixel_average
                );
    }
?>

Test:

Black and White

Unlike greyscale that has shades, B&W has only two colors black (0, 0, 0) and white (255, 255, 255). We use a factor here to determine where the boundary would be, meaning what do you consider black and what white. The simplest logic is that we sum R+G+B and if it's closer to 255+255+255 than it is to 0 (0+0+0), then we call it white, otherwise it is black. Using a factor gives us some more flexibility of drawing the line between black and white (This reflects the subjectivity of real life ;) )

<?php
    function blackAndWhite($pixel, $factor)
    {
        $pixel_total = ($pixel->r + $pixel->g + $pixel->b);

        if ($pixel_total > (((255 + $factor) / 2) * 3)) {
            // white
            $pixel->r = 255;
            $pixel->g = 255;
            $pixel->b = 255;
        } else {
            $pixel->r = 0;
            $pixel->g = 0;
            $pixel->b = 0;
        }

        return $pixel;
    }

?>

Test with factor 20:

Clip

At this point I started hunting the web for ideas for more pixel manipulations (I'm still open, BTW, post a comment with anything you find). I found this clipping thing described somewhere. I'm not sure how useful it is (maybe I didn't get it right). It is in essence removing boundary values and replacing them with pure 0 or 255. So you have (5, 155, 250), this will become (0, 155, 255). Again, there is a factor to give you flexibility in drawing the line. I'm not convinced how useful this is, the only thing I can think of is decreased file size, because the new image uses less colors. Anyway, here's the implementation and the test.

<?php
    function clip($pixel, $factor)
    {
        if ($pixel->r > 255 - $factor) {
            $pixel->r = 255;
        }
        if ($pixel->r < $factor) {
            $pixel->r = 0;
        }
        if ($pixel->g > 255 - $factor) {
            $pixel->g = 255;
        }
        if ($pixel->g < $factor) {
            $pixel->g = 0;
        }
        if ($pixel->b > 255 - $factor) {
            $pixel->b = 255;
        }
        if ($pixel->b < $factor) {
            $pixel->b = 0;
        }

        return $pixel;
    }
?>

Clipping with factor 100:

Adjusting contrast

This is not a pure pixel operation because it takes into account all the pixels in an image in order to decide how to manipulate a given pixel. The contrast adjustment needs the so called average luminance. In order to calculate the average luminance, you need a formula, which I copied, so the implementation is:

<?php
    function getAverageLuminance($image)
    {

        $luminance_running_sum = 0;

        $x_dimension = imagesx($image);
        $y_dimension = imagesy($image);

        for ($x = 0; $x < $x_dimension; $x++) {
            for ($y = 0; $y < $y_dimension; $y++) {

                $rgb = imagecolorat($image, $x, $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;

                $luminance_running_sum += (0.30 * $r) + (0.59 * $g) + (0.11 * $b);

            }

        }

        $total_pixels = $x_dimension * $y_dimension;

        return $luminance_running_sum / $total_pixels;
    }

?>

The actual contrast callback is simple:

<?php
    function contrast($pixel, $factor, $average_luminance)
    {

        return new Pixel(
            $pixel->r * $factor + (1 - $factor) * $average_luminance,
            $pixel->g * $factor + (1 - $factor) * $average_luminance,
            $pixel->b * $factor + (1 - $factor) * $average_luminance
            );
    }
?>

Tests with decreased and increased contrast (factors 0.5 and 1.5):

Salt and Pepper

I got the idea from this page. It's basically sprinkling random white (salt) and black (pepper) pixels. The implementation:

<?php
    function saltAndPepper($pixel, $factor)
    {

        $black = (int)($factor/2 + 1);
        $white = (int)($factor/2 - 1);

        $random = mt_rand(0, $factor);

        $new_channel = false;

        if ($random == $black) {
            $new_channel = 0;
        }
        if ($random == $white) {
            $new_channel = 255;
        }

        if (is_int($new_channel)) {

            return new Pixel($new_channel, $new_channel, $new_channel);

        } else {
            return $pixel;
        }
    }
?>

Test with factor 20:

Gamma correction

<?php
    function gamma($pixel, $factor)
    {

        return new Pixel(
                pow($pixel->r / 255, $factor) * 255,
                pow($pixel->g / 255, $factor) * 255,
                pow($pixel->b / 255, $factor) * 255
            );
    }
?>

Test with factor 2.2:

Randomize

This is just picking a random function from the ones described above a calling it with a random factor. The result? Well, a scientific way to create worst (or better, it's all how you look at it) colorful noise ;)

Next?

I thought of two functions that might be useful. One of them is to snap a pixel to a predefined color. I mean if a pixel is close to something we want, make it closer. This might be useful when for example you want to change the color scheme of your website and you want to alter all images to make them pinkier for example. So far the experiments are going well, but the results ate not too promising ;)

The other function would be to replace a color with another one, while also using proximity. For example take all very very light shades of gray and make them white.

Then there are the filters to do, another set of operations which take into account not only the current pixel but also the neighbours. Examples - blur, edge detect, etc.

BTW, the full source for this posting is here. Extending it is easy, you just need to define a callback function for a new effect and pass it in a call to pixelOperation. I'll be happy if you post the results here.

-----------------

Update form 2005-09-21:

Thanks to Laurens Holst who posted this comment about the sensitivity of the human eye to the different colors! So I created a new greyscaling function using the formula he suggested. In my original function I just take the average of the R, G and B, while now these colors come each with a coeficient. Then I divide by the sum of the coeficients.

The result is better!

Here's the experiment with the same Wiki image (the second greyscale image is using the new formula)

The new image looks just a bit brighter, nothing special. But I tried with another image I found on SitePoint and it looks like the second image is not only brighter but contains more human-eye-readable information. Take a lcoser look at what looks like a display on a cash register. The second greyscale image looks more "informative".

Here's the source of the new greyscale function:

<?php
function greyscale2($pixel)
{

    $pixel_average =
        ( 0.3  * $pixel->r
        + 0.59 * $pixel->g
        + 0.11 * $pixel->b) / (0.3 + 0.59 + 0.11);

    return new Pixel(
                $pixel_average,
                $pixel_average,
                $pixel_average
            );

}
?>

Bookmark and Share

Somewhat related posts

46 Responses to “Image fun”

  1. lleess Says:

    best article I saw! thank you.

  2. Kevin Says:

    This looks great. I dont know that it will help with my 360 panorama stitching but it looks very promising indeed.

  3. Brian Benzinger Says:

    Wow! Excellent post! This is one of the best PHP articles I have seen in a long time! It is amazing what PHP can do :-)

    Thanks! Time to play with images!

  4. Ryan Brooks Says:

    Okay, color me impressed. That’s some pretty click work sir.

    Now, all you have to do is make a web interface for it… ;)

  5. Ryan Brooks Dot Net » Blog Archive » Image fun Says:

    [...] « Stylegala.com Image fun Stoyan had a relatively long post about using the GD library to do basic color modifications to images. I have to admit, th [...]

  6. Cliff H Says:

    Looks great, but why do you need the array in this statement if the callback fn is within the class?:
    $po->pixelOperation($_GET['image'], ‘result_noise.png’, array($po, ‘addNoise’), $noise);

  7. Paul Davey Says:

    You might also look at a Hue-shift function.

    It might be nice to be able to do functions that are not restricted to single pixels - how about a blurring function? It might be a general blur, or perhaps a directional blur (horizontal/vertical).

  8. Laurens Holst Says:

    “To greyscale an image we take the average of the amounts of R, G and B and set the three channels to the average.”

    The human eye more sensitive to green than it is to red or blue. So, simply adding and dividing by three doesn??™t do, instead you should convert by using a formula like x = r*0.3 + g*0.59 + b*0.11 which takes the eye sensitivity in account.

    ~Grauw

  9. phpXperts » Blog Archive » Image fun with PHP Says:

    [...] new pixel. I take all returned pixels and create a new image. For more information visit: Image Fun with PHP Popularity: unranked

    Posted in General |
    Leave a Re [...]

  10. phpied.com » Blog Archive » Image fun Says:

    [...] appy if you post the results here. Update form 2005-09-21: Thanks to Laurens Holst who posted this comment about the sensitivity of the human eye to the different colors! So I created a ne [...]

  11. Stoyan Says:

    Thanks to everybody for the comments and the kind words! Very nice of you guys, thanks!

    @ Cliff H - I use the callback array() because I’m calling each method from outside the class. Basically I did it this way to demo how you can create new custom pixel operations outside the class I provided and still call them.

    @ Paul Davey - Thanks, I’ll look at the the hue shift. As for the blur and other filters, I’ll experiment with them ASAP. BTW PHP5 comes with new GD functions designed exactly for this purpose. There is imagefilter() that can do some predefined filters (as well as some pixel operations like these described above) and then since PHP5.1. there is imageconvolution() that can do custom filters.

    @ Laurens Holst - see the update to the post that takes advantage of the formula you provided. BTW, I saw your site and am now listening to last.fm, thanks! ;)

  12. ?“€?“€ Says:

    ????”™
    ????”™?•???????

  13. Stoyan Says:

    Thanks ?“€?“€ for your comment!

    In what language is it exactly? I tried translating it with online translators and in Chineese simplified it means “Good, good!” or rather “Good is good!” while in traditional Chineese it’s “Not, not!” I was just wondering which one is it ;)

  14. Hi-Tech Says:

    Good job dude. Great article, well explained so easy to follow.

    You’re well started to make a summary of basic pictures traitments and filters. Giving source code doing those effects in PHP is really smart because php is easy to understand, it acts like a pseudo-code for people who would like to code this under another language and it gives the possibility to copy/paste it in php for those who want immediate result.

    Get more effects, structure them in your page and build a library :) You’ll become a reference for sure.

  15. Stoyan Says:

    Thanks Hi-Tech!
    I did continue to experiment with filters and such, just need some time to post.

    I played around with filters - blur, edge detect and the like in PHP, then in C++. While C++ was way faster (as in 30-50 times faster), PHP is sooo much easier and fun to write. So, yeah, you’re absolutely right about the pseudo-code. Can we say that PHP(5) is the new Java for studying programming - OO, stress-free and whatnot ;)

  16. wolandino Says:

    Thank!

  17. phpied.com » Blog Archive » Found in translation Says:

    [...] ading. My post that is showing how to perform pixel operations on an image, using PHP - http://www.phpied.com/image-fun/ The Russian translation This entry was posted [...]

  18. Sheraz Says:

    Excellent article for learning image manipulation in php

  19. Stoyan Says:

    Thanks Sheraz!

  20. ow Says:

    brilliant! excellent! thx :-D

  21. anupam Says:

    Thanks Stoyan. A gr8! article. All my doubts are cleared. Thanks so much.

  22. Jonabeer Says:

    maybe you can try this:
    (these r my photoshop steps)

    -open image (first layer)
    -set to greyscale
    -open image as second layer
    -set second layer to greyscale
    -invert second layer
    -set opacity of second layer to 50%
    (don’t know if this is possible with php, but removing half of the amount of pixels in a “rastershape” can do the trick too)
    -move second layer 1px up en 1px right (depending on the image size moving 2 or more px in each direction may looks nicer)

    this results in a sort of 3d effect in greyscale (bit like a medallion)

    grtz, jonabeer

  23. Kengi Says:

    Great article!
    Though I haven’t got it to work correctly. I end up with just a black output image that is only 1 KB in size. Anyone else experiencing this? Im pretty sure my server has GD installed.
    Thanks

  24. Moh Ali Says:

    Great article!
    Though I haven’t got it to work correctly. I end up with just a black output image that is only 1 KB in size. Anyone else experiencing this? Im pretty sure my server has GD installed.
    Thanks

  25. Trevor Pace Says:

    Hey great article!

    I gotta say it really gave me what I was looking for so good job!

    Thanks!

    (Would really like to see blur and sharpen functions as well as saturation and hue)

  26. ningxiaotao Says:

    I am a beginner of PHP.

    This is a very good article about the image operation.

    Thank you!

  27. Renaud Says:

    Hi ! Very good article for the beginners. However I’m searching for a function to know the brightness of a gif file…is it more clear or more dark ? Do you know if it’s possible ? Best Regards.

  28. Stoyan Says:

    You mean like having two gifs and telling which one is brighter? I’m not sure if there is an easy way, unless of course they are identical in size and you can compare pizel by pixel.

  29. Renaud Says:

    Not, I would like only to know if a GIF (of which I know the size) has a rather clear or rather dark predominance. Thank you.

  30. MarbleMad Says:

    HI, greet PHP script !! But how to create a saturation function ? Thanks

  31. hugetto.homelinux.com » ProgramaciĂł PHP i altres recursos Says:

    [...] Funciones para librerĂ­as GD: funcions per a afegir sorolls a imatges, colorejar, desaturar,etc. utilitzant llibreries GD de PHP [...]

  32. Julien Says:

    It’s a very good article about image in php.
    I have a job which consist in make an edge detect and reinforcement filter (a canny filter) but majority of sites are in PHP4. Do you think is it possible to “create” a canny filter with the help of your php classes?

    I know it would be easier with imageconvolution() or imagefilter() but i can’t use them…
    Thanks.

  33. ZeBrian Says:

    Good article about GD :-)

    Just something should be improved in the greyscale function at the end :

    $pixel_average =
    ( 0.3 * $pixel->r
    + 0.59 * $pixel->g
    + 0.11 * $pixel->b) / (0.3 + 0.59 + 0.11);

    should become :

    $pixel_average =
    0.3 * $pixel->r
    + 0.59 * $pixel->g
    + 0.11 * $pixel->b

    as it is obvious that 0.3 + 0.59 + 0.11 = 1.00 ;-)

  34. Wivern Says:

    One more function :)

    function sepia($pixel)
    {

    return new Pixel(
    round(($pixel->r * 0.393 + $pixel->g * 0.769 + $pixel->b * 0.189) / 1.351),
    round(($pixel->r * 0.349 + $pixel->g * 0.686 + $pixel->b * 0.168) / 1.203),
    round(($pixel->r * 0.272 + $pixel->g * 0.534 + $pixel->b * 0.131) / 2.140)
    );
    }

  35. Wivern Says:

    Here that at me has left with a gradient map:

    $factor = array (
    ‘from’ => array (0,250,0),
    ‘to’ => array (0,200,255),
    ‘reverse’ => false
    );

    $po->pixelOperation($_GET['image'], ‘result.jpg’, array($po, ‘map’), $factor);

    function map($pixel, $factor)
    {

    if (!$factor['reverse'])
    return new Pixel(
    round($pixel->r / 255 * abs($factor['from'][0]-$factor['to'][0]) + min($factor['from'][0],$factor['to'][0])),
    round($pixel->g / 255 * abs($factor['from'][1]-$factor['to'][1]) + min($factor['from'][1],$factor['to'][1])),
    round($pixel->b / 255 * abs($factor['from'][2]-$factor['to'][2]) + min($factor['from'][2],$factor['to'][2]))
    );
    else
    return new Pixel(
    round(max($factor['from'][0],$factor['to'][0]) - $pixel->r / 255 * abs($factor['from'][0]-$factor['to'][0])),
    round(max($factor['from'][1],$factor['to'][1]) - $pixel->g / 255 * abs($factor['from'][1]-$factor['to'][1])),
    round(max($factor['from'][2],$factor['to'][2]) - $pixel->b / 255 * abs($factor['from'][2]-$factor['to'][2]))
    );
    }

    But this script works correctly only with one tone, i.e. with two different colors result strange enough, but interesting. :) And it’s look like Color Overlay

    Help to finish!!!

  36. Nate Fanaro Says:

    great article. thanks!

  37. phpied.com » Blog Archive » Image fun with PHP - part 2 Says:

    [...] Part one of the image fun is here, it contains code that more or less does the same things, but pixel by pixel, which is very slow, but also works in PHP4. At the time of writing part one, imagefilter() funtion was probaly only in cvs, not part of the official PHP. imagefilter() is PHP5-only. Post this entry to: » del.icio.us  » Digg  » Furl  » Newsvine  » reddit  » Y! [...]

  38. Image fun with PHP - part 2 | MT-Soft Website Development Says:

    [...] funtion was probaly only in cvs, not part of the official PHP. imagefilter() is PHP5-only. Incoming Links (via Tecnorati):Nothing Reported Tags: Graphics, PHP, Tutorial Relatedposts [...]

  39. Utilizar funcion en PHP imagefilter() | Sitescripts Says:

    [...] Part one of the image fun is here, it contains code that more or less does the same things, but pixel by pixel, which is very slow, but also works in PHP4. At the time of writing part one, imagefilter() funtion was probaly only in cvs, not part of the official PHP. imagefilter() is PHP5-only. [...]

  40. slikrz Says:

    Wow that’s a great article! It is really helpful for a beginer with graphics in php as i am :) Thanks a lot for your work!

  41. felix Says:

    hi there
    I cant run this code I have this warnning I dont know why
    Warning: call_user_func(Image_PixelOperations::adjustBrightness) [function.call-user-func]: First argument is expected to be a valid callback in /var/www/pasaporteordinario/include/pixelfun.php on line 46

    maybe someone can help with these

    thanks a lot

  42. illuf Says:

    Can any body help me? I need put more layers on background. But not with transparent color, i wana use photoshop/firework function - show pixel from new layer if it is darker than pixel from deeper lyaer.
    How can I do this?
    thanx a lot.

  43. underdog Says:

    I had to change the test code from this:

    $po->pixelOperation($_GET['image'], ‘result_noise.png’, array($po, ‘addNoise’), $noise);

    to this:

    $po->pixelOperation($_GET['image'], ‘result_noise.png’, ‘addNoise’, $noise);

    in order to get it to work. The “$operation_callback” variable was being used without a dereference.

    Another solution would have been to use $operation_callback[1] in call_user_func() thusly:

    $pixel = call_user_func(
    $operation_callback[1],
    $pixel,
    $factor,
    $average_luminance
    );

    Regardless, this is great information! Thank you!

  44. Location vacances Says:

    Thanks for this article about effect images :)

  45. battery Says:

    […] Unfortunately, this isn’t the first time I’ve had this problem. Enter Six Apart. When signed up and tested, I couldn’t leave comments on my own brand new TypePad blog (tdavid.typepad.com). This was around Christmas time two years ago and it took a week or so to find out that the reason my comments were being blocked was because makeyougohmm.com was listed on an old spam service. […]

  46. strony internetowe wrocław Says:

    nice site, many useful and helpful informatiuons, really good work, thx so mutch

Leave a Reply