在这个类中,你需要有一个背景base.png
和字体arial.ttf
你可以有一个不同的字体,但必须是一个ttf
。如果你想有不同的字体格式,你必须对代码进行更改
class SecurityImg{
static function Image_Create($basename){//Create image
$im =imagecreatefrompng ($basename);
//only replace imagecreatefrompng with imagecreatefromjpeg for open jpg instead of png
return($im);
}
static function PutTextOnImage($text,$baseimage,$angel,$xi,$yi){
// Create some colors
$text_color= imagecolorallocate($baseimage, 255, 50, 150);
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($baseimage, 15, $angel, $xi, $yi, $text_color, $font, $text);
return($baseimage);
}
static function Create($imgbase,$TEXT){
$ifp=self::Image_Create($imgbase);
$im=self::PutTextOnImage($TEXT,$ifp,0,10,20);
return($im);
}
}
$Securityimg=new SecurityImg();
$im=$Securityimg->Create("base.png","test");
// Output the image
// Set the content-type
header('Content-Type: image/jpeg');
imagejpeg($im);
// Using imagepng() results in clearer text compared with imagejpeg()
imagedestroy($im);