Php-mysql Course

In this page it is presented a PHP function that can be used to create simple JPEG images with PHP, with text displayed on multiple lines.
- This function (named textToImg() can receive 4 arguments: the text string, the image width (in pixels), background color, and text color. The last 2 parameters are optionals, they are by default: blue, and a green color.
Here's the code of the function:
// Function to create images with PHP, with text on new lines
// Receives the text-string, image-width (in pixels), and optional: arrays with RGB color for background-color, text-color
function textToImg($text, $image_width, $colour = array(0,1,244), $background = array(130,200,150)) {
  $font = 5;
  $line_height = 15;
  $padding = 5;
  $text = wordwrap($text, ($image_width/10));
  $lines = explode("\n", $text);
  $image = imagecreate($image_width,((count($lines) * $line_height)) + ($padding * 2));
  $background = imagecolorallocate($image, $background[0], $background[1], $background[2]);
  $colour = imagecolorallocate($image,$colour[0],$colour[1],$colour[2]);
  imagefill($image, 0, 0, $background);
  $i = $padding;

  foreach($lines as $line){
    imagestring($image, $font, $padding, $i, trim($line), $colour);
    $i += $line_height;
  }

  // output to show the image in browser
  header("Content-type: image/jpeg");
  imagejpeg($image);

  // to save the image on server, delete '\\\', exit;, the header() and imagejpeg() above, add your dir/img
///  imagejpeg($image, 'dir/img.jpg');

  imagedestroy($image);
  exit;
}
- The textToImg() function displays the JPEG image in browser. If you want to save the image on server, delete these lines of code:
header("Content-type: image/jpeg");
imagejpeg($image);
...
exit;
And delete the three backslashes on this line ('dir/img.jpg' is the directory and the image name saved on server):
\\\ imagejpeg($image, 'dir/img.jpg');
Example:
<?php
// Here add the textToImg() function

$text = 'Free PHP MySQL course: http;//coursesweb.net/ , with video tutorials, scripts, and code snippets.';

// image width in pixels
$image_width = 250;

// calls the function
textToImg($text, $image_width);
Results:
Free PHP MySQL course: http;//coursesweb.net/ , with video tutorials, scripts, and code snippets.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is a block element?
<div> <img> <span>
<div>Web Programming and Development</div>
Which CSS code displays the text underlined?
font-style: italic; text-decoration: underline; font-weight: 500;
h2 {
  text-decoration: underline;
}
Click on the JavaScript function that can access other function after a specified time.
insertBefore() setTimeout() querySelector()
function someFunction() { alert("CoursesWeb.net"); }
setTimeout("someFunction()", 2000);
Click on the instruction that returns the number of items of a multidimensional array in PHP.
count($array) count($array, 1) strlen()
$food =["fruits" =>["banana", "apple"), "veggie" =>["collard", "pea"));
$nr_food = count($food, 1);
echo $nr_food;       // 6
PHP Image with text on New Lines

Last accessed pages

  1. For loops in JavaScript (986)
  2. SHA1 Encrypt data in JavaScript (35339)
  3. AJAX with POST and PHP (18831)
  4. Image object (1401)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137644)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (333)
  2. Read Excel file data in PHP - PhpExcelReader (127)
  3. The Four Agreements (98)
  4. PHP Unzipper - Extract Zip, Rar Archives (95)
  5. The Mastery of Love (90)
Chat
Chat or leave a message for the other users
Full screenInchide