PHP用imagerotate旋转图片和等比缩放压缩、添加水印

后端开发   发布日期:2023年05月19日   浏览次数:466

本文介绍PHP用imagerotate旋转图片和缩放压缩、添加水印方法。思路就是我要实现,先判断一下图片的宽和高的比例,如果是高大于宽,则逆时针旋转90度,同时再判断一下是否超过我的限制宽度或高度,如果超过就等比压缩,再添加水印。

下面是具体应用PHP代码:

  1. include_once("new_image.class.php");
  2. $article_suofang='1';
  3. $article_shuiyin='1';
  4. //超宽尺寸自动缩放 $var_article_suofang='1';//发布文章是否将内容中的图片自动缩放到指定宽度以内,1是,0否
  5. if ($article_suofang=='1'){
  6. @list($width,$height)=@getimagesize($filePath);
  7. //判断是竖版时才做横向调整
  8. if ($height>$width){
  9. $imageSize = getimagesize($filePath);
  10. $imageSize = explode('/', $imageSize['mime']);
  11. $type = $imageSize[1];
  12. switch ($type) {
  13. case "png":
  14. $image = imagecreatefrompng($filePath);
  15. break;
  16. case "jpeg":
  17. $image = imagecreatefromjpeg($filePath);
  18. break;
  19. case "jpg":
  20. $image = imagecreatefromjpeg($filePath);
  21. break;
  22. case "gif":
  23. $image = imagecreatefromgif($filePath);
  24. break;
  25. }
  26. $rotateImage = imagerotate($image, 90, 0); //逆时90针旋转
  27. //获取旋转后的宽高
  28. $width = imagesx($rotateImage);
  29. $height = imagesy($rotateImage);
  30. imagejpeg($rotateImage, $filePath);
  31. imagedestroy($rotateImage);
  32. }
  33. //图片存档结束,准备判断是否要压缩图片尺寸
  34. if ($width>1100 || $height>700){ //宽度超过1100或,高度超过700,才按指定的宽高比例缩放
  35. $image=new image($filePath,1, "1100", "700" ,$filePath);
  36. $image->outimage();
  37. }
  38. }
  39. //水印,图片处理完最后执行 $var_article_shuiyin='1';//发布文章是否将内容中的图片自动添加水印,1是,0否
  40. if ($article_shuiyin=='1'){
  41. @list($width,$height)=@getimagesize($filePath);
  42. if ($width>396 && $height>249){ //宽度超过396且,高度超过249,才添加水印,0为水印随机,大小判断与水印图片大小有关
  43. $image=new image($filePath, 3, "fapiao_img/shuiyin.png", "0",$filePath);
  44. $image->outimage();
  45. }
  46. }

