Assigning all usable colours to an image

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
hagsy62
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Aug 21, 2016 7:15 am

Sun Aug 21, 2016 8:14 am

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":

Code: Select all

<?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: Select all

<?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.

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 163
Joined: Wed Jun 15, 2016 8:35 am

Sun Aug 21, 2016 12:18 pm

At first glance you didn't define :

Code: Select all

$blue = imagecolorallocate($img, 86, 106, 175);

hagsy62
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Aug 21, 2016 7:15 am

Wed Aug 24, 2016 12:43 pm

I appreciate you taking the time to read my question, but I spent a lot of time making sure all the text in my question was clear and that the code was well documented and that everything other people needed in order to answer the question was present. You then start your reply with "At first glance". Please don't glance at the question and then post a short reply - if you had read my question and associated code in full, you would have seen that I've done that step in the "allocateThreeColours" function but it doesn't work.

My original question still stands, can I logically do this and if so, is this not the correct way to do it?

Just to clarify again, I don't want to have to declare every colour and associate it with each image I try to create. Instead, I want to be able to call a function that will associate ALL colours with my image. That way, I can call the function once for each image I'm creating rather than declare 3 colours for image 1, 4 different colours for image 2, and so on. Geddit?

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 163
Joined: Wed Jun 15, 2016 8:35 am

Wed Aug 24, 2016 1:33 pm

When you view the above code, the text is still in black, not blue, as I wanted
When I define $blue, the text will be blue. That was the first glance.

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 163
Joined: Wed Jun 15, 2016 8:35 am

Wed Aug 24, 2016 1:46 pm

Use an include file with all the names as variables :

http://www.rapidtables.com/web/color/RGB_Color.htm

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 163
Joined: Wed Jun 15, 2016 8:35 am

Wed Aug 24, 2016 3:10 pm

This might be what you want.

A color table : colors.php

Code: Select all

<?php

$Black   = array(0,0,0);
$White   = array(255,255,255);
$Red     = array(255,0,0);
$Lime    = array(0,255,0);
$Blue    = array(0,0,255);
$Yellow  = array(255,255,0);
$Cyan    = array(0,255,255);
$Aqua    = array(0,255,255);
$Magenta = array(255,0,255);
$Fuchsia = array(255,0,255);
$Silver  = array(192,192,192);
$Gray    = array(128,128,128);
$Maroon  = array(128,0,0);
$Olive   = array(128,128,0);
$Green   = array(0,128,0);
$Purple  = array(128,0,128);
$Teal    = array(0,128,128);
$Navy    = array(0,0,128);

function color_allocate ($image, $color) {
	return imagecolorallocate ($image, $color[0], $color[1], $color[2]);
}

?>
When you want to assign a color, do this :

Code: Select all

<?php
require_once("colors.php");

// attribute colors
$font_color         =  color_allocate ($img, $Blue);
$padding_color      =  color_allocate ($img, $Gray);
$background_color   =  color_allocate ($img, $Yellow);

?>

hagsy62
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Aug 21, 2016 7:15 am

Sat Nov 19, 2016 1:28 pm

Sorry for the massive delay in replying :oops:

That is exactly what I wanted. Thanks a million.

Post Reply