|
|
|
| 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 |
 Slavisa Miladinovic |
| Date : |
Apr 06th 2003 |
| Grade : |
3 of 5 (graded 23 times) |
| Viewed : |
8876 |
| 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 |
|
|
|
|
|
|
<?php
/*
Code by Slavisa Miladinovic, sly@medianis.net, April 4th 2003
Script for generating images with text description
Purpose:
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.
Features:
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.
Conclusion:
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 +
$noOfRows*$constImageExtendFactor);
$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);
$j++;
}
}
//write description rows
if ($noOfRows>1) {
for ($i=0;$i<$noOfRows;$i++) {
ImageString ($imNew, 2, 3, $imHeight + 3 + ($noOfInfoRows + $i)*$constRowHeight,
$desc[$i],$black);
}
}else{
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");
ImageJPEG($imNew);
ImageDestroy($imNew);
?> |
|
| 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 | | | 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 | | | Line graphics generation library written in PHP + GD library (spanish comments) Categories : PHP, Graphics, GD image library | | | Simple PHP Bar Graph using GD library Categories : PHP, GD image library, Graphics, Arrays | | | CAPTCHA[Image verification] Categories : PHP, Security, GD image library, Graphics, Sessions | | | Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics, PHP, PHP Classes, GD image library, Charts and Graphs | | | 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 | | | imageMarker v 3.00 with new advanced features Categories : PHP, PHP Classes, Graphics, GD image library | | | Generate image with random number (CAPTCHA) Categories : PHP, GD image library, Graphics, Security | | | Image Generation Class ( PNG Format ) Categories : PHP, GD image library, PHP Classes, Graphics | | | PHP Image Compression using GD library Categories : PHP, Compression, GD image library, Graphics | | | a simple pie-chart in php3 (with gd) Categories : PHP, Graphics, GD image library, Charts and Graphs | | | crop and resize image class using gd library function Categories : PHP, PHP Classes, GD image library, Graphics | |
|
|
|