GD库 图片水印+文字水印+缩率图+圆形图

先将图片放到本地再做水印处理,项目使用tp框架
class CurrPicture {

/**图片下载到本地*/
public function localImg($source) {
   #1.初始化一个cURL会话
    $ch = curl_init();
    #2.请求头
    $user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";
    #3.参数配置
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($ch, CURLOPT_URL, $source); //抓取url
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //是否显示头信息
    curl_setopt($ch, CURLOPT_SSLVERSION, 1); //传递一个包含SSL版本的长参数
    #4.读取url数据
    $data = curl_exec($ch); //执行一个cURL会话
    $error = curl_error($ch); //返回一条最近一次cURL操作明确的文本的错误信息。
    curl_close($ch); //关闭一个cURL会话并且释放所有资源
    #5.数据写入
    $imgInfo     = $this->getImgInfo($source);
    $suffix      = $imgInfo['suffix'];
    $destination = TEMP_PATH . md5($source) . $suffix;
    if (!file_exists($destination)) {
        $file = fopen($destination, "w+");
        fputs($file, $data);//写入文件
        fclose($file);
    }
    return $destination;
}

获取图片信息,后缀名等

/**获取图片信息*/
public function getImgInfo($imgUrl){
    #.图片资源
    $resource = '';
    #.图片后缀名
    $suffix = '';
    #.图片详细信息
    $size = [];
    $size = getimagesize($imgUrl);
    switch($size['mime']){
        case image_type_to_mime_type(IMAGETYPE_GIF):
            $resource = imagecreatefromgif($imgUrl);
            $suffix   = image_type_to_extension(IMAGETYPE_GIF);
            break;
        case image_type_to_mime_type(IMAGETYPE_JPEG):
            $resource = imagecreatefromjpeg($imgUrl);
            $suffix   = image_type_to_extension(IMAGETYPE_JPEG);
            break;
        case image_type_to_mime_type(IMAGETYPE_PNG):
            $resource = imagecreatefrompng($imgUrl);
            $suffix   = image_type_to_extension(IMAGETYPE_PNG);
            break;
        default:
            $resource = imagecreatefrompng($imgUrl);
            $suffix   = image_type_to_extension(IMAGETYPE_PNG);
    }
    return ['resource'=>$resource, 'suffix'=>$suffix, 'mime'=>$size['mime']];
}

生成缩率图

/**
**参数:
**@param $orgImg  string 本地图片地址
**@param $saveImg string 缩率图保存本地地址
**@param $sx      int    新的宽
**@param $sy      int    新的高
*/
public function getSmallImg($orgImg, $savePath, $sx=100, $sy=100) {
   #1.打开图片源文件资源
    $imgInfo = $this->getImgInfo($orgImg);
    $im      = $imgInfo['resource'];
    $suffix  = $imgInfo['suffix'];
    #2.获得源文件的宽高
    $fx = imagesx($im);
    $fy = imagesy($im);
    #3.使用固定的公式计算新的宽高(高宽为原图的一半,50%等比缩率)
	//$sx = $sx/2;
	//$sy = $sy/2;
    #4.生成目标图像资源
    $small = imagecreatetruecolor($sx,$sy);
    $white = imagecolorallocate($small, 255, 255, 255); #白色背景
    imagefill($small,0,0,$white); #填充画布
    #5.进行缩放
    imagecopyresampled($small,$im,0,0,0,0,$sx,$sy,$fx,$fy);
    #6.保存图像
    switch($suffix){
        case image_type_to_extension(IMAGETYPE_GIF):
            //header('content-type: image/gif');
            imagegif($small,$savePath.$suffix);
            break;
        case image_type_to_extension(IMAGETYPE_JPEG):
            //header('content-type: image/jpg');
            imagejpeg($small,$savePath.$suffix);
            break;
        case image_type_to_extension(IMAGETYPE_PNG):
            //header('content-type: image/png');
            imagepng($small,$savePath.$suffix);
            break;
        default:
            //header('content-type: image/png');
            imagepng($small,$savePath.$suffix);
    }
    #7.释放资源
    imagedestroy($im);
    imagedestroy($small);
    return $savePath.$suffix;
}

生成圆形图

/**
**参数:
**@param $orgImg  string 本地图片地址
**@param $saveImg string 圆形图保存本地地址
*/
public function getRoundImg($orgImg, $savePath) {
   #1.打开图片源文件资源
    $imgInfo = $this->getImgInfo($orgImg);
    $im      = $imgInfo['resource'];
    $suffix  = $imgInfo['suffix'];
    #2.获得源文件的宽高
    $w = imagesx($im);
    $h = imagesy($im);
    #3.生成目标图像资源
    $w = $h = min($w, $h);
    $round = imagecreatetruecolor($w, $h);//创建新的真彩色图像
    imagesavealpha($round, true);//保存PNG图像时是否保留完整的alpha通道信息
    $transparent = imagecolorallocatealpha($round, 255, 255, 255, 127);//给画布分配颜色(全透明)
    imagefill($round, 0, 0, $transparent);//给画布填充颜色
    #4.
    $r = $w / 2; // 圆的半径
    for ($x = 0; $x < $w; $x++) {
        for ($y = 0; $y < $h; $y++) {
            $rgbColor = imagecolorat($im, $x, $y);//获取像素颜色的索引
            if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))){
                imagesetpixel($round, $x, $y, $rgbColor);//设置单个像素
            }
        }
    }
    #6.保存图像
    switch($suffix){
        case image_type_to_extension(IMAGETYPE_GIF):
            //header('content-type: image/gif');
            imagegif($round,$savePath.$suffix);
            break;
        case image_type_to_extension(IMAGETYPE_JPEG):
            //header('content-type: image/jpg');
            imagejpeg($round,$savePath.$suffix);
            break;
        case image_type_to_extension(IMAGETYPE_PNG):
            //header('content-type: image/png');
            imagepng($round,$savePath.$suffix);
            break;
        default:
            //header('content-type: image/png');
            imagepng($round,$savePath.$suffix);
    }
    #5.释放资源
    imagedestroy($im);
    imagedestroy($round);
    return $savePath.$suffix;
}

水印

/**
*参数:
* @param $baseUrl    string 本地底图路径
* @param $headUrl    string 本地头像路径
* @param $qrcodeUrl  string 本地二维码路径
* @param $nickName   string 用户昵称
* @param $topicName  string 直播间名称
* @param $localPath  string 水印图保存本地地址
*/
public function getWatermarkImg($baseUrl, $headUrl, $qrcodeUrl, $nickName, $topicName, $localPath) {
    #1.生成缩率图
    $headPath    = TEMP_PATH.md5($headUrl.'small');
    $qrcodePath  = TEMP_PATH.md5($qrcodeUrl.'small');
    $headSmall   = $this->getSmallImg($headUrl, $headPath, 100, 100);
    $qrcodeSmall = $this->getSmallImg($qrcodeUrl, $qrcodePath);
    #2.生成圆形图
    //$headRound = TEMP_PATH.md5($headUrl.'round');
    //$this->getRoundImg($headSmall, $headRound);
    #3.底图水印处理
    $baseInfo    = $this->getImgInfo($baseUrl);
    $im          = $baseInfo['resource'];
    $suffix      = $baseInfo['suffix'];
    //$headInfo  = $this->getImgInfo($headRound);
    //$headStamp = $headInfo['resource']; //加载头像水印 - 圆图
    $headInfo    = $this->getImgInfo($headSmall);
    $headStamp   = $headInfo['resource']; //加载头像水印 - 方图
    $qrcodeInfo  = $this->getImgInfo($qrcodeUrl);
    $qrcodeStamp = $qrcodeInfo['resource']; //加载二维码水印
    #4.图片水印
    imagecopy($im, $headStamp, 55, 1170, 0, 0, imagesx($headStamp), imagesy($headStamp));
    imagecopy($im, $qrcodeStamp, 360, 1450, 0, 0, imagesx($qrcodeStamp), imagesy($qrcodeStamp));
    #5.文字水印
    $black = imagecolorallocate($im, 0, 0, 0);
    $gray  = imagecolorallocate($im, 96, 96, 96);
    imagettftext($im, 28, 0, 180, 1235, $black, './Public/Admin/Images/simhei.ttf', $nickName);
    imagettftext($im, 24, 0, 550, 1235, $gray, './Public/Admin/Images/simhei.ttf', '给您分享了'.$topicName);
    #6.输出图像并释放内存
    unlink($baseUrl);
    unlink($headUrl);
    unlink($qrcodeUrl);
    unlink($headRound);
    unlink($headSmall);
    unlink($qrcodeSmall);
    switch($suffix){
        case image_type_to_extension(IMAGETYPE_GIF):
            //header('content-type: image/gif');
            imagegif($im, $localPath.$suffix);
            break;
        case image_type_to_extension(IMAGETYPE_JPEG):
            //header('content-type: image/jpg');
            imagejpeg($im, $localPath.$suffix);
            break;
        case image_type_to_extension(IMAGETYPE_PNG):
            //header('content-type: image/png');
            imagepng($im, $localPath.$suffix);
            break;
        default:
            //header('content-type: image/png');
            imagepng($im, $localPath.$suffix);
    }
    imagedestroy($im);
    return $localPath.$suffix;
}

}

public function createPoster(){
	$baseMap   = 'https://static.shopshops.com.cn/debug_activity_topic_5cb4540f07cdcc50018b456b.png';
	$headImg   = 'http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqd8q3j0eDD7a31d85djPbo2OCTzLfpIibnAQAN5efiaptL0lHoLOhW6TuOwhVYUo4cEgibhiaS4wCKHw/132';
	$nickName  = '微信昵称';
	$topicName = '直播间名称';
	#.图片处理(使用GD库给图片添加水印,需要使用本地图片链接,流程:先下载到本地TEMP_PATH处理完后再清除)
	$picture   = new CurrPicture();
	#.2底图(七牛地址)头像下载本地
    $baseMap   = $picture->localImg($baseMap);
    $headImg   = $picture->localImg($headImg);
    #.3创建二维码
    vendor('Barcode1.qrcode');
    $qrcode    = new \qrcode1();
    $imgbase64 = $qrcode->generateCode('https://www.baidu.com/'); //二维码内容
    $img       = base64_decode($imgbase64->generate());
    $qrcodeImg = TEMP_PATH.md5($nickName.'_qrcode').".png";
    file_put_contents($qrcodeImg, $img);
    #.4生成带水印的海报图
    $localPath = TEMP_PATH.md5($nickName.'_poster');
    $localImg  = $picture->getWatermarkImg($baseMap, $headImg, $qrcodeImg, $nickName, $topicName, $localPath);
    if(file_exists($localImg)){
		#.5图片上传七牛,返回七牛地址
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值