如何在 PHP 中的图像上添加图像(如水印)

发布于 2024-07-30 02:36:00 字数 2136 浏览 7 评论 0原文

我正在尝试编写一个小型 php 函数来获取图像并在图像顶部应用水印类型图像,并将它们保存为 1 个图像,此代码运行时出现 0 错误,但不应用水印图像,有什么明显的吗为什么它不会呢?

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

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

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

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

发布评论

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

评论(2

猫烠⑼条掵仅有一顆心 2024-08-06 02:36:00

我认为你需要改变这个(这里有一个简单的教程 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)))

with:

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

或使用 wideImage ;)

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

with:

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

or use wideImage ;)

一花一树开 2024-08-06 02:36:00

以下是有关如何使用透明 PNG 图像制作水印的教程: http: //www.sitepoint.com/article/watermark-images-php/

我以前用过它并且它有效。 您所拥有的代码的问题可能是您正在尝试生成 jpeg,但文件扩展名是 gif,因此它无法正确呈现。 但也可能是错的。

如果它只是没有保存最终图像,那么这只是文件夹的权限问题。 在这种情况下,您很可能必须通过 chmod 将文件夹设置为 777。

Here's a tutorial on how to make it from a watermark using a transparent PNG image: http://www.sitepoint.com/article/watermark-images-php/

I've used that before and it works. The issue with the code you have is probably that you're trying to generate a jpeg, but the file extension is a gif, so it's not rendering correctly. Could be be wrong though.

If it's just not saving out a final image, then that's just a permissions problem with the folder. In which case, you most likely have to set the folder to 777 via chmod.

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