PHP利用GD库给图片添加水印

/*
 * 添加水印
 * $imgSrc:目标图片,可带相对目录地址,
 * $markImg:水印图片,可带相对目录地址,支持PNG和GIF两种格式,如水印图片在执行文件mark目录下,可写成:mark/mark.gif
 * $markPos:图片水印添加的位置,取值范围:0~9
 * 0:循环平铺水印
 * 1:顶部居左 2:顶部居中 3:顶部居右 4:左边居中
 * 5:图片中心 6:右边居中 7:底部居左 8:底部居中 9:底部居右
 * */
protected static function addWatermark($imgSrc, $markImg, $markPos = 0)
{
    //如果图片不存在或者logo不存在则不处理
    if (!file_exists($imgSrc) || !file_exists($markImg)) {

        return [
            'status' => false,
            'message' => '目标图片或水印图片不存在!'
        ];
    }
    //获取图、水印 宽高类型
    list($srcImg_w, $srcImg_h, $srcImg_t) = getimagesize($imgSrc);
    list($markImg_w, $markImg_h, $markImg_t) = getimagesize($markImg);

    if ($srcImg_w < $markImg_w || $srcImg_h < $markImg_h) {

        return [
            'status' => false,
            'message' => '水印图片尺寸不能大于目标图片尺寸!'
        ];
    }

    switch ($srcImg_t) {

        case 1:
            $srcim = imagecreatefromgif($imgSrc);
            break;
        case 2:
            $srcim = imagecreatefromjpeg($imgSrc);
            break;
        case 3:
            $srcim = imagecreatefrompng($imgSrc);
            break;
        default:
            return [
                'status' => false,
                'message' => '不支持的图片文件类型!'
            ];
    }

    switch ($markImg_t) {

        case 1:
            $markim = imagecreatefromgif($markImg);
            break;
        case 2:
            $markim = imagecreatefromjpeg($markImg);
            break;
        case 3:
            $markim = imagecreatefrompng($markImg);
            break;
        default:
            return [
                'status' => false,
                'message' => '不支持的图片文件类型!'
            ];
    }

    $logow = $markImg_w;
    $logoh = $markImg_h;

    $dst_img = imagecreatetruecolor($srcImg_w, $srcImg_h);

    imagecopy($dst_img, $srcim, 0, 0, 0, 0, $srcImg_w, $srcImg_h);

    imagedestroy($srcim);

    if ($markPos) {
        //图片水印添加的位置
        switch ($markPos) {

            case 1:
                $x = +5;
                $y = +5;
                break;
            case 2:
                $x = ($srcImg_w - $logow) / 2;
                $y = +5;
                break;
            case 3:
                $x = $srcImg_w - $logow - 5;
                $y = +15;
                break;
            case 4:
                $x = +5;
                $y = ($srcImg_h - $logoh) / 2;
                break;
            case 5:
                $x = ($srcImg_w - $logow) / 2;
                $y = ($srcImg_h - $logoh) / 2;
                break;
            case 6:
                $x = $srcImg_w - $logow - 5;
                $y = ($srcImg_h - $logoh) / 2;
                break;
            case 7:
                $x = +5;
                $y = $srcImg_h - $logoh - 5;
                break;
            case 8:
                $x = ($srcImg_w - $logow) / 2;
                $y = $srcImg_h - $logoh - 5;
                break;
            case 9:
                $x = $srcImg_w - $logow - 5;
                $y = $srcImg_h - $logoh - 5;
                break;
            default:
                return [
                    'status' => false,
                    'message' => '此位置不支持!'
                ];
        }

        imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh);

    } else {
        //循环平铺水印
        for ($x = 0; $x < $srcImg_w; $x++) {
            for ($y = 0; $y < $srcImg_h; $y++) {
                imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh);
                $y += $logoh;
            }
            $x += $logow;
        }
    }

    imagedestroy($markim);

    switch ($srcImg_t) {

        case 1:
            $status = imagegif($dst_img, $imgSrc);
            break;
        case 2:
            $status = imagejpeg($dst_img, $imgSrc);
            break;
        case 3:
            $status = imagepng($dst_img, $imgSrc);
            break;
        default:
            $status = false;
    }

    imagedestroy($dst_img);

    if ($status) {
        $res = [
            'status' => true,
            'message' => '添加水印图片成功!'
        ];
    } else {
        $res = [
            'status' => false,
            'message' => '添加水印图片失败!'
        ];
    }

    return $res;
}
上一篇:Flash中实现语音变声(下)


下一篇:利用时间维度统计分页