我的服务器上有一个图像,我想写文字。像水印一样。我能够将文字写入图像,但我想在文本中添加背景,以便于阅读。这是我到目前为止所拥有的。
header("Content-type: image/jpeg");
$imgPath = 'pic.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 224, 73, 87);
$string = "Please type the word in the circle.";
$fontSize = 8;
$x = 25;
$y = 200;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
答案 0 :(得分:1)
在这个课程中你需要有一个背景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);
答案 1 :(得分:0)
看看这个:
<?php
$stamp = imagecreatefrompng('stampimg.png');
$im = imagecreatefrompng('mainimage.png');
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);
imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>