communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "image editing" - download "colorizing"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

image editing - colorizing was created/last modified on Mon 06 Oct 2003 7:08 PM

Colorizing

Colorizing images is fairly easy to do. The easiest way to colorize an image is fairly simple to grasp. Create an image of the same dimensions and fill that image with the color you want to change it to. This new image is then placed on top of the older image, giving it a colorized look.

<?php
function imagecolorize (& $im ,& $col ,$pct )
{
    
// Get the image's width
    
$im_w = imagesx ($im );
    
// Get the image's height
    
$im_h = imagesy ($im );
    
// Set a pixel with the color, so we can get it easily
    
$setpixel = imagesetpixel ($im ,$im_w ,0,$col );
    
// Get the color
    
$index = imagecolorat ($im ,$im_w ,0);
    
// Find the color in the index
    
$rgb = imagecolorsforindex ($im ,$index );
    
// Get the red value
    
$r = $rgb ["red" ];
    
// Get the green value
    
$g = $rgb ["green" ];
    
// Get the blue value
    
$b = $rgb ["blue" ];
    
// Create the layover
    
$layover = imagecreate ($im_w ,$im_h );
    
// Allocate the color on this image
    
$color = imagecolorallocate ($layover ,$r ,$g ,$b );
    
// Fill the image with the new color (this really isn't needed)
    
$fill = imagefill ($layover ,0,0,$color );
    
// Merge the layover on top of the older image
    
$merge = imagecopymerge ($im ,$layover ,0,0,0,0,$im_w ,$im_h ,$pct );
    
imagedestroy ($layover ); // Destroy the layover
}
?>

learn more about some of the PHP functions used in this snippet: copy, for, function, imagecolorallocate, imagecolorat, imagecolorsforindex, imagecopy, imagecopymerge, imagecreate, imagedestroy, imagefill, imagesetpixel, imagesx, imagesy

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

go to PHParadise to get free PHP, Javascript and FlashMX code