PHP and MySQL Code

Start typing to search for PHP and MySQL Code Snippets and Articles Search

Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
Title : Add a text description to an image. The text is not created over the image but rather creates a new, higher image. The upper part is the original image and the lower is the text. Text is wrapped at the end of line, the number of rows is adjustable.
Categories : PHP, GD image library, Graphics Update Picture
Slavisa Miladinovic
Date : Apr 06th 2003
Grade : 3 of 5 (graded 24 times)
Viewed : 15170
File : example.rar
Images : No Images for this code example.
Search : More code by Slavisa Miladinovic
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 

Code by Slavisa Miladinovic, [email protected], April 4th 2003

Script for generating images with text description


You can use this script if you have image gallery on your site.

Info about image is often to long to put in the picture name.
One solution is too put the text on the html page bellow the image.
This way people can perform text search through html pages and find
the right picture.

But when you want to browse through images saved on your hard disk
with some image viewer, it would be nice if you could see info about
the images. So my idea is to make new pictures, with the same width
as original image and with greater height.

The original image will be in the upper part of the new picture.In
the lower part there will be a white space with the text info on it.


Currently there are 4 rows (name, created, published and description).
The First three rows are fixed in height But the "description row" will
be split to multiple rows if the description text is too long for the
image width. The Image is extended downward to accommodate description.

I have a constant for max "description rows" ($constMaxDescRows) which is
currently set to 4. So if the description is 5 rows long, only 4 rows will
be shown. How many rows will some text take depend on the text length and
the image width. You can change the constant.

You can also change the number of fixed rows (name, created,published). Just
add or remove elements from "$rows" array. The script will adjust the image
height for this.


The down side of this concept is that you'll have the same text info on the
image and on the html page. I think it's less pretty but more convenient.


//in pixels
$constRowHeight = 15;

//in pixels
$constImageExtendFactor = 17;

//If the description is longer than max rows, show only max rows, cut the rest
$constMaxDescRows = 4;

//load "image.jpg" from disk - you must have it in same dir as script
$im = @ImageCreateFromJPEG ('image.jpg');
$imWidth = ImageSX($im);
$imHeight = ImageSY($im);

//add or remove elements from $rows array
$name = "Nice image";
$rows["name"] = "Name: " . $name;

$created = "01.04.2003 (d-m-y)";
$rows["created"] = "Created: " . $created;

$published = "01.04.2003 (d-m-y)";
$rows["published"] = "Published: ". $published;

//$newRow = "Text you want";
//$rows["newRow"] = "New row name: " . $newRow;

//$desc must have "Description: " appended at start, because of
//calculating length of row & number of rows
$descText = "Nice image. Here goes the description of image. If the
description is to long it goes to new row.";
$desc = "Description: " . $descText;

$noOfInfoRows = sizeof($rows);

$descLength = strlen($desc);

//one character is approximately 6 px wide for font size of 2 (size in
//ImageString function)
$charsInLine = floor($imWidth/6)-2;

if ($charsInLine<1) {
$charsInLine = 1; //prevent division by zero

$noOfRows = 1; //description rows
if ($descLength>$charsInLine){
$noOfRows = ceil($descLength/$charsInLine);
if ($noOfRows>$constMaxDescRows) {
$noOfRows = $constMaxDescRows;
//make array, element = row
$desc = chunk_split ($desc, $charsInLine, "&&&");
$desc = explode ("&&&", $desc);

$imNew = @ImageCreate ($imWidth, $imHeight + $noOfInfoRows*$constRowHeight +

$black = ImageColorAllocate ($imNew, 0, 0, 0);
$white = ImageColorAllocate ($imNew, 255, 255, 255);

//paint rectangle at bottom of image to white
ImageFilledRectangle ($imNew, 0, $imHeight, $imWidth, $imHeight +
$noOfInfoRows*$constRowHeight +
$noOfRows*$constImageExtendFactor, $white);

//write info rows
if (is_array($rows)) {
$j = 0;
while (list (, $oneRow) = each ($rows)) {
ImageString ($imNew, 2, 3, $imHeight+3+$j*$constRowHeight, $oneRow, $black);

//write description rows
if ($noOfRows>1) {
for ($i=0;$i<$noOfRows;$i++) {
ImageString ($imNew, 2, 3, $imHeight + 3 + ($noOfInfoRows + $i)*$constRowHeight,
ImageString ($imNew, 2, 3, $imHeight + 3 + $noOfInfoRows*$constRowHeight,
$desc, $black);

//copy original image to new image
ImageCopy ($imNew, $im, 0, 0, 0, 0, $imWidth, $imHeight);

//send image stream web browser
Header ("Content-type: image/jpeg");


CAPTCHA[Image verification]
Categories : PHP, Security, GD image library, Graphics, Sessions
imageMarker v 3.00 with new advanced features
Categories : PHP, PHP Classes, Graphics, GD image library
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
Generate image with random number (CAPTCHA)
Categories : PHP, GD image library, Graphics, Security
Create Thumbnails - resize an image - jpeg, jpg, gif, png to the specifed width and height in proportion without loosing out on pixcel quality.
Categories : PHP, GD image library, Graphics
Create a Thumbnail Using PHP, GD and Exif
Categories : PHP, Graphics, Exif, GD image library
a simple pie-chart in php3 (with gd)
Categories : PHP, Graphics, GD image library, Charts and Graphs
PHP Email image generator - hide your email from bots - using the GD Library
Categories : PHP, Graphics, GD image library, Beginner Guides
Display a bar chart based on random values.
Categories : Graphics, PHP, GD image library, Charts and Graphs
Advanced Image WaterMarker
Categories : PHP, PHP Classes, GD image library, Graphics, Object Oriented
Simple PHP Bar Graph using GD library
Categories : PHP, GD image library, Graphics, Arrays
Image Generation Class ( PNG Format )
Categories : PHP, GD image library, PHP Classes, Graphics
A captcha image allows you to prevent spam posting when users reload the page and stop bots from submitting forms automatically. This version allows you to use your own fonts (.ttf) to show the text.
Categories : PHP, Security, Graphics, GD image library
EasyPhpThumbnail Class - The EasyPhpThumbnail class allows you to generate thumbnails and handle image manipulation for GIF, JPG and PNG on-the-fly.
Categories : PHP, PHP Classes, Object Oriented, Graphics, GD image library
PHP Image Compression using GD library
Categories : PHP, Compression, GD image library, Graphics