到处都在谈论在透明图像上添加文字.我希望以jpeg格式向经典图像添加水印(所以意味着透明文本)等文本.我尝试了一切,我发现了什么.有人解决方案吗?可以解决吗?
我想使用GD(不是想象),因为项目是建立在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);
解决方法:
试试这个对我有用……
PHP脚本添加文本作为水印
Here是我得到它的参考
<?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);