This one of my new code snippet for the GD Library.
str_write("jpeg","abc/foo.jpg","ZEND");
str_write("png","abc/foo.png","ZEND");
<?php
function str_write($mime_type,$s_path,$string)
{
if((strcmp("jpeg",$mime_type)==0) ||( strcmp("jpg",$mime_type)==0))
{
header("Content Type:image/jpeg"); // Set the mime type as jpeg
$im = imagecreatefromjpeg($s_path); //load the image
$orange = imagecolorallocate($im, 0, 0,255); // pass the color of the font to be written
$px = (imagesx($im) - 10.5 * strlen($string)) / 2; //set the X position of the string
$py = imagesy($im) - 15 ; //set the Y position of the string
imagestring($im, 5, $px, $py, $string, $orange); //Write the string on the image
imagejpeg($im); //Generate the jpeg image
return (imagejpeg($im));
}
else if(strcmp("png",$mime_type)==0)
{
header("Content Type:image/png"); // Set the mime type as jpeg
$im = imagecreatefrompng($s_path); //load the image
$orange = imagecolorallocate($im, 0, 0,255); // pass the color of the font to be written
$px = (imagesx($im) - 10.5 * strlen($string)) / 2; //set the X position of the string
$py = imagesy($im) - 15 ; //set the Y position of the string
imagestring($im, 5, $px, $py, $string, $orange); //Write the string on the image
imagepng($im); //Generate the png image
return (imagepng($im));
}
else
{
print("<B>UNKNOWN MIME TYPE !!! </B>");
}
}
?>