国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php圖像處理類實例

2024-05-04 23:38:11
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了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. * @access private 
  67. * @return void 
  68. */ 
  69. private function getSrcImageInfo() { 
  70. $info = $this->getImageInfo(); 
  71. $this->imageWidth = $info[0]; 
  72. $this->imageHeight = $info[1]; 
  73. $this->imageType = $info[2]; 
  74. $this->width = $info[0]; 
  75. $this->height = $info[1]; 
  76. $this->type = $info[2]; 
  77. /** 
  78. * 取圖像信息 
  79. * @param string $fileName 文件名 
  80. * @access private 
  81. * @return array 
  82. */ 
  83. private function getImageInfo($fileName = NULL) { 
  84. if ($fileName==NULL) { 
  85. $fileName = $this->fileName; 
  86. $info = getimagesize($fileName); 
  87. return $info; 
  88. /** 
  89. * 創建源圖像GD 資源 
  90. * @access private 
  91. * @return void 
  92. */ 
  93. private function createSrcImage () { 
  94. $this->imageResource = $this->createImageFromFile(); 
  95. /** 
  96. * 跟據文件創建圖像GD 資源 
  97. * @param string $fileName 文件名 
  98. * @return gd resource 
  99. */ 
  100. public function createImageFromFile($fileName = NULL) 
  101. if (!$fileName) { 
  102. $fileName = $this->fileName; 
  103. $imgType = $this->imageType; 
  104. if (!is_readable($fileName) || !file_exists($fileName)) { 
  105. throw new Exception('Unable to open file "' . $fileName . '"'); 
  106. if (!$imgType) { 
  107. $imageInfo = $this->getImageInfo($fileName); 
  108. $imgType = $imageInfo[2]; 
  109. switch ($imgType) { 
  110. case IMAGETYPE_GIF: 
  111. $tempResource = imagecreatefromgif($fileName); 
  112. break
  113. case IMAGETYPE_JPEG: 
  114. $tempResource = imagecreatefromjpeg($fileName); 
  115. break
  116. case IMAGETYPE_PNG: 
  117. $tempResource = imagecreatefrompng($fileName); 
  118. break
  119. case IMAGETYPE_WBMP: 
  120. $tempResource = imagecreatefromwbmp($fileName); 
  121. break
  122. case IMAGETYPE_XBM: 
  123. $tempResource = imagecreatefromxbm($fileName); 
  124. break
  125. default
  126. throw new Exception('Unsupport image type'); 
  127. return $tempResource; 
  128. /** 
  129. * 改變圖像大小 
  130. * @param int $width 寬 
  131. * @param int $height 高 
  132. * @param string $flag 一般而言,允許截圖則用4,不允許截圖則用1; 假設要求一個為4:3比例的圖像,則:4=如果太長則自動刪除一部分 0=長寬轉換成參數指定的 1=按比例縮放,自動判斷太長還是太寬,長寬約束在參數指定內 2=以寬為約束縮放 3=以高為約束縮放 
  133. * @param string $bgcolor 如果不為null,則用這個參數指定的顏色作為背景色,并且圖像擴充到指定高寬,該參數應該是一個數組; 
  134. * @return string 
  135. */ 
  136. public function resizeImage($width, $height, $flag=1, $bgcolor=null) { 
  137. $widthRatio = $width/$this->imageWidth; 
  138. $heightRatio = $height/$this->imageHeight; 
  139. switch ($flag) { 
  140. case 1: 
  141. if ($this->imageHeight < $height && $this->imageWidth < $width) { 
  142. $endWidth = $this->imageWidth; 
  143. $endHeight = $this->imageHeight; 
  144. //return; 
  145. } elseif (($this->imageHeight * $widthRatio)>$height) { 
  146. $endWidth = ceil($this->imageWidth * $heightRatio); 
  147. $endHeight = $height; 
  148. else { 
  149. $endWidth = $width; 
  150. $endHeight = ceil($this->imageHeight * $widthRatio); 
  151. break
  152. case 2: 
  153. $endWidth = $width; 
  154. $endHeight = ceil($this->imageHeight * $widthRatio); 
  155. break
  156. case 3: 
  157. $endWidth = ceil($this->imageWidth * $heightRatio); 
  158. $endHeight = $height; 
  159. break
  160. case 4: 
  161. $endWidth2 = $width; 
  162. $endHeight2 = $height; 
  163. if ($this->imageHeight < $height && $this->imageWidth < $width) { 
  164. $endWidth = $this->imageWidth; 
  165. $endHeight = $this->imageHeight; 
  166. //return; 
  167. } elseif (($this->imageHeight * $widthRatio)<$height) { 
  168. $endWidth = ceil($this->imageWidth * $heightRatio); 
  169. $endHeight = $height; 
  170. else { 
  171. $endWidth = $width; 
  172. $endHeight = ceil($this->imageHeight * $widthRatio); 
  173. break
  174. default
  175. $endWidth = $width; 
  176. $endHeight = $height; 
  177. break
  178. if ($this->imageResource==NULL) { 
  179. $this->createSrcImage(); 
  180. if($bgcolor){ 
  181. $this->newResource = imagecreatetruecolor($width,$height); 
  182. $bg=ImageColorAllocate($this->newResource,$bgcolor[0],$bgcolor[1],$bgcolor[2]); 
  183. ImageFilledRectangle($this->newResource,0,0,$width,$height,$bg); 
  184. $tox=ceil(($width-$endWidth)/2); 
  185. $toy=ceil(($height-$endHeight)/2); 
  186. if($tox<0) $tox=0; 
  187. if($toy<0) $toy=0; 
  188. }else if ($flag==4) { 
  189. $this->newResource = imagecreatetruecolor($endWidth2,$endHeight2); 
  190. }else { 
  191. $this->newResource = imagecreatetruecolor($endWidth,$endHeight); 
  192. $this->newResType = $this->imageType; 
  193. imagecopyresampled($this->newResource, $this->imageResource, $tox, $toy, 0, 0, $endWidth, $endHeight,$this->imageWidth,$this->imageHeight); 
  194. /** 
  195. * 給圖像加水印 
  196. * @param string $waterContent 水印內容可以是圖像文件名,也可以是文字 
  197. * @param int $pos 位置0-9可以是數組 
  198. * @param int $textFont 字體大字,當水印內容是文字時有效 
  199. * @param string $textColor 文字顏色,當水印內容是文字時有效 
  200. * @return string 
  201. */ 
  202. public function waterMark($waterContent, $pos = 0, $textFont=5, $textColor="#ffffff") { 
  203. $isWaterImage = file_exists($waterContent); 
  204. if ($isWaterImage) { 
  205. $waterImgRes = $this->createImageFromFile($waterContent); 
  206. $waterImgInfo = $this->getImageInfo($waterContent); 
  207. $waterWidth = $waterImgInfo[0]; 
  208. $waterHeight = $waterImgInfo[1]; 
  209. else { 
  210. $waterText = $waterContent; 
  211. //$temp = @imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterContent); 
  212. if ($temp) { 
  213. $waterWidth = $temp[2]-$temp[6]; 
  214. $waterHeight = $temp[3]-$temp[7]; 
  215. else { 
  216. $waterWidth = 100; 
  217. $waterHeight = 12; 
  218. if ($this->imageResource==NULL) { 
  219. $this->createSrcImage(); 
  220. switch($pos)  
  221. {  
  222. case 0://隨機  
  223. $posX = rand(0,($this->imageWidth - $waterWidth));  
  224. $posY = rand(0,($this->imageHeight - $waterHeight));  
  225. break;  
  226. case 1://1為頂端居左  
  227. $posX = 0;  
  228. $posY = 0;  
  229. break;  
  230. case 2://2為頂端居中  
  231. $posX = ($this->imageWidth - $waterWidth) / 2;  
  232. $posY = 0;  
  233. break;  
  234. case 3://3為頂端居右  
  235. $posX = $this->imageWidth - $waterWidth;  
  236. $posY = 0;  
  237. break;  
  238. case 4://4為中部居左  
  239. $posX = 0;  
  240. $posY = ($this->imageHeight - $waterHeight) / 2;  
  241. break;  
  242. case 5://5為中部居中  
  243. $posX = ($this->imageWidth - $waterWidth) / 2;  
  244. $posY = ($this->imageHeight - $waterHeight) / 2;  
  245. break;  
  246. case 6://6為中部居右  
  247. $posX = $this->imageWidth - $waterWidth;  
  248. $posY = ($this->imageHeight - $waterHeight) / 2;  
  249. break;  
  250. case 7://7為底端居左  
  251. $posX = 0;  
  252. $posY = $this->imageHeight - $waterHeight;  
  253. break;  
  254. case 8://8為底端居中  
  255. $posX = ($this->imageWidth - $waterWidth) / 2;  
  256. $posY = $this->imageHeight - $waterHeight;  
  257. break;  
  258. case 9://9為底端居右  
  259. $posX = $this->imageWidth - $waterWidth-20;  
  260. $posY = $this->imageHeight - $waterHeight-10;  
  261. break;  
  262. default://隨機  
  263. $posX = rand(0,($this->imageWidth - $waterWidth));  
  264. $posY = rand(0,($this->imageHeight - $waterHeight));  
  265. break;  
  266. imagealphablending($this->imageResource, true);  
  267. if($isWaterImage) { 
  268. imagecopy($this->imageResource, $waterImgRes, $posX, $posY, 0, 0, $waterWidth,$waterHeight);  
  269. else {  
  270. $R = hexdec(substr($textColor,1,2));  
  271. $G = hexdec(substr($textColor,3,2));  
  272. $B = hexdec(substr($textColor,5));  
  273. $textColor = imagecolorallocate($this->imageResource, $R, $G, $B); 
  274. imagestring ($this->imageResource, $textFont, $posX, $posY, $waterText, $textColor);  
  275. $this->newResource = $this->imageResource; 
  276. $this->newResType = $this->imageType; 
  277. /** 
  278. * 生成驗證碼圖片 
  279. * @param int $width 寬 
  280. * @param string $height 高 
  281. * @param int $length 長度 
  282. * @param int $validType 0=數字,1=字母,2=數字加字母 
  283. * @param string $textColor 文字顏色 
  284. * @param string $backgroundColor 背景顏色 
  285. * @return void 
  286. */ 
  287. public function imageValidate($width, $height, $length = 4, $validType = 1, $textColor = '#000000', $backgroundColor = '#ffffff') { 
  288. if ($validType==1) { 
  289. $validString = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  290. $validLength = 52; 
  291. } elseif ($validType==2) { 
  292. $validString = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  293. $validLength = 62; 
  294. else { 
  295. $validString = '123456789'
  296. $validLength = 9; 
  297. srand((int)time()); 
  298. $valid = ''
  299. for ($i=0; $i<$length; $i++) { 
  300. $valid .= $validString{rand(0, $validLength-1)}; 
  301. $this->newResource = imagecreate($width,$height); 
  302. $bgR = hexdec(substr($backgroundColor,1,2)); 
  303. $bgG = hexdec(substr($backgroundColor,3,2)); 
  304. $bgB = hexdec(substr($backgroundColor,5,2)); 
  305. $backgroundColor = imagecolorallocate($this->newResource, $bgR, $bgG, $bgB); 
  306. $tR = hexdec(substr($textColor,1,2)); 
  307. $tG = hexdec(substr($textColor,3,2)); 
  308. $tB = hexdec(substr($textColor,5,2)); 
  309. $textColor = imagecolorallocate($this->newResource, $tR, $tG, $tB); 
  310. for ($i=0;$i<strlen($valid);$i++){  
  311. imagestring($this->newResource,5,$i*$width/$length+3,2, $valid[$i],$textColor);  
  312. $this->newResType = IMAGETYPE_JPEG; 
  313. return $valid; 
  314. /** 
  315. * 顯示輸出圖像 
  316. * @return void 
  317. */ 
  318. public function display($fileName='', $quality=100) { 
  319. $imgType = $this->newResType; 
  320. $imageSrc = $this->newResource; 
  321. switch ($imgType) { 
  322. case IMAGETYPE_GIF: 
  323. if ($fileName=='') { 
  324. header('Content-type: image/gif'); 
  325. imagegif($imageSrc, $fileName, $quality); 
  326. break
  327. case IMAGETYPE_JPEG: 
  328. if ($fileName=='') { 
  329. header('Content-type: image/jpeg'); 
  330. imagejpeg($imageSrc, $fileName, $quality); 
  331. break
  332. case IMAGETYPE_PNG: 
  333. if ($fileName=='') { 
  334. header('Content-type: image/png'); 
  335. imagepng($imageSrc); 
  336. else { 
  337. imagepng($imageSrc, $fileName); 
  338. break
  339. case IMAGETYPE_WBMP: 
  340. if ($fileName=='') { 
  341. header('Content-type: image/wbmp'); 
  342. imagewbmp($imageSrc, $fileName, $quality); 
  343. break
  344. case IMAGETYPE_XBM: 
  345. if ($fileName=='') { 
  346. header('Content-type: image/xbm'); 
  347. imagexbm($imageSrc, $fileName, $quality); 
  348. break
  349. default
  350. throw new Exception('Unsupport image type'); 
  351. imagedestroy($imageSrc); 
  352. /** 
  353. * 保存圖像 
  354. * @param int $fileNameType 文件名類型 0使用原文件名,1使用指定的文件名,2在原文件名加上后綴,3產生隨機文件名 
  355. * @param string $folder 文件夾路徑 為空為與原文件相同 
  356. * @param string $param 參數$fileNameType為1時為文件名2時為后綴 
  357. * @return void 
  358. */ 
  359. public function save($fileNameType = 0, $folder = NULL, $param = '_miniature') { 
  360. if ($folder==NULL) { 
  361. $folder = dirname($this->fileName).DIRECTORY_SEPARATOR; 
  362. $fileExtName = FileSystem::fileExt($this->fileName, true); 
  363. $fileBesicName = FileSystem::getBasicName($this->fileName, false); 
  364. switch ($fileNameType) { 
  365. case 1: 
  366. $newFileName = $folder.$param; 
  367. break
  368. case 2: 
  369. $newFileName = $folder.$fileBesicName.$param.$fileExtName; 
  370. break
  371. case 3: 
  372. $tmp = date('YmdHis'); 
  373. $fileBesicName = $tmp; 
  374. $i = 0; 
  375. while (file_exists($folder.$fileBesicName.$fileExtName)) { 
  376. $fileBesicName = $tmp.$i; 
  377. $i++; 
  378. $newFileName = $folder.$fileBesicName.$fileExtName; 
  379. break
  380. default
  381. $newFileName = $this->fileName; 
  382. break
  383. $this->display($newFileName); 
  384. return $newFileName; 
  385. ?> 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 犍为县| 金昌市| 南丹县| 栾城县| 微山县| 延津县| 确山县| 潜山县| 和硕县| 象州县| 长垣县| 泾源县| 双城市| 平塘县| 陵川县| 横峰县| 改则县| 乌什县| 乌苏市| 聂拉木县| 长春市| 英山县| 怀远县| 和平区| 隆林| 海兴县| 根河市| 读书| 南丹县| 永吉县| 赫章县| 南平市| 大荔县| 鄂尔多斯市| 苏尼特左旗| 桂林市| 女性| 资兴市| 浮梁县| 宣城市| 扎赉特旗|