This function will brand an image with some text. You can give it a filename or an image resource. You can use built-in gd fonts or, with some modification, TrueType fonts if GD on the server supports it.
Usage when giving the function a filename:
brand_image('/path/to/file','text',font);
The font is the built-in GDfont number. This number can be from 1 to 4. See also the PHP function imageloadfont() to use different GD fonts.
The function will return FALSE if something failed otherwise it returns an array. The first element of the array is the mime type of the returned image and the second element is the contents of the branded image. Here is a full example:
$result = brand_image('/path/to/file','text',4);
if ($result === FALSE) { echo "Branding failed."; }
else {
list($type,$contents) = $result;
header("Content-type: $type");
header("Content-length: " . strlen($contents));
echo $contents;
}
Usage when giving the function an image resource:
brand_image(&$resource,'text',font);
This function will return FALSE if it failed. Otherwise it returns an array with the only element containing a reference to the image resource.
Full example:
// $resource is a pre-existing image resource
$result = brand_image($resource,'text',4);
if ($result === FALSE) { echo "Branding failed."; }
else {
list($image) = $result;
header("Content-type: image/jpeg");
imagejpeg($image);
}
The text is branded using two different strings of different colours. The colours can be changed at the beginning of the function. The text will be written using the first color and the second color is used to create a shadow of the text. If the two colors are very different this has the effect of allowing the text to be read no matter what colour it is printed on.
function brand_image($source,$text,$font = 4) {
$color = array('red' => 255,'green' => 255,'blue' => 255);
$back_color = array('red' => 0,'green' => 0,'blue' => 0);
// Assume we're given a path if $source is a string
if (is_string($source)) {
if (!is_file($source)) { return FALSE; }
list($width,$height,$type,$attr) = getimagesize($source);
if ($type == 2) {
$mime = 'image/jpeg';
$func = 'imagecreatefromjpeg';
$output = 'imagejpeg';
} elseif ($type == 3) {
$mime = 'image/png';
$func = 'imagecreatefrompng';
$output = 'imagepng';
} elseif ($type == 1) {
$mime = 'image/gif';
$func = 'imagecreatefromgif';
$output = 'imagegif';
} else return FALSE;
$img = $func($source);
if (!$img) { return FALSE; }
} else {
// Assume it's an image resource
$img =& $source;
$width = imagesx($source);
$height = imagesy($source);
}
// Attempt to allocate text colour
$color_index = @imagecolorallocate($img,$color['red'],$color['green'],$color['blue']);
if ($color_index == -1) {
// If unsuccessful use closest colour in palette
$color_index = imagecolorclosest($img,$color['red'],$color['green'],$color['blue']);
}
$back_color_index = @imagecolorallocate($img,$back_color['red'],$back_color['green'],$back_color['blue']);
if ($back_color_index == -1) {
$back_color_index = imagecolorclosest($img,$back_color['red'],$back_color['green'],$back_color['blue']);
}
// Write the text to the image using built-in php fonts
$text_width = imagefontwidth($font) * strlen($text) + 5;
$text_height = imagefontheight($font) + 5;
imagestring($img,$font,$width - $text_width + 1,$height - $text_height + 1,$text,$back_color_index);
imagestring($img,$font,$width - $text_width,$height - $text_height,$text,$color_index);
/*
// If GD has TrueType fonts enabled you can use these functions to use TrueType fonts
// I don't know of a way to calculate the width/height of TrueType fonts so you'll have
// to adjust the width and height values manually. $font is the filename of a TrueType font
imagettftext($img,10,0,$width - 83,$height - 6,$color_index,$font,$text);
imagettftext($img,10,0,$width - 83 + 1,$height - 6 + 1,$back_color_index,$font,$text);
*/
if (is_string($source)) {
// Get the image contents
ob_start();
if ($output == 'imagejpeg') {
// Change the quality of JPEG output here
$output($img,NULL,75);
} else $output($img);
$contents = ob_get_contents();
ob_end_clean();
// Return the contents of the image and the mime-type
return array($mime,$contents);
} else {
// Return image resource (returned in an array
// so that it can be returned as a reference)
return array(&$img);
}
}