PHP Colorize an image using GD


Hits: 3298  

I was working for color customization for HTML5 MP3 Player with Playlist

Finally, I have time saving code for you PHP Colorize an image using GD

<?php

//print_r(html2rgb("#ffffff"));
//echo rgb2html(137, 113, 79); 

$color=isset($_REQUEST['color'])?$_REQUEST['color']:"ff00ff";
$image=isset($_REQUEST['image'])?$_REQUEST['image']:"2.jpg";

$rgb= html2rgb("#".$color);

$imgname=$image;
         
    $im=imagecreatefromjpeg($imgname);
    if($im&& imagefilter($im, IMG_FILTER_COLORIZE,$rgb[0],$rgb[1],$rgb[2]))
    {
         
         header("Content-Type: image/jpeg");
         imagejpeg($im);
         
         //imagejpeg($im, 'output.jpg'); 
         //imagedestroy($im); 
    }
   else
   {
        echo'Processing failed.';
   }
   

function rgb2html($r,$g=-1,$b=-1)
{
    if(is_array($r)&&sizeof($r)==3)
        list($r,$g,$b)=$r;

    $r=intval($r);$g=intval($g);
    $b=intval($b);

    $r=dechex($r<0?0:($r>255?255:$r));
    $g=dechex($g<0?0:($g>255?255:$g));
    $b=dechex($b<0?0:($b>255?255:$b));

    $color=(strlen($r)<2?'0':'').$r;
    $color.=(strlen($g)<2?'0':'').$g;
    $color.=(strlen($b)<2?'0':'').$b;
    return'#'.$color;
}

function html2rgb($color)
{
    if($color[0]=='#')
        $color=substr($color,1);

    if(strlen($color)==6)
        list($r,$g,$b)=array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]);
    elseif(strlen($color)==3)
        list($r,$g,$b)=array($color[0].$color[0],$color[1].$color[1],$color[2].$color[2]);
    else
        returnfalse;

    $r=hexdec($r);$g=hexdec($g);$b=hexdec($b);

    returnarray($r,$g,$b);
}

   
   
?>

The above code will work for JPG image files, you can optimize for other image formats 😉

=>

Published by

Sandeep Verma

I’m an Entrepreneur. I’m proud to work as Blogger, LAMP Programmer, Linux Admin, Web Consultant, Cloud Manager, Apps Developer, Searcher. Concentrate > Observe > Imagine > Launch

18 thoughts on “PHP Colorize an image using GD”

  1. I like the valuable info you provide in your articles.
    I will bookmark your blog and check again here regularly.

    I’m quite sure I’ll learn lots of new stuff right here!
    Best of luck for the next!

  2. Ahaa, its pleasant conversation regarding
    this article here at this blog, I have read all that, so at this time me also commenting at
    this place.

  3. We are a group of volunteers and starting a new scheme in our
    community. Your site provided us with valuable info to work
    on. You have done a formidable job and our whole community will be grateful to
    you.

  4. Undeniably believe that which you stated.
    Your favorite reason appeared to be on the net the easiest thing to
    be aware of. I say to you, I certainly get irked
    while people think about worries that they plainly don’t know about.
    You managed to hit the nail upon the top and also defined out
    the whole thing without having side effect ,
    people can take a signal. Will likely be back to get more.
    Thanks

  5. Hello there! This is my 1st comment here so I just wanted to give a quick
    shout out and say I genuinely enjoy reading your posts.
    Can you suggest any other blogs/websites/forums that
    deal with the same topics? Appreciate it!

  6. I have been browsing online more than 2 hours today, yet I never found any
    interesting article like yours. It’s pretty worth enough
    for me. In my view, if all website owners and bloggers made
    good content as you did, the web will be much more useful than ever before.

  7. Howdy! Do you know if they make any plugins to help with
    SEO? I’m trying to get my blog to rank for some targeted keywords but I’m
    not seeing very good results. If you know of any please share.
    Many thanks!

  8. Thank you a bunch for sharing this with all of us you really recognise
    what you are speaking about! Bookmarked. Please additionally seek advice from my web site
    =). We could have a hyperlink change contract between us

  9. Hiya! Quick question that’s totally off topic. Do you know how to make your site mobile
    friendly? My website looks weird when viewing from
    my iphone4. I’m trying to find a template or plugin that might
    be able to resolve this problem. If you have any suggestions, please share.
    Cheers!

  10. Thank you a lot for sharing this with all of us you
    really realize what you are speaking about! Bookmarked.
    Please additionally consult with my web site =). We could have a link exchange contract between us

  11. Today, I went to the beach with my kids. I found a sea shell
    and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She
    placed the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

  12. Howdy! I could have sworn I’ve been to this blog before
    but after browsing through some of the posts I realized it’s new to me.
    Anyways, I’m certainly pleased I stumbled upon it
    and I’ll be book-marking it and checking back frequently!

Leave a Reply

Your email address will not be published.

*