今天的实现目标就是使用GD库完成对图片加水印和图

片缩略图两个功能

动身前逻辑准备

属性:路径功能:构造方法生成水印的方法获取图片信息获取位置信息(123 456 789)创建图片资源合并图片资源重新命名保存图片生成缩略图的方法获取图片信息获取需要缩放的图片大小创建图片资源处理一下可能出现的黑边问题重新命名保存图片

接下来将文字描述转换为代码

首先,创建一个Image类,包含属性和构造方

class Image(){//定义图片路径static $path = '/';//构造方法public function __construct($path = '/') {$this->path = rtrim($path,'').'/';}}

接下来完善类中的方法

  • 获取图片信息
static private function getImageInfo($imgPath){$info = getimagesize($imgPath);return array('width' => $info[0],'height'=> $info[1],'mime'  => $info['mime'],'name'  => basename($imgPath));}
  • 检查目标图片与水印图片大小是不是

匹配

static private function checksize($dstImgInfo,$waterImgInfo){if($waterImgInfo['width'] > $dstImgInfo['width'] || $waterImgInfo['height'] > $dstImgInfo['height']){return false;} else {return true;}}
  • 根据需要

确定水印的位置

这里将图片分成九宫格

1 2 3

4 5 6

7 8 9

static private function getPosition($dstImgInfo,$waterImgInfo,$postion){switch ($position) {case 1:$x = 0;$y = 0;break;case 2:$x = ceil(($desImgInfo['width'] - $waterImgInfo['width']) / 2);$y = 0;break;case 3:$x = $dstImgInfo['width'] - $waterImgInfo['width'];$y = 0;case 4:$x = 0;$y = ceil(($dstImgInfo['height'] - $waterImgInfo['height']) / 2);break;case 5:$x = ceil(($desImgInfo['width'] -$waterImgInfo['width']) / 2);$y = ceil(($dstImgInfo['height'] - $waterImgInfo['height']) / 2);break;case 6:$x = $dstImgInfo['width'] - $waterImgInfo['width'];$y = ceil(($dstImgInfo['height'] - $waterImgInfo['height']) / 2);break;case 7:$x = 0;$y = $dstImgInfo['height'] - $waterImgInfo['height'];break;case 8:$x = ceil(($desImgInfo['width'] - $waterImgInfo['width']) / 2);$y = $dstImgInfo['height'] - $waterImgInfo['height'];break;case 9:$x = $dstImgInfo['width'] - $waterImgInfo['width'];$y = $dstImgInfo['height'] - $waterImgInfo['height'];}return ['x' => $x,'y' => $y];}
  • 根据图片的mime类型创建图片资源
static private function createImgRes($imgPath,$imgInfo){switch ($imgInfo['mime']) {case 'image/jpg':case 'image/jpeg':case 'image/pjpeg':$res = imagecreatefromjpeg($imgPath);break;case 'image/png':case 'image/x-png':$res = imagecreatefrompng($imgPath);break;case 'image/gif':$res = imagecreatefromgif($imgPath);break;case 'image/bmp':case 'image/wbmp':$res = imagecreatefromwbmp($imgPath);break;default:exit('图片我们不支持!');break;}return $res;}
  • 给原图加上水印
static private function merge($dstImgRes,$waterImgRes,$waterImgInfo,$pos,$opacity){imagecopymerge($dstImgRes, $waterImgRes, $pos['x'], $pos['y'], 0, 0, $waterImgInfo['width'], $waterImgInfo['height'], $opacity);return $dstImgRes;}
  • 保存图片
static private function saveImg($newImgRes,$newPath,$dstImgInfo){switch ($dstImgInfo['mime']) {case 'image/jpg':case 'image/jpeg':case 'image/pjpeg':$res = imagejpeg($newImgRes,$newPath);break;case 'image/png':case 'image/x-png':$res = imagepng($newImgRes,$newPath);break;case 'image/gif':$res = imagegif($imgPath);break;case 'image/bmp':case 'image/wbmp':$res = imagewbmp($newImgRes,$newPath);break;default:exit('您的破图片我们不支持!');break;}
}
  • 完成水印生成的公共方法
/*$dstImg:原图像
 *$waterImg:水印图片
 *$postion:水印的位置
 *$prefix:最终生成水印的图片名称前缀
 *$opacity:水印透明度
 */
static public function water($dstImg,$waterImg,$position = 9,$prefix = 'water_',$opacity = 50)
{   //判断图片是否存在$dstImg = self::$path.$dstImg;if(!file_exists($dstImg)){exit('目标图片不存在');}if(!file_exists($waterImg)){exit('水印图片不存在');}//通过自己定义的函数来获取图片和水印图片的信息$dstImgInfo = self::getImageInfo($dstImg);$waterImgInfo = self::getImageInfo($waterImg);//检查图片大小的函数if(!self::checkSize($dstImgInfo,$waterImgInfo)){exit('水印图片大小不能超过目标图片的大小');}//获取水印在目标图片的位置$pos = self::getPosition($dstImgInfo,$waterImgInfo,$position);//创建图片资源,准备合并$dstImgRes = self::createImgRes($dstImg,$dstImgInfo);$waterImgRes = self::createImgRes($waterImg,$waterImgInfo);//合并图片,生成新图片$newImgRes = self::merge($dstImgRes,$waterImgRes,$waterImgInfo,$pos,$opacity);$newPath = self::$path.$prefix.uniqid().$dstImgInfo['name'];imagedestroy($dstImgRes);imagedestroy($waterImgRes);imagedestroy($newImgRes);//生成新图片self::saveImg($newImgRes,$newPath,$dstImgInfo);return $newPath;
}

上述面是给图片加水印的全部方法,接下来完成

图片缩略图

//生成缩略图的方法static public function thump($img,$width,$height,$prefix = 'thump_'){if(!file_exists($img)){exit('破图片不存在');}//获取图片的信息//getImageInfo()自定义函数$imgInfo = self::getImageInfo($img);//创建图片资源//createImgRes()自定义函数$imgRes  = self::createImgRes($img,$imgInfo);//获取新的大小//getNewSize()自定义函数--->得到最新的大小$newSize = self::getNewSize($width,$height,$imgInfo);//解决黑边问题//kidOfImage()自定义函数$newImgRes = self::kidOfImage($imgRes, $newSize, $imgInfo);//拼装缩略后的新的图片名字$newPath = $prefix.$newSize['width'].'X'.$newSize['height'].$imgInfo['name'];//保存图片self::saveImg($newImgRes,$newPath,$imgInfo);imagedestroy($newImgRes);//返回新的路径return $newPath;}
  • 重新计算缩略后的图成比例的宽

高问题

static private function getNewSize($width,$height,$imgInfo){$size = [];//判断缩略后的图的宽度,是不是比原图小if($imgInfo['width'] > $width){$size['width'] = $width;}//判断缩略图的高度是不是比原图小if($imgInfo['height'] > $height){$size['height'] = $height;}//即使小了,但是不成比例也不可以//如果缩略后的宽度是合适的,那么按照比例重新设高度if($imgInfo['width'] * $size['width'] > $imgInfo['height'] * $imgInfo['height']){$size['height'] = $imgInfo['height'] * $size['width'] / $imgInfo['width'];}else{//缩略后的高度是合适的,按照比例重新设置宽度$size['width'] = $imgInfo['width'] * $size['height'] / $imgInfo['height'];}return $size;}
  • 解决可能出现的黑边问题
    static private function kidOfImage($srcImg, $size, $imgInfo){$newImg = imagecreatetruecolor($size["width"], $size["height"]);$otsc = imagecolortransparent($srcImg);if ( $otsc >= 0 && $otsc < imagecolorstotal($srcImg)) {$transparentcolor = imagecolorsforindex( $srcImg, $otsc );$newtransparentcolor = imagecolorallocate($newImg,$transparentcolor['red'],$transparentcolor['green'],$transparentcolor['blue']);imagefill( $newImg, 0, 0, $newtransparentcolor );imagecolortransparent( $newImg, $newtransparentcolor );}imagecopyresized( $newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"] );imagedestroy($srcImg);return $newImg;}
}

使用方式

Image::water('zhyunfe-com.jpg','2.jpg');Image::thump('./zhyunfe-com.jpg',50,50);

PHP面向对象——GD库实现图片水印和缩略图相关推荐

  1. PHP用gd库给图片添加水印,php用GD库给图片添加水印

    php用GD库给图片添加文字水印,整个代码比较简单,DEMO如下: /*打开图片*/ //1.配置图片路径 $src = "aeroplane.jpg"; //2.获取图片信息 $ ...

  2. php 图片上加文字,php使用GD库实现图片上添加文字的方法(代码)

    本篇文章给大家带来的内容是关于php使用GD库实现图片上添加文字的方法(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 我们可以利用php的gd库扩展来对我们的图片进行处理,例 ...

  3. php颜色底色,PHP GD 库自定义图片背景颜色

    缘起 开发"微信推广海报"的时, 背景图片未覆盖的地方默认会被黑色填充. 而我希望改成白色背景, 以对用户更友好一些. 但是无论设置成什么颜色, 图片的背景颜色一直都是黑色, 无法 ...

  4. php gd库 图片水印,php使用GD库实现文字图片水印及缩略图教程

    我们要使用gd库就必须先打开gd库,具体如下 Windows下开启PHP的GD库支持 找到php.ini,打开内容,找到: ;extension=php_gd2.dll 把最前面的分号";& ...

  5. 使用GD库为图片打水印

    一.文字水印 文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件. 代码如下: $dst_path = 'dst.jpg'; //创建图片的实例 $dst = ...

  6. 使用php的GD库拼接图片

    本来是想用ImageMagick来实现这个功能,但是ImageMagick 的环境搭建了半天没搞定,就换成了GD库.等有时间再去研究ImageMagick吧. 为了简化业务人员的工作流程,需要在系统上 ...

  7. php 用gd库在图片上写文字,并处理文字糊模问题

    今天有个需求,用php在一张图片上写文字. 这个不是挺简单的嘛?我在一个test.php文件上,敲出6行代码,搞定 img=imagecreatefrompng("C:\Users\Admi ...

  8. php获取微信素材图片乱码,如何解决在php用gd库输出图片到微信浏览器出现乱码...

    如何解决在php用gd输出图片到微信浏览器出现乱码? 程序如下:<?php $im = imagecreate(200, 300); $white = imagecolorallocate($i ...

  9. php GD库实现图片合并、文本居中 案例:生成分享海报

    因有需求要做到用php服务端去生成分享海报,并要求把头像.文字.以及二维码跟海报背景图合并,所以研究了一下php的GD库来实现该需求 本次用到的函数介绍 getimagesize 获取图片的宽高 im ...

最新文章

  1. 「Excel技巧」Excel技巧之如何看文件里的宏?
  2. docker 限制cpu使用率
  3. VS报错 <error-type> 此声明没有存储类或类型说明符
  4. html5-常用的通用元素
  5. Spring Bean引用例子
  6. 如何使用PHP自动备份数据库
  7. java 面板 选择颜色_[代码全屏查看]-java颜色选择器
  8. 数据分析CSV模块的基本使用(以分析复杂的天气情况为例),附完整的Python代码及csv文件详解---数据可视化
  9. HDU 1085 Holding Bin-Laden Captive!
  10. 合肥工贸高级技工学校计算机系,合肥工贸高级技工学校
  11. Linux命令格式及帮助命令详解
  12. bzoj 1614: [Usaco2007 Jan]Telephone Lines架设电话线(二分+SPFA)
  13. Getting a handle on
  14. 查看服务器虚拟机版本,查看虚拟机版本命令
  15. java 页眉页脚_Java 添加Word页眉、页脚
  16. 2018年Android面试题含答案--适合中高级(上)
  17. 2022软件测试校招笔试题-软件测试基本理论
  18. 计算机哪个按键可以和弦,钢琴键盘和弦图解大全!作曲必看!老师和家长快收藏起来...
  19. 如何实现业务+项目一体化管理?
  20. 藏在爱情里的那些咒语,你被下蛊了么?

热门文章

  1. MySQL(十三):分区表( Partitioning Table)
  2. 男,40岁,总监,失业:职场中年人,愿你终能体面的离开
  3. 微信小程序 之 程序题
  4. win7一点计算机就卡死,Win7系统卡屏假死怎么解决
  5. Effie:陪你守候那些观影岁月
  6. 获取枚举常量的描述值Description
  7. 爱了!这个网页气泡提示组件,让你的网站更炫酷!
  8. 计算机一级云居寺,刁常宇-Zhejiang University Personal homepage
  9. 《毕 业 论 文 致 谢 大 赏》
  10. Deep Learning for 3D Point Clouds: A Survey - 3D点云的深度学习:一项调查 (IEEE TPAMI 2020)