SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image tinting with the GD library

    Hi,

    I'm trying to set up a range of preset image tints using the GD library. I tried with ImageMagick and only managed to find one function which gave me one tint (using Imagick::sepiaToneImage()) - ended up pretty disappointed with Imagick due to the lack of documentation. Anyway, I've been messing about all day trying to get the right formula for these tints but am still pretty stuck.

    Basically, I want to be able to take any image and allow CMS users to be able to select their desired tint using a radio button. The tint applied has to work with whatever image has been uploaded - this might mean I should convert everything to greyscale before applying the colour tint? The tints I want to apply are really saturations and hues, and I've not been able to get this effect with the code i have below.

    If anyone has experience of doing this type of thing, any help would be appreciated as i'm still trying to get to grip with the GD functions and have a long way to go!

    Here is what I have at the moment:
    PHP Code:
    function img_tint($path_image$red$green$blue)
    {
        
    $im imagecreatefromjpeg($path_image);
        
    $pixel = array();
           
        
    $n_im imagecreatetruecolor(imagesx($im),imagesy($im));
        
    $fond imagecolorallocatealpha($n_im2552552550);
        
    imagefill($n_im00$fond);
       
        for(
    $y=0;$y<imagesy($n_im);$y++)
        {
            for(
    $x=0;$x<imagesx($n_im);$x++)
            {
                
    $rgb imagecolorat($im$x$y);           
                
    $pixel imagecolorsforindex($im$rgb);
               
                
    $r min(round($red*$pixel['red']/169),255);
                
    $g min(round($green*$pixel['green']/169),255);
                
    $b min(round($blue*$pixel['blue']/169),255);
                
    $a $pixel['alpha'];           
               
                
    $pixelcolor imagecolorallocatealpha($n_im$r$g$b$a);
               
                
    imagealphablending($n_imTRUE);
                
    imagesetpixel($n_im$x$y$pixelcolor);
            }
        }
              
    header("Content-type: image/jpeg");
           
    imagejpeg($n_imtime().$path_image.'.jpg');
           
    imagedestroy($n_im);
    }

    //img_tint("neyo.jpg", 167, 59, 62); //too red
    //img_tint("neyo.jpg", 57, 0, 12); //too dark and red
    //img_tint("neyo.jpg", 15, 200, 32); //too green
    //img_tint("neyo.jpg", 15, 100, 32); //bit dark and still too green
    //img_tint("neyo.jpg", 15, 50, 132); //too blue
    //img_tint("neyo.jpg", 15, 10, 32); //far too dark
    //img_tint("neyo.jpg", 35, 30, 62); //still too dark, bit purple
    img_tint("neyo.jpg"155022); 

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,369
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    You could ask a question about Imagick on the Imagemagick forum: http://redux.imagemagick.org/discourse-server/index.php

    I use imagemagick with the command line and php as there are a lot more options.

    Anthony has pages of examples here http://www.imagemagick.org/Usage/ and there must be an example of what you want to do.

  3. #3
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys - you're right i should take this to the ImageMagick forums. @rubble - yes i'll just use ImageMagick with PHP from the command line instead of using the Imagick wrapper

  5. #5
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,369
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    Can you get the examples working from the Imagemagick forum Nicky77?

  6. #6
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Rubble - i actually tried to post a reply on the ImageMagick forum earlier but there was a problem with the site and the page kept going blank.

    Anyway, I played around with fmw's suggestions and those in the usage section on the website and managed to get the range of tints i was looking for. To achieve a consistent tint, i had to convert to greyscale first, before applying the tint and HSB.

    I wasn't too sure if there was a better way of writing the following command though, which i broke down into 2 statements?

    Code:
     exec("convert $original_image -colorspace Gray $new_image; convert $new_image -fill LightGoldenrod2 -tint 100 -modulate 125,75,100 $new_image");

  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Hi, glad to see you've got something working with ImageMagick. Is there any reason why the GD tinting with imagefilter won't work for you?
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,369
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    Supposed to be working so have to be quick!

    Not as easy as it should be but try:
    PHP Code:
    $array=array();
    echo 
    "<pre>";
    exec("convert $original_image -colorspace Gray  miff:- | convert -fill LightGoldenrod2 -tint 100 -modulate 125,75,100 - $new_image 2>&1"$array); 
    echo 
    "<br>".print_r($array)."<br>";
    echo 
    "</pre>"
    I have put some code in for error reporting; you can remove it when everything is working.
    This code may depend on your type of shell.

  9. #9
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Rubble - just tested the code you provided and it achieves the same tint effect! is it more preferable to use the command syntax you provided (using miff) than the 2 sequential statements i had initially? both your suggestion and my code both have the desired effect anyway

    @salathe - i spent a lot of time trying to use the GD library to get the tint effects i was looking for but didn't really come close. I'm sure it probably can be done, i just couldn't get it right so i gave up the ghost and decided to try imagemagick instead.

  10. #10
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,369
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    The code I provided uses a miff format which is an Imagemagick format and no compression which is better than jpg.
    The miff:- saves the image in the memory so no tempory files.
    The | "pipes" the output from the first part of the code to the second; something to do with Linux ?

    Anyway not quite sure exactly how it all works but it does. We should have been able to get the same result with something like:
    PHP Code:
    exec("convert \( $original_image -colorspace Gray \) -fill LightGoldenrod2 -tint 100 -modulate 125,75,100 $new_image"); 
    But that does not work !

  11. #11
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nicky77 View Post
    @salathe - i spent a lot of time trying to use the GD library to get the tint effects i was looking for but didn't really come close. I'm sure it probably can be done, i just couldn't get it right so i gave up the ghost and decided to try imagemagick instead.
    Given the ImageMagick examples post in this thread, it's perfectly reasonable to achieve the same effect through GD. If you're willing to pull back that ghost for a while, you might try something along these lines:

    PHP Code:
    imagefilter($imIMG_FILTER_GRAYSCALE);
    // Apply tint (values range from -255 to +255 for r, g, b and 0 to 127 for a);
    imagefilter($imIMG_FILTER_COLORIZE0, -15, -9030);
    imagefilter($imIMG_FILTER_BRIGHTNESS40);
    imagefilter($imIMG_FILTER_CONTRAST, -20); 
    Salathe
    Software Developer and PHP Manual Author.

  12. #12
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Given the ImageMagick examples post in this thread, it's perfectly reasonable to achieve the same effect through GD. If you're willing to pull back that ghost for a while, you might try something along these lines:

    PHP Code:
    imagefilter($imIMG_FILTER_GRAYSCALE);
    // Apply tint (values range from -255 to +255 for r, g, b and 0 to 127 for a);
    imagefilter($imIMG_FILTER_COLORIZE0, -15, -9030);
    imagefilter($imIMG_FILTER_BRIGHTNESS40);
    imagefilter($imIMG_FILTER_CONTRAST, -20); 
    @Salathe - this works perfectly too! I'll probably stick with the ImageMagick code i have which is working fine and seeing as i now have ImageMagick installed on my server. But it's good to know how to achieve this effect with GD too, in case i ever need to do this again on a server where i can't install ImageMagick. Many thanks to you and Rubble for your help

  13. #13
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what is the version of ImageMagick that you have?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •