1. <?php
  2. /**
  3. * 常见图像处理函数的封装
  4. */
  5. class Image{
  6. private $info=[];
  7. private $width;//原始图片宽度
  8. private $height;//图片原始高度
  9. private $mime;//图片mime类型 自 PHP 4.3.0 起,getimagesize() 还会返回额外的参数 mime,符合该图像的 MIME 类型
  10. private $image;//图像资源
  11. private $format;//图像格式
  12. private $ratio;//角度
  13. // 1、打开图片 读取到内存中
  14. public function __construct($src){
  15. $this->info=getimagesize($src);
  16. $this->width=$this->info['0'];
  17. $this->height=$this->info['1'];
  18. $this->mime=$this->info['mime'];
  19. $func=str_replace('/', 'createfrom', $this->mime);
  20. $filearray=explode(".",$src);
  21. //end() 将 array 的内部指针移动到最后一个单元并返回其值。 mixed end ( array &$array )
  22. $this->format=strtolower(end($filearray));//strtolower(end(explode('.', $src))) Only variables should be passed by reference不能连在一起写
  23. $this->image=$func($src);//返回一图像标识符,代表了从给定的文件名取得的图像。
  24. $this->ratio=rad2deg(atan2($this->height,$this->width));
  25.  
  26. }
  27. /**
  28. * [thumb description]
  29. * 操作图片 压缩
  30. * @DateTime 2018-07-27T16:10:38+0800
  31. * @param [type] $width [图片宽度或者最大宽度]
  32. * @param [type] $height [图片高度或者最大高度]
  33. * @param boolean $flag [是否等比例缩放]
  34. * @param integer $scale [缩放比例为0时不缩放按宽高比]
  35. * @return [type] [description]
  36. */
  37. public function thumb($width=null,$height=null,$flag=true,$scale=0){
  38. $ratio=round($this->width/$this->height,2);//宽高比 比值越大图片越扁
  39. $dstratio=$width&&$height?round($width/$height,2):1;
  40. // 根据不同的情况计算缩放图的宽高
  41. if($scale){
  42. $dst_width=floor($this->width*$scale);
  43. $dst_height=floor($this->height*$scale);
  44. }
  45. if($width&&$height&&!$flag){
  46. $dst_width=$width;
  47. $dst_height=$height;
  48. }
  49. if(!$scale&&$flag){
  50. if($ratio>$dstratio){
  51. $dst_width=$width;
  52. $dst_height=floor($width/$ratio);
  53. }elseif($ratio<$dstratio){
  54. $dst_width=floor($height*$ratio);
  55. $dst_height=$height;
  56. }else{
  57. $dst_width=$width;
  58. $dst_height=$height;
  59. }
  60. }
  61. $dst_image=imagecreatetruecolor($dst_width, $dst_height);
  62. imagealphablending($dst_image , false);//关闭混合模式,以便透明颜色能覆盖原画板
  63. imagesavealpha($dst_image, true); //设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反)。 要使用本函数,必须将 alphablending 清位(imagealphablending($im, false)
  64. imagecopyresampled($dst_image, $this->image, 0, 0, 0, 0, $dst_width, $dst_height, $this->width, $this->height);
  65. imagedestroy($this->image);
  66. $this->image= $dst_image;
  67.  
  68. }
  69. /**
  70. * [addTextwatermark description]
  71. * 给图片加文字水印
  72. * @DateTime 2018-07-28T17:27:42+0800
  73. * @param [mixed] $text [要加的文字多行的话要存成数组]
  74. * @param integer $font [description]
  75. * @param integer $angle [description]
  76. * @param integer $point [description]
  77. */
  78.  
  79. public function addTextwatermark($text,$font=20,$color=[255,255,255,50],$angle=0,$point=9){
  80. $textcolor=imagecolorallocatealpha($this->image,$color[0],$color[1],$color[2],$color[3]);
  81. $angle=$angle==false?$this->ratio:$angle;
  82. $textlength=is_array($text)&&count($text)>1?count($text):1;//多行文字
  83. $textSize = imagettfbbox($font, $angle, '../fonts/simkai.ttf',$text);
  84. $textWidth = $textSize[2] - $textSize[1]; //文字的最大宽度
  85. $textHeight = $textSize[1] - $textSize[7]; //文字的高度
  86. $lineHeight = $textlength==1?$textHeight:$textHeight + 3; //文字的行高
  87. //是否可以添加文字水印 只有图片的可以容纳文字水印时才添加
  88. if ($textWidth + 40 > $this->width || $lineHeight * $textLength + 40 > $this->height) {
  89. return false; //图片太小了,无法添加文字水印
  90. }
  91. $pointxy=$this->markLocation(array('width'=>$this->width,'height'=>$this->height),array('width'=>$textWidth,'height'=>$lineHeight),$point,$angle);
  92. imagettftext($this->image, $font, $angle, $pointxy['x'],$pointxy['y']+$lineHeight*$textlength, $textcolor,'../fonts/simkai.ttf',$text);
  93.  
  94. }
  95. /**
  96. * [addPicwatermark description]
  97. * 添加图片水印
  98. * @DateTime 2018-07-30T09:40:53+0800
  99. * @param [string] $markimg [水印图片路径]
  100. * @param integer $point [水印图所处位置默认为左上角]
  101. */
  102. public function addPicwatermark($markimg,$point=1){
  103. $info=getimagesize($markimg);
  104. $markWidth=$info[0];
  105. $markHight=$info[1];
  106. $func=str_replace('/', 'createfrom', $info['mime']);
  107. $filearray=explode(".",$markimg);
  108. $format=strtolower(end($filearray));
  109. $mark_image=$func($markimg);//返回一图像标识符,代表了从给定的文件名取得的图像。
  110. imagealphablending($mark_image , false);//关闭混合模式,以便透明颜色能覆盖原画板
  111. imagesavealpha($mark_image, true);
  112. $pointxy=$this->markLocation(array('width'=>$this->width,'height'=>$this->height),array('width'=>$markWidth,'height'=>$markWidth),$point,0);
  113. imagecopy($this->image, $mark_image, $pointxy['x'],$pointxy['y'], 0, 0, $markWidth, $markHight);
  114. imagedestroy($mark_image);
  115. }
  116. /**
  117. * [createCircleimg description]
  118. * 生成圆角png图像
  119. * @DateTime 2018-07-30T11:21:33+0800
  120. * @return [type] [description]
  121. */
  122. public function createCircleimg(){
  123. $diameter=$this->width>$this->height?floor($this->height/2)*2:floor($this->width/2)*2;
  124. $Circleimg=imagecreatetruecolor($diameter, $diameter);
  125. // imagesavealpha($Circleimg, true);
  126. $bg=imagecolorallocatealpha($Circleimg, 255, 255, 255, 127);
  127. if(is_array($border)&&count($border)==3){
  128. $border_color=imagecolorallocate($Circleimg, 0, 0, 0);
  129. }else{
  130. //$border_color= imagecolorallocatealpha($Circleimg, 255, 255, 255, 127);
  131. $border_color=imagecolorallocate($Circleimg, 0, 0, 0);
  132. }
  133.  
  134. imagealphablending($Circleimg , false);
  135. imagesavealpha($Circleimg, true);
  136. $r = floor($diameter / 2);
  137. if($this->width>$this->height ){
  138. $plusx=round(($this->width-$this->height)/2);
  139. $plusy=0;
  140. }else{
  141. $plusy=round(($this->height-$this->width)/2);
  142. $plusx=0;
  143. }
  144. for ($x = 0; $x < $diameter; $x++) {
  145. for ($y = 0; $y <$diameter; $y++) {
  146. $rgbColor = imagecolorat($this->image, $x+$plusx, $y+$plusy);
  147. if (((($x-$r) * ($x-$r) + ($y-$r) * ($y-$r)) < ($r*$r))) {
  148. imagesetpixel($Circleimg, $x, $y, $rgbColor);
  149. }else{
  150. imagesetpixel($Circleimg, $x, $y, $bg);
  151. }
  152. }
  153. }
  154. imagedestroy($this->image);
  155. $this->image= $Circleimg;
  156. $this->mime='image/png';
  157. $this->format='png';
  158. }
  159.  
  160. /**
  161. * [markLocation description]
  162. * 位置函数
  163. * @DateTime 2018-07-28T17:22:17+0800
  164. * @param [array] $outerbox [外层容器宽高] array('width'=>'','height'=>'')
  165. * @param [array] $innerbox [内层容器宽高]array('width'=>'','height'=>'')
  166. * @param [int] $point [位置]$point 1、左上角 2,上居中 3,右上角 4、右居中 5,右下角 6,下居中 7,左下角 8,左居中 9、居中
  167. * @param [int] $angle [偏移角度]
  168. * @param integer $padding [内距]
  169. * @return [mixed] [成功返回位移数组]
  170. */
  171. public function markLocation($outerbox,$innerbox,$point,$angle,$padding=20){
  172.  
  173. if(!is_array($outerbox)||!is_array($innerbox)) return false;
  174. switch($point){
  175. case 1://左上角
  176. $pointLeft = $padding;
  177. $pointTop = $padding;
  178. break;
  179. case 2:
  180. $pointLeft=($outerbox['width']-$innerbox['width'])/2;
  181. $pointTop = $padding;
  182. break;
  183. case 3:
  184. $pointLeft=$outerbox['width']-$innerbox['width']-$padding;
  185. $pointTop = $padding;
  186. break;
  187. case 4:
  188. $pointLeft=$outerbox['width']-$innerbox['width']-$padding;
  189. $pointTop = ($outerbox['height']-$innerbox['height'])/2;
  190. break;
  191. case 5:
  192. $pointLeft=$outerbox['width']-$innerbox['width']-$padding;
  193. $pointTop = $outerbox['height']-$innerbox['height']-$padding;
  194. break;
  195. case 6:
  196. $pointLeft=($outerbox['width']-$innerbox['width'])/2;
  197. $pointTop = $outerbox['height']-$innerbox['height']-$padding;
  198. break;
  199. case 7://左上角
  200. $pointLeft = $padding;
  201. $pointTop = $outerbox['height']-$innerbox['height']-$padding;
  202. break;
  203. case 8:
  204. $pointLeft=$padding;
  205. $pointTop=($outerbox['height']-$innerbox['height'])/2;
  206. break;
  207. case 9:
  208. $pointLeft=($outerbox['width']-$innerbox['width'])/2;
  209. $pointTop=($outerbox['height']-$innerbox['height'])/2;
  210. break;
  211. default;
  212.  
  213. }
  214. if ($angle != 0) {
  215. if ($angle < 90) {
  216. //画一下图 根据三角关系得到偏移量
  217. $diffTop = ceil(sin($angle * M_PI / 180) * $innerbox['width']);
  218. $diffLeft = ceil(sin($angle * M_PI / 180) * $innerbox['height']);
  219. if (in_array($point, array(1, 2, 3))) {// 上部 top 值增加
  220. $pointTop += ($diffTop-$diffLeft/2);
  221. } elseif (in_array($point, array(4, 8, 9))) {// 中部 top 值根据图片总高判断
  222. if ($innerbox['width']+$innerbox['heihgt']/2 > ceil($outerbox['height'] / 2)) {
  223. $pointTop += ceil(($innerbox['width'] - $outerbox['height'] / 2) / 2);
  224. $diagonal=sqrt(pow($this->width,2)+pow($this->height,2))/2;
  225. $pointLeft= ($outerbox['width']-ceil(cos($angle * M_PI / 180) *$innerbox['width']))/2;
  226.  
  227. }
  228. }
  229. } elseif ($angle > 270) {
  230. $diffTop = ceil(sin((360 - $angle) * M_PI / 180) * $innerbox['width']);
  231.  
  232. if (in_array($point, array(1, 2, 3))) {// 上部 top 值增加
  233. $pointTop -= $diffTop;
  234. } elseif (in_array($point, array(4, 8, 9))) {// 中部 top 值根据图片总高判断
  235. if ($innerbox['width'] > ceil($outerbox['height'] / 2)) {
  236. $pointTop = ceil(($outerbox['height'] - $diffTop) / 2);
  237. }
  238. }
  239. }
  240. }
  241. return array('x'=>intval($pointLeft),'y'=>intval($pointTop));
  242. }
  243. /**
  244. * [outputInBrower description]
  245. * 把图片在浏览器输出
  246. * @DateTime 2018-07-27T16:48:33+0800
  247. * @return [type] [description]
  248. */
  249. public function outputInBrower(){
  250. header('content-type:'.$this->mime);
  251. $outfunc=str_replace('/', '', $this->mime);
  252. $outfunc($this->image);
  253. }
  254.  
  255. /**
  256. * [outputAsFile description]
  257. * 图片保存为文件
  258. * @DateTime 2018-07-27T17:00:26+0800
  259. * @param [string] $destionation [图片保存路径]
  260. * @return [type] [description]
  261. */
  262. public function outputAsFile($destionation=null){
  263. $outfunc=str_replace('/', '', $this->mime);
  264. if($destionation&&!file_exists(dirname($destionation))){
  265. mkdir(dirname($destionation),0777,true);
  266. }
  267. $randname=md5(uniqid(microtime(true),true));
  268. //$dstFilename= $destionation==null ? $destionation : $destionation.'/'.$randname.'.'.$this->format;
  269. $dstFilename= $destionation.'/'.$randname.'.'.$this->format;
  270. return $outfunc($this->image,$dstFilename);
  271. }
  272. public function __destruct(){
  273. imagedestroy($this->image);
  274. }
  275. }
  276.  
  277. 1、生成缩略图函数
  1. $test=new Image('../images/new3.jpg');
  2. $test->thumb();
  3. $test->outputAsFile('../upload');

