如何使用GD在php中特定区域的图像上放置一些文本

Posted

技术标签:

【中文标题】如何使用GD在php中特定区域的图像上放置一些文本【英文标题】:how to put some text on image on particular area in php using GD 【发布时间】:2015-07-22 17:43:26 【问题描述】:

我想在 php 中使用 GD 生成在线证书,我已经准备好证书图像格式。只是我想将候选人姓名和其他详细信息作为特定区域的文本放在图像上。我有 jpg、gif 和 png 格式的图像文件

【问题讨论】:

到目前为止您尝试过什么? php.net/manual/en/function.imagettftext.php 【参考方案1】:

这是一个带有文本和证书图像的完整示例

<?php
 function LoadGif($imgname)
 
    /* Attempt to open */
    $im = @imagecreatefromgif($imgname);
    // Allocate A Color For The Text
   $black = imagecolorallocate($im, 0, 0, 0);
  // Set Path to Font File
  $font_path = 'ARIALNB.TTF';

  // Set Text to Be Printed On Image
  $candidate_name = "Chirag jagani";
  $speed = "8829";
  $percentage = "98%";
  $mintues ="4:00";
  $date="The 1st Day of July in the Year 2015";

  // Print Text On Image
  //imagettftext(image, size, varitcal means 90, X cordinates, Y coordinates, $white, $font_path, $speed);
  //candidate name
  imagettftext($im, 54, 90, 199, 713, $black, $font_path, $candidate_name);

  //speed
  imagettftext($im, 13, 90, 271, 620, $black, $font_path, $speed);

  //percentage
  imagettftext($im, 14, 90, 310, 605, $black, $font_path, $percentage);

  //mintues
  imagettftext($im, 13, 90, 331, 350, $black, $font_path, $mintues);  

  //date
  imagettftext($im, 22, 90, 382, 610, $black, $font_path, $date);  

    /* See if it failed */
    if(!$im)
    
        /* Create a blank image */
        $im = imagecreatetruecolor (150, 30);
        $bgc = imagecolorallocate ($im, 255, 255, 255);
        $tc = imagecolorallocate ($im, 0, 0, 0);

        imagefilledrectangle ($im, 0, 0, 150, 30, $bgc);

        /* Output an error message */
        imagestring ($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    
    return $im;

header('Content-Type: image/gif');
$img = LoadGif('main.gif');
imagegif($img);
imagedestroy($img);
?>

如果您有任何疑问,请告诉我。

一切顺利:)

【讨论】:

【参考方案2】:

如何在现有图像上创建水印..

//-------------------------------------------------------------------------

$wWidth  = 280; // set watermark image width
$wHeight = 50;  // set watermark image height

// Create the watermark image
$watermark = imagecreate( $wWidth, $wHeight );    

$black = imagecolorallocate( $watermark, 0, 0, 0 );        // define a colour to use
$white = imagecolorallocate( $watermark, 255, 255, 255 );  // define a colour to use

// Create a rectangle and fill it white
imagefilledrectangle( $watermark, 0, 0, $wWidth, $wHeight, $white );  
// Make white transparent
#imagecolortransparent ( $watermark, $white );  

$font = 'fonts/arial.ttf';  // store path to the font file

$wText = "(c) Chris Maggs ".date('Y');   // set watermark text
$wSize = "20";                              // set the watermark font size 

// Calculate the size of $wText (requires php/gd with ttf support)
$box = imagettfbbox( $wSize, 0, $font, $wText );  
$x = ( $wWidth -  ($box[2] - $box[0]) ) / 2;  
$y = ( $wHeight - ($box[1] - $box[7]) ) / 2;  
$y -= $box[7];  

// Add $wText to the image
imagettftext( $watermark, $wSize, 0, $x, $y, $black, $font, $wText );  

// Save the text image as temp.png
imagepng( $watermark, "uploads/temp.png" );  

// Cleanup the image memory usage
imagedestroy( $watermark );  

//-------------------------------------------------------------------------

// Set the path to the image to watermark
$input_image = "resources/tenby.jpg"; 

// Read in the text watermark image
$watermarkImage = imagecreatefrompng( "uploads/temp.png" );  

$watermark_width  = imagesx( $watermarkImage ); // width of image resource 
$watermark_height = imagesy( $watermarkImage ); // height of image resource   

// Create a new image resource
$image = imagecreatefromjpeg( $input_image );

// Find the size of the original image and read it into an array      
$size = getimagesize( $input_image ); 

// Set the positions of the watermark on the image
$dest_x = $size[0] - $watermark_width;    
$dest_y = $size[1] - $watermark_height;    

// Merge 2 image resources..
imagecopymerge($image, $watermarkImage, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 50);   

// OPTION 1 : save the watermarked image as watermarked.jpg 
imagejpeg( $image, "uploads/watermarked.jpg" );

// OPTION 2 : output the watermarked image
header("Content-type: image/jpeg");
imagejpeg( $image );

// delete the text watermark image
unlink( "uploads/temp.png"); 

// Cleanup the image memory usage
imagedestroy( $image );    

【讨论】:

以上是关于如何使用GD在php中特定区域的图像上放置一些文本的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 PHP GD 将文本写入不同大小的图像

在 PHP 中,如何直接在我的网站上使用 GD 库创建的图像

将文本放置在图像中特定位置的简单方法?

拉伸由 GD 创建的图像文本

PHP GD如何在一行上绘制文本

当我尝试在 PHP + GD 库中添加图像时,文本消失了