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

问题描述 投票:-3回答:2

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

php image gd
2个回答
2
投票

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

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

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

祝一切顺利 :)


0
投票

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

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

$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 );    
© www.soinside.com 2019 - 2024. All rights reserved.