php , gd, 创建水印, 更改水印文本大小和背景颜色, imagecreatefromjpeg

发布于 2024-09-19 11:00:39 字数 1145 浏览 7 评论 0原文

我需要创建一个水印,将其应用到图片上并使用不同的名称保存。当前的脚本运行得很好,但唯一的问题是我需要增加“示例文本”的大小并将背景从黑色更改为白色。我尝试了不同的场景,改变了不透明度,但仍然无法改变背景颜色。

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

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

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

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

发布评论

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

评论(2

蓝戈者 2024-09-26 11:00:39

为什么要使用邮票?我在我的一个网站上使用以下代码:

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

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

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.

乱了心跳 2024-09-26 11:00:39

在我的代码中,我修复了一些常见错误,例如

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...
}

here in my code, i fixed some common mistakes like :

1) Watermark Text goes outside of the image.

2) PNG & JPG image error.

So i calculate image width and decide font size. so font size is Dynamic.

so you can just copy my method and paste where you want to use it.

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...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文