PHP 生成水印图片

这段时间因工作需要,学习了下用PHP来给背景图上添加公司logo,宣传语之类的图片合并功能。话不多说,直接上代码。 

<?php

public function getImage()
{
    $data_set = array(
        "text" => "这是测试文字"
    );

    // 项目根目录
    $path = __DIR__."/路径/";

    // 背景图
    $tem_bg = __DIR__."/项目存放背景图得路径/背景图.jpg";

    // 最终合成图片得路径和名字
    $final_path = "/img/.../...之类得路径/用户ID-bg.jpg";

    $bg = $path.$final_path;
    copy($tem_bg, $bg); // 复制图片
    list($bgWidth, $bgHeight) = getimagesize($bg); // 获取图片宽高

    // 字体文件路径
    $path_src = __DIR__."/字体文件路径";

    // 如果是需要生成文字图片,则调用下面方法,生成文字图片,再合成
    $text_image = $this->text_image_png($path, $path_src, $data_set);

    // 获取文字图片得宽高
    list($textWidth, $textHeight) = getimagesize($text_image);

    // 将背景图和文字图合成
    $this->synthetic($bg, 'jpg', $bgWidth, $bgHeight, $text_image, $textHeight, $textWidth, "bottom-center", 0, 210);
    unlink($text_image); // 删除临时合成得文字图片

    // 如果不需要生成文字图片,直接使用现有的图片,则执行下面的,这2种方式可共存
    $image = __DIR__."/图片路径/xxx.png";
    list($logoWidth, $logoHeight) = getimagesize($image);
    // 将背景图和文字图合成
    $this->synthetic($bg, 'jpg', $bgWidth, $bgHeight, $image, $logoHeight, $logoWidth, "bottom-center", 0, 210, 0, 10);

    // 如果需要合成多个图片,那就执行多次$this->synthetic()方法

    return array("width" => $bgWidth, "height" => $bgHeight, "path" => $bg_name); // path就是合成后图片的路径
}


/**
 * 生成文字图片
 * @param $path 项目根目录
 * @param $font_path 字体文件路径
 * @param $data_set 需要生成文字图片的文字
 * @return string
 */
public function text_image_png($path, $font_path, $data_set)
{
    $bgwidth = 640; // 文字图片的宽
    $bgheight = 500; // 文字图片的高
    $block = imagecreatetruecolor($bgwidth, $bgheight);//建立一个画板
    $bg = imagecolorallocatealpha($block , 255 , 255 , 255 , 127);// 拾取一个完全透明的颜色,不要用imagecolorallocate拾色
    $color = imagecolorallocate($block,137,54,20); //字体拾色
    imagealphablending($block , false);//关闭混合模式,以便透明颜色能覆盖原画板
    imagefill($block , 0 , 0 , $bg);//填充
    $font_file = $font_path.'/fonts/simhei.ttf'; // 字体文件

    $color = imagecolorallocate($block,255,228,116); //字体拾色

    // 文字
    $text = $data_set["text"];
    imagettftext($block,50,0,260,150,$color,$font_file,$text); // 向图像写入文本

    imagesavealpha($block , true);//设置保存png时保留透明通道信息
    $image_path = $path."/临时文件路径/text.png";
    imagepng($block, $image_path);//生成图片
    imagedestroy($block); // 销毁画板

    return $image_path;
}


/**
 * 合并图片
 *
 * @param $image // 背景图
 * @param $mimeType // 合成后的图片类型
 * @param $imgWidth // 背景的宽
 * @param $imgHeight // 背景的高
 * @param $watermark // 需要合成的图
 * @param $watermarkHeight // 需要合成的高
 * @param $watermarkWidth // 需要合成的宽
 * @param string $position // 居中类型
 * @param int $mt // 设置margin-top值
 * @param int $mb // 设置margin-bottom值
 * @param int $ml // 设置margin-left值
 * @param int $mr // 设置margin-right值
 * @param string $watermakMimeType // 合成后图片的类型
 * @throws \Exception
 */
public function synthetic($image, $mimeType, $imgWidth, $imgHeight, $watermark, $watermarkHeight, $watermarkWidth, $position = "center", $mt = 0, $mb = 0, $ml = 0, $mr = 0, $watermakMimeType = "png")
{
    // Calculate the watermark position
    switch ($position) {
        case "center":
            $marginBottom  = round($imgHeight / 2);
            $marginRight   = round($imgWidth / 2) - round($watermarkWidth / 2);
            break;

        case "top-left":
            $marginBottom  = round($imgHeight - $watermarkHeight);
            $marginRight   = round($imgWidth - $watermarkWidth);
            break;

        case "bottom-left":
            $marginBottom  = 5;
            $marginRight   = round($imgWidth - $watermarkWidth);
            break;

        case "top-right":
            $marginBottom  = round($imgHeight - $watermarkHeight);
            $marginRight   = 5;
            break;

        case "bottom-center":
            $marginBottom  = $mb;
            $marginRight   = round($imgWidth / 2) - round($watermarkWidth / 2) + $mr;
            break;

        default:
            $marginBottom  = 2;
            $marginRight   = 2;
            break;
    }

    if($watermakMimeType == "png") {
        $watermark = imagecreatefrompng($watermark); // 生成png图片
    } else {
        $watermark = imagecreatefromjpeg($watermark); // 生成jpg图片
    }

    switch ($mimeType) {
        case "jpeg":
        case "jpg":
            $createImage = imagecreatefromjpeg($image);
            break;

        case "png":
            $createImage = imagecreatefrompng($image);
            break;

        case "gif":
            $createImage = imagecreatefromgif($image);
            break;

        default:
            $createImage = imagecreatefromjpeg($image);
            break;
    }

    $sx = imagesx($watermark);
    $sy = imagesy($watermark);
    imagecopy(
        $createImage,
        $watermark,
        imagesx($createImage) - $sx - $marginRight,
        imagesy($createImage) - $sy - $marginBottom,
        0,
        0,
        imagesx($watermark),
        imagesy($watermark)
    ); // 拷贝图像的一部分


    switch ($mimeType) {
        case "jpeg":
        case "jpg":
            imagejpeg($createImage, $image); // 以 jpeg 格式将图像输出到浏览器或文件
            break;

        case "png":
            imagepng($createImage, $image); // 以 PNG 格式将图像输出到浏览器或文件
            break;

        case "gif":
            imagegif($createImage, $image); // 以 gif 格式将图像输出到浏览器或文件
            break;

        default:
            throw new \Exception("A watermark can only be applied to: jpeg, jpg, gif, png images ");
            break;
    }
}

以上就是将多个图片合成一张图片的过程。

posted @ 2018-07-24 10:53  陈浩宇人呢  阅读(1026)  评论(0编辑  收藏  举报