php图像处理类实例

这篇文章主要介绍了php图像处理类,涉及php操作图片的大小修改、加水印、生成验证码、输出及保存图像的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了php图像处理类,分享给大家供大家参考,具体如下:

  1. <?php
  2. /**
  3. * Image 类
  4. */
  5. class Image {
  6. /**
  7. * @var string $fileName 文件名
  8. * @access private
  9. */
  10. private $fileName = '';
  11. /**
  12. * @var gd resource $imageResource 原图像
  13. * @access private
  14. */
  15. private $imageResource = NULL;
  16. /**
  17. * @var int $imageWidth 原图像宽
  18. * @access private
  19. */
  20. private $imageWidth = NULL;
  21. /**
  22. * @var int $imageHeight 原图像高
  23. * @access private
  24. */
  25. private $imageHeight = NULL;
  26. /**
  27. * @var int $imageType 原图像类型
  28. * @access private
  29. */
  30. private $imageType = NULL;
  31. /**
  32. * @var int $imageWidth 原图像宽
  33. * @access private
  34. */
  35. public $width = NULL;
  36. /**
  37. * @var int $imageHeight 原图像高
  38. * @access private
  39. */
  40. public $height = NULL;
  41. /**
  42. * @var int $imageType 原图像类型
  43. * @access private
  44. */
  45. public $type = NULL;
  46. /**
  47. * @var int $newResource 新图像
  48. * @access private
  49. */
  50. private $newResource = NULL;
  51. /**
  52. * @var int $newResType 新图像类型
  53. * @access private
  54. */
  55. private $newResType = NULL;
  56. /**
  57. * 构造函数
  58. * @param string $fileName 文件名
  59. */
  60. public function __construct($fileName = NULL) {
  61. $this->fileName = $fileName;
  62. if ($this->fileName) {
  63. $this->getSrcImageInfo();
  64. }
  65. }
  66. /**
  67. * 取源图像信息
  68. * @access private
  69. * @return void
  70. */
  71. private function getSrcImageInfo() {
  72. $info = $this->getImageInfo();
  73. $this->imageWidth = $info[0];
  74. $this->imageHeight = $info[1];
  75. $this->imageType = $info[2];
  76. $this->width = $info[0];
  77. $this->height = $info[1];
  78. $this->type = $info[2];
  79. }
  80. /**
  81. * 取图像信息
  82. * @param string $fileName 文件名
  83. * @access private
  84. * @return array
  85. */
  86. private function getImageInfo($fileName = NULL) {
  87. if ($fileName==NULL) {
  88. $fileName = $this->fileName;
  89. }
  90. $info = getimagesize($fileName);
  91. return $info;
  92. }
  93. /**
  94. * 创建源图像GD 资源
  95. * @access private
  96. * @return void
  97. */
  98. private function createSrcImage () {
  99. $this->imageResource = $this->createImageFromFile();
  100. }
  101. /**
  102. * 跟据文件创建图像GD 资源
  103. * @param string $fileName 文件名
  104. * @return gd resource
  105. */
  106. public function createImageFromFile($fileName = NULL)
  107. {
  108. if (!$fileName) {
  109. $fileName = $this->fileName;
  110. $imgType = $this->imageType;
  111. }
  112. if (!is_readable($fileName) || !file_exists($fileName)) {
  113. throw new Exception('Unable to open file "' . $fileName . '"');
  114. }
  115. if (!$imgType) {
  116. $imageInfo = $this->getImageInfo($fileName);
  117. $imgType = $imageInfo[2];
  118. }
  119. switch ($imgType) {
  120. case IMAGETYPE_GIF:
  121. $tempResource = imagecreatefromgif($fileName);
  122. break;
  123. case IMAGETYPE_JPEG:
  124. $tempResource = imagecreatefromjpeg($fileName);
  125. break;
  126. case IMAGETYPE_PNG:
  127. $tempResource = imagecreatefrompng($fileName);
  128. break;
  129. case IMAGETYPE_WBMP:
  130. $tempResource = imagecreatefromwbmp($fileName);
  131. break;
  132. case IMAGETYPE_XBM:
  133. $tempResource = imagecreatefromxbm($fileName);
  134. break;
  135. default:
  136. throw new Exception('Unsupport image type');
  137. }
  138. return $tempResource;
  139. }
  140. /**
  141. * 改变图像大小
  142. * @param int $width 宽
  143. * @param int $height 高
  144. * @param string $flag 一般而言,允许截图则用4,不允许截图则用1; 假设要求一个为4:3比例的图像,则:4=如果太长则自动刪除一部分 0=长宽转换成参数指定的 1=按比例缩放,自动判断太长还是太宽,长宽约束在参数指定内 2=以宽为约束缩放 3=以高为约束缩放
  145. * @param string $bgcolor 如果不为null,则用这个参数指定的颜色作为背景色,并且图像扩充到指定高宽,该参数应该是一个数组;
  146. * @return string
  147. */
  148. public function resizeImage($width, $height, $flag=1, $bgcolor=null) {
  149. $widthRatio = $width/$this->imageWidth;
  150. $heightRatio = $height/$this->imageHeight;
  151. switch ($flag) {
  152. case 1:
  153. if ($this->imageHeight < $height && $this->imageWidth < $width) {
  154. $endWidth = $this->imageWidth;
  155. $endHeight = $this->imageHeight;
  156. //return;
  157. } elseif (($this->imageHeight * $widthRatio)>$height) {
  158. $endWidth = ceil($this->imageWidth * $heightRatio);
  159. $endHeight = $height;
  160. } else {
  161. $endWidth = $width;
  162. $endHeight = ceil($this->imageHeight * $widthRatio);
  163. }
  164. break;
  165. case 2:
  166. $endWidth = $width;
  167. $endHeight = ceil($this->imageHeight * $widthRatio);
  168. break;
  169. case 3:
  170. $endWidth = ceil($this->imageWidth * $heightRatio);
  171. $endHeight = $height;
  172. break;
  173. case 4:
  174. $endWidth2 = $width;
  175. $endHeight2 = $height;
  176. if ($this->imageHeight < $height && $this->imageWidth < $width) {
  177. $endWidth = $this->imageWidth;
  178. $endHeight = $this->imageHeight;
  179. //return;
  180. } elseif (($this->imageHeight * $widthRatio)<$height) {
  181. $endWidth = ceil($this->imageWidth * $heightRatio);
  182. $endHeight = $height;
  183. } else {
  184. $endWidth = $width;
  185. $endHeight = ceil($this->imageHeight * $widthRatio);
  186. }
  187. break;
  188. default:
  189. $endWidth = $width;
  190. $endHeight = $height;
  191. break;
  192. }
  193. if ($this->imageResource==NULL) {
  194. $this->createSrcImage();
  195. }
  196. if($bgcolor){
  197. $this->newResource = imagecreatetruecolor($width,$height);
  198. $bg=ImageColorAllocate($this->newResource,$bgcolor[0],$bgcolor[1],$bgcolor[2]);
  199. ImageFilledRectangle($this->newResource,0,0,$width,$height,$bg);
  200. $tox=ceil(($width-$endWidth)/2);
  201. $toy=ceil(($height-$endHeight)/2);
  202. if($tox<0) $tox=0;
  203. if($toy<0) $toy=0;
  204. }else if ($flag==4) {
  205. $this->newResource = imagecreatetruecolor($endWidth2,$endHeight2);
  206. }else {
  207. $this->newResource = imagecreatetruecolor($endWidth,$endHeight);
  208. }
  209. $this->newResType = $this->imageType;
  210. imagecopyresampled($this->newResource, $this->imageResource, $tox, $toy, 0, 0, $endWidth, $endHeight,$this->imageWidth,$this->imageHeight);
  211. }
  212. /**
  213. * 给图像加水印
  214. * @param string $waterContent 水印内容可以是图像文件名,也可以是文字
  215. * @param int $pos 位置0-9可以是数组
  216. * @param int $textFont 字体大字,当水印内容是文字时有效
  217. * @param string $textColor 文字颜色,当水印内容是文字时有效
  218. * @return string
  219. */
  220. public function waterMark($waterContent, $pos = 0, $textFont=5, $textColor="#ffffff") {
  221. $isWaterImage = file_exists($waterContent);
  222. if ($isWaterImage) {
  223. $waterImgRes = $this->createImageFromFile($waterContent);
  224. $waterImgInfo = $this->getImageInfo($waterContent);
  225. $waterWidth = $waterImgInfo[0];
  226. $waterHeight = $waterImgInfo[1];
  227. } else {
  228. $waterText = $waterContent;
  229. //$temp = @imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterContent);
  230. if ($temp) {
  231. $waterWidth = $temp[2]-$temp[6];
  232. $waterHeight = $temp[3]-$temp[7];
  233. } else {
  234. $waterWidth = 100;
  235. $waterHeight = 12;
  236. }
  237. }
  238. if ($this->imageResource==NULL) {
  239. $this->createSrcImage();
  240. }
  241. switch($pos)
  242. {
  243. case 0://随机
  244. $posX = rand(0,($this->imageWidth - $waterWidth));
  245. $posY = rand(0,($this->imageHeight - $waterHeight));
  246. break;
  247. case 1://1为顶端居左
  248. $posX = 0;
  249. $posY = 0;
  250. break;
  251. case 2://2为顶端居中
  252. $posX = ($this->imageWidth - $waterWidth) / 2;
  253. $posY = 0;
  254. break;
  255. case 3://3为顶端居右
  256. $posX = $this->imageWidth - $waterWidth;
  257. $posY = 0;
  258. break;
  259. case 4://4为中部居左
  260. $posX = 0;
  261. $posY = ($this->imageHeight - $waterHeight) / 2;
  262. break;
  263. case 5://5为中部居中
  264. $posX = ($this->imageWidth - $waterWidth) / 2;
  265. $posY = ($this->imageHeight - $waterHeight) / 2;
  266. break;
  267. case 6://6为中部居右
  268. $posX = $this->imageWidth - $waterWidth;
  269. $posY = ($this->imageHeight - $waterHeight) / 2;
  270. break;
  271. case 7://7为底端居左
  272. $posX = 0;
  273. $posY = $this->imageHeight - $waterHeight;
  274. break;
  275. case 8://8为底端居中
  276. $posX = ($this->imageWidth - $waterWidth) / 2;
  277. $posY = $this->imageHeight - $waterHeight;
  278. break;
  279. case 9://9为底端居右
  280. $posX = $this->imageWidth - $waterWidth-20;
  281. $posY = $this->imageHeight - $waterHeight-10;
  282. break;
  283. default://随机
  284. $posX = rand(0,($this->imageWidth - $waterWidth));
  285. $posY = rand(0,($this->imageHeight - $waterHeight));
  286. break;
  287. }
  288. imagealphablending($this->imageResource, true);
  289. if($isWaterImage) {
  290. imagecopy($this->imageResource, $waterImgRes, $posX, $posY, 0, 0, $waterWidth,$waterHeight);
  291. } else {
  292. $R = hexdec(substr($textColor,1,2));
  293. $G = hexdec(substr($textColor,3,2));
  294. $B = hexdec(substr($textColor,5));
  295. $textColor = imagecolorallocate($this->imageResource, $R, $G, $B);
  296. imagestring ($this->imageResource, $textFont, $posX, $posY, $waterText, $textColor);
  297. }
  298. $this->newResource = $this->imageResource;
  299. $this->newResType = $this->imageType;
  300. }
  301. /**
  302. * 生成验证码图片
  303. * @param int $width 宽
  304. * @param string $height 高
  305. * @param int $length 长度
  306. * @param int $validType 0=数字,1=字母,2=数字加字母
  307. * @param string $textColor 文字颜色
  308. * @param string $backgroundColor 背景颜色
  309. * @return void
  310. */
  311. public function imageValidate($width, $height, $length = 4, $validType = 1, $textColor = '#000000', $backgroundColor = '#ffffff') {
  312. if ($validType==1) {
  313. $validString = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  314. $validLength = 52;
  315. } elseif ($validType==2) {
  316. $validString = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  317. $validLength = 62;
  318. } else {
  319. $validString = '123456789';
  320. $validLength = 9;
  321. }
  322. srand((int)time());
  323. $valid = '';
  324. for ($i=0; $i<$length; $i++) {
  325. $valid .= $validString{rand(0, $validLength-1)};
  326. }
  327. $this->newResource = imagecreate($width,$height);
  328. $bgR = hexdec(substr($backgroundColor,1,2));
  329. $bgG = hexdec(substr($backgroundColor,3,2));
  330. $bgB = hexdec(substr($backgroundColor,5,2));
  331. $backgroundColor = imagecolorallocate($this->newResource, $bgR, $bgG, $bgB);
  332. $tR = hexdec(substr($textColor,1,2));
  333. $tG = hexdec(substr($textColor,3,2));
  334. $tB = hexdec(substr($textColor,5,2));
  335. $textColor = imagecolorallocate($this->newResource, $tR, $tG, $tB);
  336. for ($i=0;$i<strlen($valid);$i++){
  337. imagestring($this->newResource,5,$i*$width/$length+3,2, $valid[$i],$textColor);
  338. }
  339. $this->newResType = IMAGETYPE_JPEG;
  340. return $valid;
  341. }
  342. /**
  343. * 显示输出图像
  344. * @return void
  345. */
  346. public function display($fileName='', $quality=100) {
  347. $imgType = $this->newResType;
  348. $imageSrc = $this->newResource;
  349. switch ($imgType) {
  350. case IMAGETYPE_GIF:
  351. if ($fileName=='') {
  352. header('Content-type: image/gif');
  353. }
  354. imagegif($imageSrc, $fileName, $quality);
  355. break;
  356. case IMAGETYPE_JPEG:
  357. if ($fileName=='') {
  358. header('Content-type: image/jpeg');
  359. }
  360. imagejpeg($imageSrc, $fileName, $quality);
  361. break;
  362. case IMAGETYPE_PNG:
  363. if ($fileName=='') {
  364. header('Content-type: image/png');
  365. imagepng($imageSrc);
  366. } else {
  367. imagepng($imageSrc, $fileName);
  368. }
  369. break;
  370. case IMAGETYPE_WBMP:
  371. if ($fileName=='') {
  372. header('Content-type: image/wbmp');
  373. }
  374. imagewbmp($imageSrc, $fileName, $quality);
  375. break;
  376. case IMAGETYPE_XBM:
  377. if ($fileName=='') {
  378. header('Content-type: image/xbm');
  379. }
  380. imagexbm($imageSrc, $fileName, $quality);
  381. break;
  382. default:
  383. throw new Exception('Unsupport image type');
  384. }
  385. imagedestroy($imageSrc);
  386. }
  387. /**
  388. * 保存图像
  389. * @param int $fileNameType 文件名类型 0使用原文件名,1使用指定的文件名,2在原文件名加上后缀,3产生随机文件名
  390. * @param string $folder 文件夹路径 为空为与原文件相同
  391. * @param string $param 参数$fileNameType为1时为文件名2时为后缀
  392. * @return void
  393. */
  394. public function save($fileNameType = 0, $folder = NULL, $param = '_miniature') {
  395. if ($folder==NULL) {
  396. $folder = dirname($this->fileName).DIRECTORY_SEPARATOR;
  397. }
  398. $fileExtName = FileSystem::fileExt($this->fileName, true);
  399. $fileBesicName = FileSystem::getBasicName($this->fileName, false);
  400. switch ($fileNameType) {
  401. case 1:
  402. $newFileName = $folder.$param;
  403. break;
  404. case 2:
  405. $newFileName = $folder.$fileBesicName.$param.$fileExtName;
  406. break;
  407. case 3:
  408. $tmp = date('YmdHis');
  409. $fileBesicName = $tmp;
  410. $i = 0;
  411. while (file_exists($folder.$fileBesicName.$fileExtName)) {
  412. $fileBesicName = $tmp.$i;
  413. $i++;
  414. }
  415. $newFileName = $folder.$fileBesicName.$fileExtName;
  416. break;
  417. default:
  418. $newFileName = $this->fileName;
  419. break;
  420. }
  421. $this->display($newFileName);
  422. return $newFileName;
  423. }
  424. }
  425. ?>

希望本文所述对大家的php程序设计有所帮助。