2

我的服务器上有一张图片,我想给它写文字。像水印一样。我可以在图像中写入文本,但我想为文本添加背景以便于阅读。这是我到目前为止所拥有的。

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);
4

2 回答 2

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);
于 2013-03-30T06:32:04.873 回答
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);
?>
于 2014-12-10T14:47:24.360 回答