找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1907|回复: 0

[教程] 图片加水印、等比例缩放 类

[复制链接]
发表于 2013-4-13 21:04:40 | 显示全部楼层 |阅读模式 来自 广东省湛江市
  1. <p><?php
  2. </p><p>class image{
  3. </p><p>private $res; //图像资源
  4. </p><p>private $srcpath;
  5. private $srcwidth;

  6. private $srchight;

  7. private $srctype;
  8. </p><p>private $logpath;

  9. private $logwidth;

  10. private $loghight;

  11. private $logtype;
  12. </p><p>//初始化得到图像信息

  13. public function __construct($srcpath,$logpath){

  14. //判断是否为文件

  15. if(!is_file($srcpath) || !is_file($logpath)){

  16. die('文件不存在');

  17. }
  18. </p><p>//获取图像地址

  19. $this->srcpath = $srcpath;

  20. $this->logpath = $logpath;
  21. </p><p>//取得目标图像大小,类型1 = GIF,2 = JPG,3 = PNG 6 = BMP

  22. list($sw,$sh,$stype) = getimagesize($srcpath);

  23. $this->srcwidth = $sw;

  24. $this->srchigth = $sh;

  25. $this->srctype = $stype;
  26. </p><p>//取得水印图像大小,类型

  27. list($lw,$lh,$ltype) = getimagesize($logpath);

  28. $this->logwidth = $lw;

  29. $this->loghight = $lh;

  30. $this->logtype = $ltype;
  31. </p><p>}
  32. </p><p>
  33. /************************************创建图像资源********************************/

  34. public function createimage(){
  35. </p><p>//图像地址关联图像类型

  36. $imgarr = array_combine( array($this->srcpath,$this->logpath),array($this->srctype,$this->logtype) );
  37. $res = array();
  38. </p><p>//创建对应类型的图像资源

  39. foreach($imgarr as $key=>$v){

  40. switch($v){

  41. case 1:

  42. $res[] = imagecreatefromgif($key);
  43. break;

  44. case 2:

  45. $res[] = imagecreatefromjpeg($key);

  46. break;

  47. case 3:

  48. $res[] = imagecreatefrompng($key);
  49. break;

  50. case 6:

  51. $res[] = imagecreatefromwbmp($key);
  52. break;

  53. default:

  54. return false;

  55. }

  56. }
  57. </p><p>//图像资源赋给属性

  58. $this->res = $res;
  59. </p><p>}
  60. </p><p>
  61. /************************************给图片加水印********************************/

  62. //$dx,$dy水印放置目标图片坐标

  63. //$sx,$sy水印开始部分坐标

  64. //$sw,$sh水印结束部分坐标

  65. //$pct水印透明度(100不透明)

  66. public function shuiyin($dx,$dy,$sx,$sy,$sw,$sh,$pct){

  67. $dstres = $this->res[0];

  68. $logres = $this->res[1]; </p><p>imagecopymergegray($dstres,$logres,$dx,$dy,$sx,$sy,$sw,$sh,$pct);

  69. return $dstres;

  70. }
  71. </p><p>
  72. /************************************等比例缩放图片********************************/

  73. //$res需缩小的图片资源

  74. //$width,$higth缩小尺寸
  75. </p><p>public function small($logres,$imw,$imh){

  76. $img = imagecreatetruecolor($imw,$imh); //创建画布

  77. $imgcolor = imagecolorallocate($img,100,100,100);

  78. imagefill($img,0,0,$imgcolor);
  79. </p><p>//判断图像来源,取得图像大小

  80. if(is_file($logres)){

  81. list($width,$hight,$type) = getimagesize($logres);
  82. switch($type){

  83. case 1:

  84. $src = imagecreatefromgif($logres);
  85. break;

  86. case 2:

  87. $src = imagecreatefromjpeg($logres);

  88. break;

  89. case 3:

  90. $src = imagecreatefrompng($logres);
  91. break;

  92. case 6:

  93. $src = imagecreatefromwbmp($logres);
  94. break;

  95. default:

  96. return false;

  97. }
  98. }

  99. else{

  100. $src = $logres;

  101. $width = imagesx($src);

  102. $hight = imagesy($src);

  103. }
  104. </p><p>//判断图像大小,进行缩放

  105. if($width==$imw && $hight==$imh){

  106. imagecopyresampled($img,$src,0,0,0,0,$imw,$imh,$width,$hight);

  107. }

  108. elseif($width>$hight){

  109. $bili = $imw/$width;

  110. $nw = $bili*$width;

  111. $nh = $imh*($hight/$width);

  112. imagecopyresampled($img,$src,0,($imh-$nh)/2,0,0,$nw,$nh,$width,$hight); //宽>高

  113. }else{

  114. $bili = $imh/$hight;

  115. $nw = $imw*($width/$hight);

  116. $nh = $bili*$hight;

  117. imagecopyresampled($img,$src,($imw-$nw)/2,0,0,0,$nw,$nh,$width,$hight); //高>宽

  118. }
  119. </p><p>//返回图像资源

  120. return $img;

  121. }
  122. </p><p>}
  123. </p><p> </p>
复制代码

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-6-6 20:58 , Processed in 0.029283 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表