此函数可以按等比例缩放也可以按指定宽高缩放也可以按比例缩放。

2、加文字水印 可设置水印位置,颜色,旋转角度默认是对顶角

  1. $test=new Image('../images/new3.jpg');
  2. $test->addTextwatermark('hello world');
  3. $test->outputAsFile('../upload');

3、图片水印 默认位置是左上角 addPicwatermark()

  1. $test=new Image('../images/new3.jpg');
  2. $test->addPicwatermark('../images/logo.png');
  3. $test->outputAsFile('../upload');

4、生成圆角图像 做头像createCircleimg

  1. $test=new Image('../images/new3.jpg');
  2. $test->createCircleimg();
  3. $test->outputAsFile('../upload');

总结,圆角函数跟最初设想的是有差别的,设想中是可以加个边,但是实际做的时候由于坐标是不连续的不能取到圆形精准的坐标值,希望以后能找到代替方法。而同样的原因,边缘很不光滑。

php 常见图片处理函数封装的更多相关文章

  1. c语言_常见图片格式判断

    c语言_常见图片格式判断 我想尽各种思路.今天,终于把图片判断搞定了. 在此,我写一下我的思路.希望对那些不想看代码的朋友们有帮助. 常风的的图片格式有:bmp,png,jpg,gif等图片格式. 我 ...

  2. js节流与防抖函数封装

    js节流与防抖函数封装 常见应用场景: window的 resize 和 scroll 事件: 文字输入时的 keyup 事件: 元素拖拽.移动时的 mousemove 事件: 防抖 定义:多次触发事 ...

  3. 测开之路一百三十三:实现sql函数封装

    连接数据库的频率很高,所以把数据库操作封装起来 函数封装: def make_dicts(cursor, row): """ 将游标获取的Tuple根据数据库列表转换为d ...

  4. JavaScript节流与防抖函数封装

    js节流与防抖函数封装 常见应用场景: window的 resize 和 scroll 事件: 文字输入时的 keyup 事件: 元素拖拽.移动时的 mousemove 事件: 防抖 定义:多次触发事 ...

  5. php文件操作(最后进行文件常用函数封装)

    文件信息相关API $filename="./1-file.php"; //filetype($filename):获取文件的类型,返回的是文件的类型 echo '文件类型为:', ...

  6. php 随机显示图片的函数(实例分享)

    转自:http://www.jbxue.com/article/12695.html 发布:thatboy   来源:Net     [大 中 小] 本文分享一个php实现的随机显示图片的函数,可以将 ...

  7. php 随机显示图片的函数(实例)

    转自:http://www.jbxue.com/article/12695.html   发布:thatboy   来源:Net     [大 中 小] 本文分享一个php实现的随机显示图片的函数,可 ...

  8. PHP图片裁剪函数(图像不变形)

    PHP图片裁剪函数(图像不变形) <? *exif_imagetype -- 判断一个图像的类型 *说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形 * 参数说明:输入 需要处理图片的 ...

  9. c#读写共享内存操作函数封装

    原文 c#读写共享内存操作函数封装 c#共享内存操作相对c++共享内存操作来说原理是一样,但是c#会显得有点复杂. 现把昨天封装的读写共享内存封装的函数记录下来,一方面希望给需要这块的有点帮助,另一方 ...

随机推荐

  1. 2018.07.17 HAOI2016 找相同字符(SAM)

    传送门 就是给两个字符串,让你求公共字串的个数. 本来大佬们都是用的广义后缀自动机,但我感觉后缀自动机已经可以做这道题了.我们对其中一个字串建出后缀自动机,然后用另外一个后缀自动机在上面统计贡献即可. ...

  2. 第1章 (名词)Le nom

    ★名词的种类:(1)普通名词 —专有名词,如:          un livre —la Chine(2)可数名词—不可数名词,如:          un ami —le lait(3)具体名词— ...

  3. SQL之mysql常用操作语句(入门级)

    1.进入数据库: mysql -u root -p mysql -h localhost -u root -p database_name 2.列出数据库 show databases; 3.选择数据 ...

  4. 山东省第七届ACM竞赛 C题 Proxy (Dijkstra算法,单源路径最短问题)

    题意:给定0-n+1个点,和m条边,让你找到一条从0到n+1的最短路,输出与0相连的结点... 析:很明显么,是Dijkstra算法,不过特殊的是要输出与0相连的边,所以我们倒着搜,也是从n+1找到0 ...

  5. authentication 和 authorization

    单词 词性 解释 authentication n. 认证 authentic adj. 真实的 authorization n. 授权 authorise vt. 授权 authentication ...

  6. codevs 1012

    题目描述 Description 给出n和n个整数,希望你从小到大给他们排序 输入描述 Input Description 第一行一个正整数n 第二行n个用空格隔开的整数 输出描述 Output De ...

  7. 安卓添加USB外置UVC摄像头

    实现的方法有很多种,按步骤来看适合哪一种,网上说什么接采集卡,其实就是把AV转成UVC,现在市面上很多摄像头直接就已经是UVC的了,在windows上面即插即用. 安卓也是Linux,这个就好办了. ...

  8. mac与win7(台式电脑)共享文件

    人生处处又都坑,自己走过了,所以记下来. mac共享文件,win7访问: 1.系统偏好设置-共享-都选中就行.一般都会这样说. 2.系统偏好设置-用户与群组-解锁-客人用户-允许客人用户连接到共享文件 ...

  9. Beyond Compare脚本:命令行批量比较文件并生成html格式的差异报告

    BComp.exe /silent /closescript /solo @E:\compareTest\BCbatch.txt text-report layout:side-by-side opt ...

  10. Spring框架事务支持模型的优势

    全局事务 全局事务支持对多个事务性资源的操作,通常是关系型数据库和消息队列.应用服务器通过JTA管理全局性事务,API非常烦琐.UserTransaction通常需要从JNDI获取,意味着需要与JND ...