php,gd,创建水印,更改水印文本大小和背景颜色,imagecreatefromjpeg [英] php , gd, create watermark, change watermark text size and background color, imagecreatefromjpeg

查看:106
本文介绍了php,gd,创建水印,更改水印文本大小和背景颜色,imagecreatefromjpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建水印,然后将其应用到图片上并以其他名称保存.当前脚本运行良好,但是唯一的问题是我需要增加示例文本"的大小并将背景从black更改为white.我尝试了不同的方案,更改了不透明度,但仍然无法更改背景色.

I need to create a watermark apply it on a picture and save it with a different name . The current script works pretty well but the only problem is that I need to increase the size of the "sample text" and change the background from black to white . I tried different scenarios , changed the opacity but still can't change the background color.

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

推荐答案

为什么要全部使用图章?我在一个网站上使用以下代码:

Why use a stamp at all? I use the following code on one of my sites:

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

此代码的运行速度足够快,我们可以使用它在下载时将动态标签动态添加到照片部分中的照片中,而不是将其保存到磁盘中.

This code runs fast enough that we use it to add dynamic labels on the fly to photos from our photo section as they're downloaded, rather than save them to disk.

这篇关于php,gd,创建水印,更改水印文本大小和背景颜色,imagecreatefromjpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