BigToach.com

PHP Snippets

colorallocate

Information

(PHP Version >= 3.0.6)
Functions used in this snippet: str_replace, strlen, list, sscanf, imagecolorallocate.

Description

int colorallocate( str color, mixed img )

Takes the HEX color color and turns it into a color resource for the image img

Snippet

<?php
function colorallocate($color$img){
    
$color str_replace('#','',$color);
    
$color strlen($color) === $color{0} . $color{0} . $color{1} . $color{1} . $color{2} . $color{2} : $color
    list(
$r$g$b) = sscanf($color"%2x%2x%2x");
    return 
imagecolorallocate($img$r$g$b);
}
?>

Example

colorallocate Can be used in the following way:

<?php
$img 
imagecreate(50,50); // create a image

$black colorallocate('#000000'$img); // create the color black

header('Content-type: image/jpeg'); // set image header

imagefill($img$black); // fill the image with black

imagejpeg($img); // send the image to the browser


// another quick example


$img imagecreate(50,50); // make an image

$red colorallocate('F00'$img); // make the color red

imagefill($img$red); // fill the image with red

header('Content-type: image/png'); // set header

imagepng($img); // send the image to browser
?>

Added on Jul 5th at 8 pm by Scott - 0 Comments


0 Responses to colorallocate

Name:

Site or Mail:

Comment:

To try to reduce the ammount of spam, please enter the word GOOSE in the following form

Security Code: