如何将图像添加到像水印一样的PHP图像中 [英] How can I add an image onto an image in PHP like a watermark

查看:102
本文介绍了如何将图像添加到像水印一样的PHP图像中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个小的php函数来拍摄图像并将水印类型的图像应用于图像顶部,并将它们保存为1张图像,此代码运行0错误,但未应用水印图像,是有什么明显的理由表明为什么不这样做?

I am trying to write a small php function to take an image and apply a watermark type image on top of the image and them save them as 1 image, this code runs with 0 errors but does not apply the watermark image, is there anything obvious that stants out on why it won't?

<?PHP
$source_file_path ='http://cache2.mycrib.net/images/image_group92/0/43/653807d5727a46498180e8ef57fdf7819b2b0c.jpg';
$watermark_image='fpwatermark.gif'; // the watermark image
$destination_image ='coooolgif.gif'; // where to save new file

$imagesize = getimagesize($destination_image);
$watermarksize = getimagesize($watermark_image);
$watermark_x = $imagesize[0] - $watermarksize[0] - 2;
$watermark_y = $imagesize[1] - $watermarksize[1] - 2;

//run function
watermark_img($watermark_image, $destination_image, $watermark_x, $watermark_y, $watermark_w, $watermarksize[0], $watermarksize[1], $source_file_path);


function watermark_img($watermark_src, $image_src, $watermark_x, $watermark_y, $watermark_w,$watermark_h, $source_file_path) {
    //Determine what type of image we're working with
    list($width, $height, $type) = getimagesize($image_src);
    $image_ext = $type;
    switch (strtolower($image_ext)) {
            #gif
        case 1:
            $image = imagecreatefromgif($image_src);
            break;
            #jpg
        case 2:
            $image = imagecreatefromjpeg($image_src);
            imageAlphaBlending($image, true);
            break;
            #png
        case 3:
            $image = imagecreatefrompng($image_src);
            imageAlphaBlending($image, true);
            break;
        default:
            return false;
    }
    //Create an instance of the watermark in memory
    if (!($watermark = imagecreatefromgif($watermark_src)))
        return false; //Make sure your Watermark is a GIF
    //Add watermark to the image
    if (!(imagecopy($image, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_w,
        $watermark_h)))
        return false;
    //Resave the image with the watermark now in place
    if (!(imagejpeg($image, $image_src)))
        return false;
    //Destroy instaces of images to free up RAM
    imagedestroy($image);
    imagedestroy($watermark);
    //Apparently everything went well.
    return $image_ext;
}
?>

推荐答案

我认为您需要更改此设置(这里是一个简单的教程 http://www.cafewebmaster.com/image-watermark-php ):

I think you need to change this (here is a simple tutorial http://www.cafewebmaster.com/image-watermark-php):

//Add watermark to the image
if (!(imagecopy($image, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_w,
    $watermark_h)))

具有:

//Add watermark to the image
if (!(imagecopymerge($image, $watermark, $watermark_x, $watermark_y, 0, 0, $watermark_w,
    $watermark_h, 100)))

或使用 wideImage ;)

这篇关于如何将图像添加到像水印一样的PHP图像中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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