PHP 图片增加图片或文字水印

/**
 * 图片增加图片或文字水印
 *
 * @param int $position 位置1~9分别代表了左上角到右下角 依次类推
 */
function bornshareqrode(
    $bannerimg,
    $qrodeimg,
    $position,
    $uid = '',
    $width = '200',
    $heigth = '200'
) {
    $arr = array();
    $arr = positionImg($position, $bannerimg, $width, $heigth);

    if ($bannerimg) {
        //生成中间带logo的二维码
        //这是合成后的图片保存的路径
        $upload_dir = "/public/upload/comp_pic/";
        if (is_file($bannerimg)) {
            //创建画布
            $logo      = imagecreatefromstring(file_get_contents($bannerimg));
            $head_img1 = imagecreatefromstring(file_get_contents($qrodeimg));
            //将$qrodeimg插入到$bannerimg里
            imagecopyresampled($logo, $head_img1, $arr[0], $arr[1], 0, 0, $width, $heigth, imagesx($head_img1),
                imagesy($head_img1));

            $imgName = 'u_'.$uid.'_'.time().'.png';
            //生成图片
            imagepng($logo, ROOT_PATH.$upload_dir.$imgName);
            //生成图片名字
            $twocode = $upload_dir.$imgName;
        }

        return $twocode;//返回结果图片url
    } else {
        return false;
    }
}

function positionImg($position, $imgPath, $width, $height)
{
    $arr = array();
    list($source_w, $source_h) = getimagesize($imgPath);
    $source_w = $source_w * 0.97;
    $source_h = $source_h * 0.98;
    switch ($position) {
        case 1:
            $wx = 5;
            $wy = 5;
            break;
        case 2:
            $wx = ($source_w - $width) / 2;
            $wy = 0;
            break;
        case 3:
            $wx = $source_w - $width;
            $wy = 0;
            break;
        case 4:
            $wx = 0;
            $wy = ($source_h - $height) / 2;
            break;
        case 5:
            $wx = ($source_w - $width) / 2;
            $wy = ($source_h - $height) / 2;
            break;
        case 6:
            $wx = $source_w - $width;
            $wy = ($source_h - $height) / 2;
            break;
        case 7:
            $wx = 0;
            $wy = $source_h - $height;
            break;
        case 8:
            $wx = ($source_w - $width) / 2;
            $wy = $source_h - $height;
            break;
        case 9:
            $wx = $source_w - $width;
            $wy = $source_h - $height;
            break;
        case 0:
            $wx = rand(0, ($source_w - $width));
            $wy = rand(0, ($source_h - $height));
            break;
        default:
            $wx = rand(0, ($source_w - $width));
            $wy = rand(0, ($source_h - $height));
            break;
    }

    $arr[] = (int)$wx;
    $arr[] = (int)$wy;
    dump($arr);

    return $arr;
}

使用方法:

public function test()
{
    $bannerimg = ROOT_PATH.'/public/upload/comp_pic/img_1.png';
    $qrodeimg  = ROOT_PATH.'/public/upload/comp_pic/qr_1.png';
    $uid       = 11;
    $imgShare  = bornshareqrode($bannerimg, $qrodeimg, 9, $uid, 90, 90);
    halt($imgShare);
}
PHP 图片增加图片或文字水印插图
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    blank

    暂无评论内容