将透明文本写入 jpeg

发布于 2024-12-18 14:51:16 字数 1058 浏览 0 评论 0原文

到处都在谈论在透明图像上添加文本。我想将水印之类的文本(即透明文本)添加到 jpeg 格式的经典图像中。我尝试了一切,我发现了什么。有人解决吗?完全可以解决吗?

我想使用GD(不是imagick),因为项目是基于GD构建的。

谢谢

编辑: 这就是我使用的

$textBox = imagettfbbox($fontSize, $angle, $font, $text);

//file is orig image
$im = imagecreatefromjpeg($file);
$transparent = imagecolorallocate($im, 250, 250, 250);

//does not help - text disapper
//$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);

//does not help, burn whole to image
//imagealphablending($im, false);
//imagesavealpha($im, true);

//calculate position - $imageInfo is getsizeimage on orig image
$textX = ($imageInfo[0] / 2) - ($textBox[2] / 2);
$textY = ($imageInfo[1] / 2) - ($textBox[5] / 2);

//does not help
//imagecolortransparent ($im, $transparent);

imagettftext($im, $fontSize, $angle, $textX, $textY, $transparent, $font, $text);

header("Content-Type: image/jpeg");
imagejpeg($im);

简单的解决方案

Color is neccessary set by imagecolorresolvealpha($im, 0, 0, 0, 100);

Everywhere is talking about adding text on transparent image. I would like add text like watermark (so mean transparent text) to classic image in jpeg format. I tried everything, what I found. Has someone solution for it? Is solutionable at all?

I would like to use GD (not imagick), because project is built on GD.

Thanks

Edit:
This is what I using

$textBox = imagettfbbox($fontSize, $angle, $font, $text);

//file is orig image
$im = imagecreatefromjpeg($file);
$transparent = imagecolorallocate($im, 250, 250, 250);

//does not help - text disapper
//$transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);

//does not help, burn whole to image
//imagealphablending($im, false);
//imagesavealpha($im, true);

//calculate position - $imageInfo is getsizeimage on orig image
$textX = ($imageInfo[0] / 2) - ($textBox[2] / 2);
$textY = ($imageInfo[1] / 2) - ($textBox[5] / 2);

//does not help
//imagecolortransparent ($im, $transparent);

imagettftext($im, $fontSize, $angle, $textX, $textY, $transparent, $font, $text);

header("Content-Type: image/jpeg");
imagejpeg($im);

Simple solution

Color is neccessary set by imagecolorresolvealpha($im, 0, 0, 0, 100);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2024-12-25 14:51:16

试试这个,它对我有用......
添加文本作为水印的 Php 脚本

这里是我获取它的参考