new_image.class.php文件代码:

  1. <?php
  2. /*已知问题:1.在图片缩放功能中,使用imagecreatetruecolor函数创建画布,并使用透明处理算法,但PNG格式的图片无法透明。用imagecreate函数创建画布可以解决这个问题,但是缩放出来的图片色数太少了
  3. *
  4. *
  5. *type值:
  6. * (1):代表使用图片缩放功能,此时,$value1代表缩放后图片的宽度,$value2代表缩放后图片的高度
  7. * (2):代表使用图片裁剪功能,此时,$value1代表裁剪开始点的坐标,例:从原点开始即是“0,0”前面是x轴后面是y轴,中间用,分隔,$value2代表裁剪的宽度和高度,同样也是“20,20”的形式使用
  8. * (3):代表使用加图片水印功能,此时,$value1代表水印图片的文件名,$value2代表水印在图片中的位置,有10值个可以选,1代表左上,2代表左中,3代表左右,4代表中左,5代表中中,6代表中右,7代表下做,8代表下中,9代表下右,0代表随机位置
  9. $image=new image("1.jpg", 1, "300", "400", "11.jpg"); //使用图片缩放功能 原图,1缩放,最宽,最高,新图位置 (其中,最高或最宽任一超过即等比缩放)
  10. $image=new image("1.jpg", 2, "0,0", "200,200", "12.jpg"); //使用图片裁剪功能 原图,2裁剪,起始坐标XY(左上角开始),大小XY,新图位置
  11. $image=new image("1.jpg", 3, "12.jpg", "9", "13.jpg"); //使用加图片水印功能 原图,3水印,水印图片(水印大小取决此图大小),位置9右下0随机,新图位置
  12. $image->outimage();
  13. *
  14. */
  15. class image{
  16. private $types; //使用的功能编号,1为图片缩放功能 2为图片裁剪功能 3,为图片加图片水印功能
  17. private $imgtype;//图片的格式
  18. private $image; //图片资源
  19. private $width;//图片宽度
  20. private $height;//图片高度
  21. private $value1;//根据所传type值的不同,$value1分别代表不同的值
  22. private $value2;//根据所传type值的不同,$value2分别代表不同的值
  23. private $endaddress;//输出后的地址+文件名
  24. function __construct($imageaddress, $types, $value1="", $value2="", $endaddress){
  25. $this->types=$types;
  26. $this->image=$this->imagesources($imageaddress);//获取不到
  27. $this->width=$this->imagesizex();
  28. $this->height=$this->imagesizey();
  29. $this->value1=$value1;
  30. $this->value2=$value2;
  31. $this->endaddress=$endaddress;
  32. }
  33. function outimage(){ //根据传入type值的不同,输出不同的功能
  34. switch($this->types){
  35. case 1:
  36. $this->scaling();
  37. break;
  38. case 2:
  39. $this->clipping();
  40. break;
  41. case 3:
  42. $this->imagewater();
  43. break;
  44. default:
  45. return false;
  46. }
  47. }
  48. private function imagewater(){ //加图片水印功能
  49. //用函数获取水印文件的长和宽
  50. $imagearrs=$this->getimagearr($this->value1);
  51. //调用函数计算出水印加载的位置
  52. $positionarr=$this->position($this->value2, $imagearrs[0], $imagearrs[1]);
  53. //加水印
  54. imagecopy($this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]);
  55. //调用输出方法保存
  56. $this->output($this->image);
  57. }
  58. private function clipping(){ //图片裁剪功能
  59. //将传进来的值分别赋给变量
  60. list($src_x, $src_y)=explode(",", $this->value1);
  61. list($dst_w, $dst_h)=explode(",", $this->value2);
  62. if($this->width < $src_x+$dst_w || $this->height < $src_y+$dst_h){ //这个判断就是限制不能截取到图片外面去
  63. return false;
  64. }
  65. //创建新的画布资源
  66. $newimg=imagecreatetruecolor($dst_w, $dst_h);
  67. //进行裁剪
  68. imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h);
  69. //调用输出方法保存
  70. $this->output($newimg);
  71. }
  72. private function scaling(){ //图片缩放功能
  73. //获取等比缩放的宽和高
  74. $this-> proimagesize();
  75. //根据参数进行缩放,并调用输出函数保存处理后的文件
  76. $this->output($this->imagescaling());
  77. }
  78. private function imagesources($imgad){ //获取图片类型并打开图像资源
  79. $imagearray=$this->getimagearr($imgad);
  80. switch($imagearray[2]){
  81. case 1://gif
  82. $this->imgtype=1;
  83. $img=imagecreatefromgif($imgad);
  84. break;
  85. case 2://jpeg
  86. $this->imgtype=2;
  87. $img=imagecreatefromjpeg($imgad);
  88. break;
  89. case 3://png
  90. $this->imgtype=3;
  91. $img=imagecreatefrompng($imgad);
  92. break;
  93. default:
  94. return false;
  95. }
  96. return $img;
  97. }
  98. private function imagesizex(){ //获得图片宽度
  99. return imagesx($this->image);
  100. }
  101. private function imagesizey(){ //获取图片高度
  102. return imagesy($this->image);
  103. }
  104. private function proimagesize(){ //计算等比缩放的图片的宽和高
  105. if($this->value1 && ($this->width < $this->height)) { //等比缩放算法
  106. $this->value1=round(($this->value2/ $this->height)*$this->width);
  107. }else{
  108. $this->value2=round(($this->value1/ $this->width) * $this->height);
  109. }
  110. }
  111. private function imagescaling(){//图像缩放功能,返回处理后的图像资源
  112. $newimg=imagecreatetruecolor($this->value1, $this->value2);
  113. $tran=imagecolortransparent($this->image);//处理透明算法
  114. if($tran >= 0 && $tran < imagecolorstotal($this->image)){
  115. $tranarr=imagecolorsforindex($this->image, $tran);
  116. $newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']);
  117. imagefill($newimg, 0, 0, $newcolor);
  118. imagecolortransparent($newimg, $newcolor);
  119. }
  120. imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this->height);
  121. return $newimg;
  122. }
  123. private function output($image){//输出图像
  124. echo $this->imgtype;
  125. switch($this->imgtype){
  126. case 1:
  127. imagegif($image, $this->endaddress);//参数可能是两个 ($image, $this->endaddress,99)
  128. break;
  129. case 2:
  130. imagejpeg($image, $this->endaddress);
  131. break;
  132. case 3:
  133. imagepng($image, $this->endaddress);//$quality参数取值范围0-99 在php 5.1.2之后变更为0-9
  134. break;
  135. default:
  136. return false;
  137. }
  138. }
  139. private function getimagearr($imagesou){//返回图像属性数组方法
  140. return getimagesize($imagesou);
  141. }
  142. private function position($num, $width, $height){//根据传入的数字返回一个位置的坐标,$width和$height分别代表插入图像的宽和高
  143. switch($num){
  144. case 1:
  145. $positionarr[0]=0;
  146. $positionarr[1]=0;
  147. break;
  148. case 2:
  149. $positionarr[0]=($this->width-$width)/2;
  150. $positionarr[1]=0;
  151. break;
  152. case 3:
  153. $positionarr[0]=$this->width-$width;
  154. $positionarr[1]=0;
  155. break;
  156. case 4:
  157. $positionarr[0]=0;
  158. $positionarr[1]=($this->height-$height)/2;
  159. break;
  160. case 5:
  161. $positionarr[0]=($this->width-$width)/2;
  162. $positionarr[1]=($this->height-$height)/2;
  163. break;
  164. case 6:
  165. $positionarr[0]=$this->width-$width;
  166. $positionarr[1]=($this->height-$height)/2;
  167. break;
  168. case 7:
  169. $positionarr[0]=0;
  170. $positionarr[1]=$this->height-$height;
  171. break;
  172. case 8:
  173. $positionarr[0]=($this->width-$width)/2;
  174. $positionarr[1]=$this->height-$height;
  175. break;
  176. case 9:
  177. $positionarr[0]=$this->width-$width;
  178. $positionarr[1]=$this->height-$height;
  179. break;
  180. case 0:
  181. $positionarr[0]=rand(0, $this->width-$width);
  182. $positionarr[1]=rand(0, $this->height-$height);
  183. break;
  184. }
  185. return $positionarr;
  186. }
  187. function __destruct(){
  188. imagedestroy($this->image);
  189. }
  190. }
  191. ?>

$filePath就是图片的路径,我是做的压缩、旋转和添加水印都是原图覆盖。

以上就是PHP用imagerotate旋转图片和等比缩放压缩、添加水印的详细内容,更多关于PHP用imagerotate旋转图片和等比缩放压缩、添加水印的资料请关注九品源码其它相关文章!