Mega Code Archive

 
Categories / Php / Graphics
 

Write Text to the Image

<?php //=========== change the text to display in the image //$text = 'mydomain.com'; //$imgfile="smp.jpg"; $font = 'arial.ttf'; $ext=substr($imgfile,-3); $ext=strtolower($ext); if($ext=="jpg" || $ext=="jpe") $im=@imagecreatefromjpeg("$imgfile"); elseif ($ext=="gif") $im=@imagecreatefromgif("$imgfile"); else {print "Unknown image format"; exit;} if (!$im) { /* See if it failed */ $im = ImageCreate (150, 30); /* Create a blank image */ $bgc = ImageColorAllocate ($im, 255, 255, 255); $tc = ImageColorAllocate ($im, 0, 0, 0); ImageFilledRectangle ($im, 0, 0, 150, 30, $bgc); /* Output an errmsg */ ImageString($im, 1, 5, 5, "Error loading $imgfile", $tc); return $im; } $x=imagesx($im); $y=imagesy($im); $fontsize=$x/20; $fontsize=floor($fontsize); if($fontsize<10) $fontsize=10; $white = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 0, 0, 0); imagettftext($im, $fontsize, 0, 12, $fontsize+8, $black, $font, $text); imagettftext($im, $fontsize, 0, 10, $fontsize+6, $white, $font, $text); if($ext=="gif") { header("Content-type: image/gif"); imagegif($im); } else { header("Content-type: image/jpeg"); imagejpeg($im); } imagedetroy($im); ?>