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

问题描述:

我需要创建一个水印将其应用于图片并将其另存为一个不同的名称。目前的脚本工作得很好,但唯一的问题是我需要增加“示例文本”的大小并将背景从黑色变为白色。我尝试了不同的场景,改变了不透明度,但仍然无法更改背景颜色。php,gd,创建水印,更改水印文本大小和背景颜色,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); 

此代码运行速度不够快,我们用它在飞行中从我们的照片部分照片添加动态标注,因为它们可以下载,而不是将它们保存到磁盘。

+0

我需要将带水印的图片保存在磁盘上,以便稍后在网站上上传。 – Michael 2010-09-15 19:40:34

+0

所以只需删除第二行并更改最后一行(用您的文件名替换'null') – Alnitak 2010-09-15 19:47:01

+0

非常感谢,您让我的一天! – Michael 2010-09-15 20:09:42