<?php
  //----------------------------------------------------------------
  // render_text_on_gd_image
  // THE FUNCTION THAT ACTUALLY RENDERS THE STRING
  //----------------------------------------------------------------

  define( 'WATERMARK_MARGIN_ADJUST', 5 );
  define( 'WATERMARK_FONT_REALPATH', 'c:\\windows\\fonts\\' );

  function render_text_on_gd_image( &$source_gd_image, $text, $font, $size, $color, $opacity, $rotation, $align )
  {
    $source_width = imagesx( $source_gd_image );
    $source_height = imagesy( $source_gd_image );

    $bb = imagettfbbox_fixed( $size, $rotation, $font, $text );

    $x0 = min( $bb[ 0 ], $bb[ 2 ], $bb[ 4 ], $bb[ 6 ] ) - WATERMARK_MARGIN_ADJUST;
    $x1 = max( $bb[ 0 ], $bb[ 2 ], $bb[ 4 ], $bb[ 6 ] ) + WATERMARK_MARGIN_ADJUST;
    $y0 = min( $bb[ 1 ], $bb[ 3 ], $bb[ 5 ], $bb[ 7 ] ) - WATERMARK_MARGIN_ADJUST;
    $y1 = max( $bb[ 1 ], $bb[ 3 ], $bb[ 5 ], $bb[ 7 ] ) + WATERMARK_MARGIN_ADJUST;

    $bb_width = abs( $x1 - $x0 );
    $bb_height = abs( $y1 - $y0 );

    switch ( $align )
    {
      case 11:
        $bpy = -$y0;
        $bpx = -$x0;
        break;
      case 12:
        $bpy = -$y0;
        $bpx = $source_width / 2 - $bb_width / 2 - $x0;
        break;
      case 13:
        $bpy = -$y0;
        $bpx = $source_width - $x1;
        break;
      case 21:
        $bpy = $source_height / 2 - $bb_height / 2 - $y0;
        $bpx = -$x0;
        break;
      case 22:
        $bpy = $source_height / 2 - $bb_height / 2 - $y0;
        $bpx = $source_width / 2 - $bb_width / 2 - $x0;
        break;
      case 23:
        $bpy = $source_height / 2 - $bb_height / 2 - $y0;
        $bpx = $source_width - $x1;
        break;
      case 31:
        $bpy = $source_height - $y1;
        $bpx = -$x0;
        break;
      case 32:
        $bpy = $source_height - $y1;
        $bpx = $source_width / 2 - $bb_width / 2 - $x0;
        break;
      case 33;
        $bpy = $source_height - $y1;
        $bpx = $source_width - $x1;
        break;
    }

    $alpha_color = imagecolorallocatealpha(
      $source_gd_image,
      hexdec( substr( $color, 0, 2 ) ),
      hexdec( substr( $color, 2, 2 ) ),
      hexdec( substr( $color, 4, 2 ) ),
      127 * ( 100 - $opacity ) / 100
    );

    return imagettftext( $source_gd_image, $size, $rotation, $bpx, $bpy, $alpha_color, WATERMARK_FONT_REALPATH . $font, $text );
  }

  //----------------------------------------------------------------
  // imagettfbbox_fixed
  // FIX FOR THE BUGGY IMAGETTFBBOX IMPLEMENTATION IN GD LIBRARY
  //----------------------------------------------------------------

  function imagettfbbox_fixed( $size, $rotation, $font, $text )
  {
    $bb = imagettfbbox( $size, 0, WATERMARK_FONT_REALPATH . $font, $text );
    $aa = deg2rad( $rotation );
    $cc = cos( $aa );
    $ss = sin( $aa );
    $rr = array( );
    for( $i = 0; $i < 7; $i += 2 )
    {
      $rr[ $i + 0 ] = round( $bb[ $i + 0 ] * $cc + $bb[ $i + 1 ] * $ss );
      $rr[ $i + 1 ] = round( $bb[ $i + 1 ] * $cc - $bb[ $i + 0 ] * $ss );
    }
    return $rr;
  }

  //----------------------------------------------------------------
  // CREATE WATERMARK FUNCTION
  //----------------------------------------------------------------

  define( 'WATERMARK_OUTPUT_QUALITY', 90 );

  function create_watermark_from_string( $source_file_path, $output_file_path, $text, $font, $size, $color, $opacity, $rotation, $align )
  {
    list( $source_width, $source_height, $source_type ) = getimagesize( $source_file_path );

    if ( $source_type === NULL )
    {
      return false;
    }

    switch ( $source_type )
    {
      case IMAGETYPE_GIF:
        $source_gd_image = imagecreatefromgif( $source_file_path );
        break;
      case IMAGETYPE_JPEG:
        $source_gd_image = imagecreatefromjpeg( $source_file_path );
        break;
      case IMAGETYPE_PNG:
        $source_gd_image = imagecreatefrompng( $source_file_path );
        break;
      default:
        return false;
    }

    render_text_on_gd_image( $source_gd_image, $text, $font, $size, $color, $opacity, $rotation, $align );

    imagejpeg( $source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY );
    imagedestroy( $source_gd_image );
  }

  //----------------------------------------------------------------
  // FILE PROCESSING FUNCTION
  //----------------------------------------------------------------

  define( 'UPLOADED_IMAGE_DESTINATION', 'originals/' );
  define( 'PROCESSED_IMAGE_DESTINATION', 'images/' );

  function process_image_upload( $Field )
  {
    $temp_file_path = $_FILES[ $Field ][ 'tmp_name' ];
    $temp_file_name = $_FILES[ $Field ][ 'name' ];

    list( , , $temp_type ) = getimagesize( $temp_file_path );

    if ( $temp_type === NULL )
    {
      return false;
    }

    switch ( $temp_type )
    {
      case IMAGETYPE_GIF:
        break;
      case IMAGETYPE_JPEG:
        break;
      case IMAGETYPE_PNG:
        break;
      default:
        return false;
    }

    $uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name;
    $processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace( '/\\.[^\\.]+$/', '.jpg', $temp_file_name );

    move_uploaded_file( $temp_file_path, $uploaded_file_path );

    //----------------------------------------------------------------
    // PARAMETER DESCRIPTION
    // (1) SOURCE FILE PATH
    // (2) OUTPUT FILE PATH
    // (3) THE TEXT TO RENDER
    // (4) FONT NAME -- MUST BE A *FILE* NAME
    // (5) FONT SIZE IN POINTS
    // (6) FONT COLOR AS A HEX STRING
    // (7) OPACITY -- 0 TO 100
    // (8) TEXT ANGLE -- 0 TO 360
    // (9) TEXT ALIGNMENT CODE -- POSSIBLE VALUES ARE 11, 12, 13, 21, 22, 23, 31, 32, 33
    //----------------------------------------------------------------

    $result = create_watermark_from_string(
      $uploaded_file_path,
      $processed_file_path,
      'Copyrights (c) 2008',
      'arial.ttf',
      14,
      'CCCCCC',
      75,
      0,
      32
    );

    if ( $result === false )
    {
      return false;
    }
    else
    {
      return array( $uploaded_file_path, $processed_file_path );
    }
  }

  //----------------------------------------------------------------
  // END OF FUNCTIONS
  //----------------------------------------------------------------

  $result = process_image_upload( 'File1' );

  if ( $result === false )
  {
    echo '<br>An error occurred during file processing.';
  }
  else
  {
    echo '<br>Original image saved as <a href="' . $result[ 0 ] . '" target="_blank">' . $result[ 0 ] . '</a>';
    echo '<br>Watermarked image saved as <a href="' . $result[ 1 ] . '" target="_blank">' . $result[ 1 ] . '</a>';
  }
