【问题标题】:write text to an image将文本写入图像
【发布时间】:2013-03-30 06:27:14
【问题描述】:

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

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

【问题讨论】:

标签: php watermark


【解决方案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);

【讨论】:

  • @Mr.1.0 仅将imagecreatefrompng 替换为imagecreatefromjpeg
  • 有效 :) 但它没有背景色,这里是一个例子img27.imageshack.us/img27/980/9fc804cfabf94734a4aec13.png
  • @Mr.1.0 在这个地方看到代码// Create some colors你可以设置你自己的颜色样本改变255, 50, 15030,30,30
  • 如何在文本后面放一个框?
  • @Mr.1.0 盒子?你是说空间?通过更改10,20,您可以设置文本的位置x,y
【解决方案2】:

看看这个:

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

【讨论】:

    猜你喜欢
    • 2013-02-19
    • 2016-05-14
    • 1970-01-01
    • 2011-12-06
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 2016-09-29
    相关资源
    最近更新 更多