[英]php , gd, create watermark, change watermark text size and background color, imagecreatefromjpeg
我需要創建一個水印,將其應用到圖片上並用不同的名稱保存。 當前腳本運行良好,但唯一的問題是我需要增加“示例文本”的大小並將背景從黑色更改為白色。 我嘗試了不同的場景,改變了不透明度但仍然無法改變背景顏色。
function watermark($imag_path, $photo_id) {
// Load the stamp and the photo to apply the watermark to
$im = imagecreatefromjpeg("$imag_path");
echo "imag_path is $imag_path and photoid is $photo_id";
// First we create our stamp image manually from GD
$stamp = imagecreatetruecolor(490, 20);
//$im = imagecreatefromjpeg("$photo_id");
imagestring($stamp, 5, 20, 2, 'sample text', 0xff0000);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Merge the stamp onto our photo with an opacity (transparency) of 100%
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 100);
$new_photo_id = $photo_id . "sample.JPG";
// Save the image to file and free memory
imagejpeg($im, "tmp/$new_photo_id");
imagedestroy($im);
}
為什么要使用郵票? 我在我的一個網站上使用以下代碼:
$im = imagecreatefromjpeg($path);
function shadow_text($im, $size, $x, $y, $font, $text)
{
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
imagettftext($im, $size, 0, $x + 1, $y + 1, $black, $font, $text);
imagettftext($im, $size, 0, $x + 0, $y + 1, $black, $font, $text);
imagettftext($im, $size, 0, $x + 0, $y + 0, $white, $font, $text);
}
$font = '../fonts/verdana.ttf';
$size = 11;
# calculate maximum height of a character
$bbox = imagettfbbox($size, 0, $font, 'ky');
$x = 8; $y = 8 - $bbox[5];
$text = 'text to be added';
shadow_text($im, $size, $x, $y, $font, $text);
header("Content-Type: image/jpeg");
imagejpeg($im, null, 90);
這段代碼運行速度足夠快,我們可以使用它在下載照片時將動態標簽動態添加到我們的照片部分,而不是將它們保存到磁盤。
在我的代碼中,我修復了一些常見錯誤,例如:
1)水印文本超出圖像范圍。
2) PNG & JPG 圖片錯誤。
所以我計算圖像寬度並決定字體大小。 所以字體大小是動態的。
所以你可以復制我的方法並粘貼到你想使用它的地方。
function waterMark($SourceFile,$ext='png',$WaterMarkText)
{
if( $ext == "jpg" or $ext == 'jpeg')
$image = imagecreatefromjpeg($SourceFile);
else
$image = imagecreatefrompng($SourceFile);
list($width, $height) = getimagesize($SourceFile);
$font = public_path('fonts/arial.ttf');
$size = $width*4/100; // calculating font size based on image width.
# calculate maximum height of a character
$bbox = imagettfbbox($size, 0, $font, 'ky');
$x = 8; $y = 8 - $bbox[5];
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
imagettftext($image, $size, 0, $x + 1, $y + 1, $black, $font, $WaterMarkText);
imagettftext($image, $size, 0, $x + 0, $y + 1, $black, $font, $WaterMarkText);
imagettftext($image, $size, 0, $x + 0, $y + 0, $white, $font, $WaterMarkText);
//header("Content-Type: image/jpeg");
// imagejpeg($image, null, 90);
if ($SourceFile <> '') {
imagejpeg ($image, $SourceFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image, null, 100);
};
imagedestroy($image);
return 1; // you can remove it...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.