Php-mysql Course

Here is how to create an image in PHP with background in two colors. First create a background for the image, with imagefill() function, then draw a rectangle on the half of the image, with imagefilledrectangle() function.
Three examples are presented in this page, with the colors for background displayed: horizontally, vertically, and diagonally (on half-diagonal).

1. - Example, PNG image with background in red and white, horizontally. The image is displayed directly in browser.
<?php
// https://coursesweb.net/php-mysql/

// Create a 160x80 image
$width = 160;
$height = 80;
$im = imagecreatetruecolor($width, $height);

// sets red color for background
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);

// sets and draw a white rectangle
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, $height/2, $width, $width/2, $white);

// sets and adds a text
$text = 'CoursesWeb.net';
$text_color = imagecolorallocate($im, 0, 1, 255);
imagestring($im, 5, 12, $height/3.3, $text, $text_color);

// Display directly the image
header('Content-Type: image/png');
imagepng($im);
?>
Results:
PHP image in two colors horizontally
2. - Another example, PNG image with background in two colors, vertically. The image is saved on server.
<?php
// https://coursesweb.net/php-mysql/

// Create a 160x80 image
$width = 160;
$height = 80;
$im = imagecreatetruecolor($width, $height);

// sets a color for background
$red = imagecolorallocate($im, 80, 120, 250);
imagefill($im, 0, 0, $red);

// sets and draw a green rectangle in left half
$white = imagecolorallocate($im, 0, 245, 1);
imagefilledrectangle($im, 0, 0, $width/2, $height, $white);

// sets and adds a red text
$text = 'CoursesWeb.net';
$text_color = imagecolorallocate($im, 225, 0, 1);
imagestring($im, 5, 12, $height/3, $text, $text_color);

// Saves the image in 'imgs' folder
imagepng($im, 'addons/php-mysql/image.png');
?>
Results:
PHP image in two colors vertically
3. To create the background with two colors on half-diagonal, draw a triangle on the image, on the half-diagonal, with the imagefilledpolygon() function.
- Example:
<?php
// https://coursesweb.net/

// Create a 160x80 image
$width = 160;
$height = 80;
$im = imagecreatetruecolor($width, $height);

// sets red color for background
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);

// sets and draw a green triangle on the half-diagonal
$white = imagecolorallocate($im, 8, 235, 9);
imagefilledpolygon($im, array(0, 0, 0, $height, $width, $height), 3, $white);

// sets and adds a text
$text = 'CoursesWeb.net';
$text_color = imagecolorallocate($im, 0, 1, 255);
imagestring($im, 5, 12, $height/3.3, $text, $text_color);

// Saves the image in 'imgs' folder
imagepng($im, 'addons/php-mysql/image.png');
?>
Results:
PHP image in two colors diagonally

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
  font-family:"Calibri",sans-serif;
}
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
alert(nr_elms);
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr =[1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
Image in PHP with background in two colors

Last accessed pages

  1. Get and Modify content of an Iframe (32494)
  2. The Mastery of Love (7690)
  3. Download HTM resources (90)
  4. HTML5 Quick Tutorial (5041)
  5. Styling HTML table with CSS (412)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (715)
  2. CSS cursor property - Custom Cursors (94)
  3. Read Excel file data in PHP - PhpExcelReader (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (67)
  5. PHP Unzipper - Extract Zip, Rar Archives (53)