1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
<?php
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);
}
}
?>
|
|