A
Anonymous
Guest
When creating images, I don't want to have to assign every colour I'm going to use to every image I'm creating each time I create a new image.
For example, the following code creates a small text box with the text "Image A":
The code for allocateThreeColours.php is...
My question is, can't I use a function something like " allocateThreeColours($img);" to allocate as many colours as I need to each image I'm creating? When you view the above code, the text is still in black, not blue, as I wanted. If the method is sound but the PHP is wrong, please could somebody correct it?
Thanks in advance.
For example, the following code creates a small text box with the text "Image A":
Code:
<?php
// some settings
$angle = 90;
$text = ' Image A ';
$font_face = 'arial.ttf';
$font_size = 20; //(int) pixels in GD 1, or points in GD 2
// retrieves box frame
$box = imagettfbbox($font_size, 0, $font_face, $text);
$bottom_left_x = $box[0]; // used below
$bottom_left_y = $box[1]; // used below
$bottom_right_x = $box[2]; // used below
$bottom_right_y = $box[3];
$top_right_x = $box[4];
$top_right_y = $box[5];
$top_left_x = $box[6];
$top_left_y = $box[7]; // used below
// define width and height of the text box
$box_w = abs($bottom_left_x) + abs($bottom_right_x);
$box_h = abs($bottom_left_y) + abs($top_left_y);
// add padding
$padding_x = 2;
$padding_y = 2;
$box_w = $box_w + 2 * $padding_x;
$box_h = $box_h + 2 * $padding_y;
// origin of the text = baseline of the first char
$text_x = abs($bottom_left_x) -1 + $padding_x;
$text_y = $box_h -1 - abs($bottom_left_y) - $padding_y;
// create the image
$img = imagecreatetruecolor($box_w, $box_h);
// define some colors
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
$lightgrey = imagecolorallocate($img, 200, 200, 200);
$grey = imagecolorallocate($img,100,100,100);
$yellow = imagecolorallocate($img, 0xFF, 0xFF, 0x00);
require_once("allocateThreeColours.php");
allocateThreeColours($img);
// attribute colors
$font_color = $blue;
$padding_color = $lightgrey;
$background_color = $yellow;
// fill image with background color
imagefill($img, 0, 0, $background_color);
// fill image with padding color
imagefilledrectangle($img, 0, 0, $box_w, $padding_y - 1, $padding_color); // top
imagefilledrectangle($img, 0, 0, $padding_x - 1, $box_h - 1, $padding_color); // left
imagefilledrectangle($img, 0, $box_h - $padding_y -1, $box_w, $box_h - 1, $padding_color); // bottom
imagefilledrectangle($img, $box_w - $padding_x, 0, $box_w - 1, $box_h - 1, $padding_color); // right
//write text
imagettftext($img, $font_size, 0, $text_x, $text_y, $font_color, $font_face, $text);
//rotate image
if ($angle > 0)
{
$img = imagerotate($img, $angle, $white);
}
// send header
header("Content-type: image/gif");
//sends image
imagegif($img);
imagedestroy($img);
?>
The code for allocateThreeColours.php is...
Code:
<?php
function allocateThreeColours($image)
{
// This function turns colour names into their actual values
// on the Red-Green-Blue scale.
function color_allocate ($image2, $color)
{
switch ($color)
{
case "black":
$red = 0;
$green = 0;
$blue = 0;
break;
case "blue":
$red = 51;
$green = 51;
$blue = 255;
break;
case "yellow":
$red = 255;
$green = 255;
$blue = 51;
break;
}
return imagecolorallocate ($image2, $red, $green, $blue);
}
// create an array of all the colours you are going to need
// (the ones specified above) in alphabetical order...
$kolours = array(0 => 'black',
'blue',
'yellow'
);
$image2 = $image;
// assign all of those colours to the image...
foreach($kolours as $key => $value)
{
color_allocate($image2, $value);
}
}
?>
My question is, can't I use a function something like " allocateThreeColours($img);" to allocate as many colours as I need to each image I'm creating? When you view the above code, the text is still in black, not blue, as I wanted. If the method is sound but the PHP is wrong, please could somebody correct it?
Thanks in advance.