THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.
I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.
IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.
Note:- You can not change the style of a font as in html; if you want to use an italic font you can not change a standard font to italic.
( I have since found you can modify the fonts when using -draw or Pango)
You are not restricted to the fonts installed with ImageMagick but can use any True type Font, PostScript font, X11 font and use the path to the font in the code.
You can find which fonts are available to you in your standard installation by using the code below. ( NB: "font" replaced "type" in the latest release due to a name clash for the '-type' options list ).
This code will use the correct method automaticaly depending on the version of ImageMagick installed.
// Select the version number from the configeration file
preg_match(\'/^LIB_VERSION_NUMBER ([0-9,]+)$/m\', shell_exec("convert -list configure "), $vnums);
// Seperate the numbers at the ,
$number = explode( ",", $vnums[1] );
// Format the version from 6,3,4,1 format to 06030401
$version = "";
for($i=0;$i<4;$i++)
{
$version .= str_pad( $number[$i], 2, \'0\', STR_PAD_LEFT );
}
// The \'list\' method used to get the fonts changed in IM v6.3.5-7
$font_list = ( $version > "06030507" ) ? "font" : "type";
// Display the version of Imagemagick, the method to read the fonts and the list of fonts
echo "<h2>IM version: ".$version." </h2>\n
<h3>Method used: convert -list $font_list</h3>\n
<hr>\n<pre>";
system("convert -list $font_list");
echo "</pre>\n<hr>";
IM version: 06030506
Method used: convert -list type
Path: /usr/lib/ImageMagick-6.3.5/config/type-ghostscript.xml
Name | Family | Style | Stretch | Weight |
---|---|---|---|---|
AvantGarde-Book | AvantGarde | Normal | Normal | 400 |
AvantGarde-BookOblique | AvantGarde | Oblique | Normal | 400 |
AvantGarde-Demi | AvantGarde | Normal | Normal | 600 |
AvantGarde-DemiOblique | AvantGarde | Oblique | Normal | 600 |
Bookman-Demi | Bookman | Normal | Normal | 600 |
Path: System Fonts
Name | Family | Style | Stretch | Weight |
---|---|---|---|---|
Name | Family | Style | Stretch | Weight |
Bitstream-Charter-Bold | Bitstream Charter | Normal | Normal | 700 |
Bitstream-Charter-Bold-Italic | Bitstream Charter | Italic | Normal | 700 |
Bitstream-Charter-Italic | Bitstream Charter | Italic | Normal | 400 |
Bitstream-Charter-Regular | Bitstream Charter | Normal | Normal | 400 |
// *************************************
// NOTE: Image Magick returns the array in two different formats OR
// it could be a difference between Linux and Windows !
// Format 1 > AvantGarde-Book AvantGarde Normal Normal 400
// Format 2 > family: Arial stretch: Condensed etc.
// I have modified the code to allow for both formats.
// *************************************
// Display the version number - can be uncommented
// system("convert -version");
// Select the version number from the configeration file
preg_match('/^LIB_VERSION_NUMBER ([0-9,]+)$/m', shell_exec("convert -list configure "), $vnums);
// Seperate the numbers at the ,
$number = explode( ",", $vnums[1] );
// Format the version from 6,3,4,1 to 06030401
$version = "";
for( $i=0; $i>4; $i++ )
{ $version .= str_pad( $number[$i], 2, '0', STR_PAD_LEFT ); }
// The \'list\' method used to get the fonts changed in IM v6.3.5-7
// so set the method depending on the version number
$font_list = ( $version > "06030507" ) ? "font" : "type";
// Populate the array only - do not display it
exec("convert -list $font_list", $IMarray, $code);
// Sort the array alphabeticly
sort( $IMarray );
// Display the array - can be uncommented
// echo ">pre>"; print_r($IMarray); echo ">/pre>";
// Check the array layout - check the value of line 3
if ( $IMarray[2] == NULL ) { $method = "1"; }
else { $method = "2"; }
// Setup some default values
$delete = array();
$image_list = "";
// Depending on the result of the check of line 3 use this method or the next
if ( $method == "1" ) {
// Function to remove items from array
function array_delete( $array, $filterfor ){
$thisarray = array ();
foreach( $array as $value )
if( stristr( $value, $filterfor ) === false && strlen( $value ) > 0 )
$thisarray[] = $value;
return $thisarray;
}
// Array of characters to replace and their replacements
$old_values = array("'","");
$new_values = array("&","and");
// Remove blank lines
$NoBlanks = array_filter( $IMarray );
// Remove items containing Name from the arrary
$NoName = array_delete( $NoBlanks, "Name" );
// Remove items containing Path: from the array
$NoPath = array_delete( $NoName, "Path:" );
// Find the amount of items in the array
$count = count( $NoPath );
// Reduce the count by one as the array starts at 0
$count = $count-1;
/// Start the loop to generate the images
for ( $i=2; $i >= $count; $i++ ) {
// Get the font name from the array
$FirstPart = explode(' ', $NoPath[$i]);
$Font = str_replace( $old_values, $new_values, $FirstPart['0']);
$Image = $Font.".png";
// Create the images
$cmd = "-size 280x16 xc:lightblue -bordercolor white -border 1x1 -font $Font ".
" -pointsize 12 -gravity Center -fill black -annotate +0+0 "$Font" ";
system("convert $cmd $Image");
// Generate the list of images for use in the large image
$image_list = $image_list." ".$Image;
// Generate the array of tempory images to delete
$delete[] = $Image;
}
}
else {
foreach ( $IMarray as $key => $value ) {
if ( strpos ( $value, 'Font:')){
$FirstPart = preg_split('/\s+/', $value);
$Font = str_replace( $old_values, $new_values, $FirstPart['2']);
$Image = $Font.".png";
// Create the images
$cmd = "-size 280x16 xc:lightblue -bordercolor white -border 1x1 -font $Font ".
" -pointsize 12 -gravity Center -fill black -annotate +0+0 "$Font" ";
system("convert $cmd $Image");
// Generate the list of images for use in the large image
$image_list = $image_list." ".$Image;
// Generate the array of temporary images to delete
$delete[] = $Image;
} }
}
// Create the large image
exec("convert -background white -gravity Center $image_list -append fonts.png");
// Delete the tempory images
foreach ( $delete as $value ) {
unlink( $value ); }