?>

添加图像作为水印的Php脚本

<?php  

header('content-type: image/jpeg');  

$watermark = imagecreatefrompng('watermark.png');  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($_GET['src']);  
$size = getimagesize($_GET['src']);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark);  

Try this it works for me...
Php Script to add Text as a watermark

Here is the reference from where I got it

<?php
  //----------------------------------------------------------------
  // render_text_on_gd_image
  // THE FUNCTION THAT ACTUALLY RENDERS THE STRING
  //----------------------------------------------------------------

  define( 'WATERMARK_MARGIN_ADJUST', 5 );
  define( 'WATERMARK_FONT_REALPATH', 'c:\\windows\\fonts\\' );

  function render_text_on_gd_image( &$source_gd_image, $text, $font, $size, $color, $opacity, $rotation, $align )
  {
    $source_width = imagesx( $source_gd_image );
    $source_height = imagesy( $source_gd_image );

    $bb = imagettfbbox_fixed( $size, $rotation, $font, $text );

    $x0 = min( $bb[ 0 ], $bb[ 2 ], $bb[ 4 ], $bb[ 6 ] ) - WATERMARK_MARGIN_ADJUST;
    $x1 = max( $bb[ 0 ], $bb[ 2 ], $bb[ 4 ], $bb[ 6 ] ) + WATERMARK_MARGIN_ADJUST;
    $y0 = min( $bb[ 1 ], $bb[ 3 ], $bb[ 5 ], $bb[ 7 ] ) - WATERMARK_MARGIN_ADJUST;
    $y1 = max( $bb[ 1 ], $bb[ 3 ], $bb[ 5 ], $bb[ 7 ] ) + WATERMARK_MARGIN_ADJUST;

    $bb_width = abs( $x1 - $x0 );
    $bb_height = abs( $y1 - $y0 );

    switch ( $align )
    {
      case 11:
        $bpy = -$y0;
        $bpx = -$x0;
        break;
      case 12:
        $bpy = -$y0;
        $bpx = $source_width / 2 - $bb_width / 2 - $x0;
        break;
      case 13:
        $bpy = -$y0;
        $bpx = $source_width - $x1;
        break;
      case 21:
        $bpy = $source_height / 2 - $bb_height / 2 - $y0;
        $bpx = -$x0;
        break;
      case 22:
        $bpy = $source_height / 2 - $bb_height / 2 - $y0;
        $bpx = $source_width / 2 - $bb_width / 2 - $x0;
        break;
      case 23:
        $bpy = $source_height / 2 - $bb_height / 2 - $y0;
        $bpx = $source_width - $x1;
        break;
      case 31:
        $bpy = $source_height - $y1;
        $bpx = -$x0;
        break;
      case 32:
        $bpy = $source_height - $y1;
        $bpx = $source_width / 2 - $bb_width / 2 - $x0;
        break;
      case 33;
        $bpy = $source_height - $y1;
        $bpx = $source_width - $x1;
        break;
    }

    $alpha_color = imagecolorallocatealpha(
      $source_gd_image,
      hexdec( substr( $color, 0, 2 ) ),
      hexdec( substr( $color, 2, 2 ) ),
      hexdec( substr( $color, 4, 2 ) ),
      127 * ( 100 - $opacity ) / 100
    );

    return imagettftext( $source_gd_image, $size, $rotation, $bpx, $bpy, $alpha_color, WATERMARK_FONT_REALPATH . $font, $text );
  }

  //----------------------------------------------------------------
  // imagettfbbox_fixed
  // FIX FOR THE BUGGY IMAGETTFBBOX IMPLEMENTATION IN GD LIBRARY
  //----------------------------------------------------------------

  function imagettfbbox_fixed( $size, $rotation, $font, $text )
  {
    $bb = imagettfbbox( $size, 0, WATERMARK_FONT_REALPATH . $font, $text );
    $aa = deg2rad( $rotation );
    $cc = cos( $aa );
    $ss = sin( $aa );
    $rr = array( );
    for( $i = 0; $i < 7; $i += 2 )
    {
      $rr[ $i + 0 ] = round( $bb[ $i + 0 ] * $cc + $bb[ $i + 1 ] * $ss );
      $rr[ $i + 1 ] = round( $bb[ $i + 1 ] * $cc - $bb[ $i + 0 ] * $ss );
    }
    return $rr;
  }

  //----------------------------------------------------------------
  // CREATE WATERMARK FUNCTION
  //----------------------------------------------------------------

  define( 'WATERMARK_OUTPUT_QUALITY', 90 );

  function create_watermark_from_string( $source_file_path, $output_file_path, $text, $font, $size, $color, $opacity, $rotation, $align )
  {
    list( $source_width, $source_height, $source_type ) = getimagesize( $source_file_path );

    if ( $source_type === NULL )
    {
      return false;
    }

    switch ( $source_type )
    {
      case IMAGETYPE_GIF:
        $source_gd_image = imagecreatefromgif( $source_file_path );
        break;
      case IMAGETYPE_JPEG:
        $source_gd_image = imagecreatefromjpeg( $source_file_path );
        break;
      case IMAGETYPE_PNG:
        $source_gd_image = imagecreatefrompng( $source_file_path );
        break;
      default:
        return false;
    }

    render_text_on_gd_image( $source_gd_image, $text, $font, $size, $color, $opacity, $rotation, $align );

    imagejpeg( $source_gd_image, $output_file_path, WATERMARK_OUTPUT_QUALITY );
    imagedestroy( $source_gd_image );
  }

  //----------------------------------------------------------------
  // FILE PROCESSING FUNCTION
  //----------------------------------------------------------------

  define( 'UPLOADED_IMAGE_DESTINATION', 'originals/' );
  define( 'PROCESSED_IMAGE_DESTINATION', 'images/' );

  function process_image_upload( $Field )
  {
    $temp_file_path = $_FILES[ $Field ][ 'tmp_name' ];
    $temp_file_name = $_FILES[ $Field ][ 'name' ];

    list( , , $temp_type ) = getimagesize( $temp_file_path );

    if ( $temp_type === NULL )
    {
      return false;
    }

    switch ( $temp_type )
    {
      case IMAGETYPE_GIF:
        break;
      case IMAGETYPE_JPEG:
        break;
      case IMAGETYPE_PNG:
        break;
      default:
        return false;
    }

    $uploaded_file_path = UPLOADED_IMAGE_DESTINATION . $temp_file_name;
    $processed_file_path = PROCESSED_IMAGE_DESTINATION . preg_replace( '/\\.[^\\.]+$/', '.jpg', $temp_file_name );

    move_uploaded_file( $temp_file_path, $uploaded_file_path );

    //----------------------------------------------------------------
    // PARAMETER DESCRIPTION
    // (1) SOURCE FILE PATH
    // (2) OUTPUT FILE PATH
    // (3) THE TEXT TO RENDER
    // (4) FONT NAME -- MUST BE A *FILE* NAME
    // (5) FONT SIZE IN POINTS
    // (6) FONT COLOR AS A HEX STRING
    // (7) OPACITY -- 0 TO 100
    // (8) TEXT ANGLE -- 0 TO 360
    // (9) TEXT ALIGNMENT CODE -- POSSIBLE VALUES ARE 11, 12, 13, 21, 22, 23, 31, 32, 33
    //----------------------------------------------------------------

    $result = create_watermark_from_string(
      $uploaded_file_path,
      $processed_file_path,
      'Copyrights (c) 2008',
      'arial.ttf',
      14,
      'CCCCCC',
      75,
      0,
      32
    );

    if ( $result === false )
    {
      return false;
    }
    else
    {
      return array( $uploaded_file_path, $processed_file_path );
    }
  }

  //----------------------------------------------------------------
  // END OF FUNCTIONS
  //----------------------------------------------------------------

  $result = process_image_upload( 'File1' );

  if ( $result === false )
  {
    echo '<br>An error occurred during file processing.';
  }
  else
  {
    echo '<br>Original image saved as <a href="' . $result[ 0 ] . '" target="_blank">' . $result[ 0 ] . '</a>';
    echo '<br>Watermarked image saved as <a href="' . $result[ 1 ] . '" target="_blank">' . $result[ 1 ] . '</a>';
  }
?>

Php Script to add Image as a watermark

<?php  

header('content-type: image/jpeg');  

$watermark = imagecreatefrompng('watermark.png');  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);  
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($_GET['src']);  
$size = getimagesize($_GET['src']);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文