国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 後端開發(fā) php教程 PHP將圖片進行浮水印添加以及產(chǎn)生縮率圖

PHP將圖片進行浮水印添加以及產(chǎn)生縮率圖

Jul 25, 2016 am 08:42 AM

1 將圖片進行浮水印添加
2產(chǎn)生一個新的縮率圖
  1. class Image{
  2. //水印配置項
  3. private $waterOn;
  4. private $waterImg ;
  5. private $waterPos;
  6. private $waterPct;
  7. private $waterText;
  8. private $waterFont;
  9. private $waterTextSize; private $private;
  10. //縮圖配置項目
  11. private $thumbWidth;
  12. private $thumbHeight;
  13. private $thumbType;
  14. private $ (){
  15. $this->waterOn=C("WATER_ON");
  16. $this->waterImg=C("WATER_IMG");
  17. $this->waterPos=C("WATER_POS") ;
  18. $this->waterPct=C("WATER_PCT");
  19. $this->waterText=C("WATER_TEXT");
  20. $this->waterFont=C("WATER_FONT");
  21. $this->waterTextSize=C("WATER_TEXT_SIZE");
  22. $this->waterTextColor=C("WATER_TEXT_COLOR");
  23. $this->qua=C("WATER_QUA");
  24. //縮率圖
  25. $this->thumbWidth=C("THUMB_WIDTH");
  26. $this->thumbHeight=C("THUMB_HEIGHT");
  27. $this->thumbType=C("THUMB_TYPE ");
  28. $this->thumbEndFix=C("THUMB_ENDFIX");
  29. }
  30. /*
  31. *驗證圖片是否合法
  32. */
  33. private function check($img ){
  34. return is_file($img)&&getimagesize($img)&&extension_loaded("gd");
  35. }
  36. /*
  37. *縮率圖
  38. *@param string $img 原圖
  39. *@param string $outFile 縮率之後儲存的圖片
  40. *@param int $thumbWidth 縮率圖寬度
  41. *@param int $thumbHeight 縮率圖高度
  42. *@param int $thumbType那種方式進行縮寫處理
  43. */
  44. public function thumb($img,$outFile="",$thumbWidth="",$thumbHeight="",$thumbType=""){
  45. if(!$this->check($img)){
  46. return false;
  47. }
  48. //縮率圖處理方式
  49. $thumbType=$thumbType?$thumbType:$this-> thumbType;
  50. //縮率圖寬度
  51. $thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;
  52. //縮率圖高度
  53. $thumbHeight=$thumbight; $this->thumbHeight;
  54. //取得原圖資訊
  55. $imgInfo=getimagesize($img);
  56. //原圖寬度
  57. $imgWidth=$imgInfo[0];
  58. //原圖高度
  59. $imgHeight=$imgInfo[1];
  60. //取得原圖型別
  61. $imgtype=image_type_to_extension($imgInfo[2]);
  62. //依不同的縮略處理方式,取得尺寸(原圖與縮圖對應的尺寸)
  63. $thumb_size=$this->thumbsize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
  64. //建立原圖
  65. $func="imagecreatefrom".substr($imgtype,1);//變數(shù)函數(shù)
  66. $resImg=$func($img);
  67. //建立縮率圖畫布
  68. if($imgtype==".gif"){
  69. $res_thumb=imagecreate($thumb_size[2],$thumb_size[3]);
  70. }else{
  71. $res_thumb=imagecreatetruecolorcolor($thumb_size" 2],$thumb_size[3]);
  72. }
  73. imagecopyresized($res_thumb,$resImg,0,0,0,0,$thumb_size[2],$thumb_size[3],$thumb_size[0] ,$thumb_size[1]);
  74. $fileInfo=pathinfo($img);//檔案資訊
  75. $outFile=$outFile?$outFile:$fileInfo['filename'].$this->thumbEndFix. $fileInfo['extension'];//檔案名稱
  76. $outFile=$fileInfo["dirname"]."/".$outFile;//加上目錄
  77. $func="image".substr( $imgtype,1);
  78. $func($res_thumb,$outFile);
  79. return $outFile;
  80. }
  81. private function thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
  82. //縮率圖尺寸
  83. $w=$thumbWidth; //原圖尺寸
  84. $img_w=$imgWidth;
  85. $img_h=$imgHeight;
  86. switch($thumbType){
  87. case 1:
  88. //寬度固定,高度自增
  89. $h=$w/$imgWidth*$imgHeight;
  90. break;
  91. case 2://高度固定,寬度自
  92. $w=$h/$imgHeight*$imgWidth;
  93. break;
  94. case 3:
  95. if($imgHeight/$thumbHeight>$imgWidth/$thumbWidth){
  96. $img_h=$imgWidth/$thumbWidth){
  97. $img_h=$imgWidth/$thumbWidth*aidight;
  98. $img_w=$imgHeight/$thumbHeight*$thumbWidth;
  99. }
  100. }
  101. return array($img_w,$img_h,$w,$h);
  102. }
  103. }
  104. / *
  105. *@param string $img 原圖
  106. *@param string $outImg 加完浮水印後產(chǎn)生的圖
  107. *@param int $pos 水印位置
  108. *@param int $pct 透明度
  109. *@param text $text 水印文字
  110. *@param string $waterImg浮水印圖片
  111. */
  112. public function water($img,$outImg=null,$pos="",$pct= "",$text="",$waterImg="",$textColor=""){
  113. if(!$this->check($img)){
  114. return false;
  115. }
  116. //加完浮水印後產(chǎn)生的圖
  117. $outImg=$outImg?$outImg:$img;
  118. //水印位置
  119. $pos=$pos?$pos:$this->waterPos ;
  120. //透明度
  121. $pct=$pct?$pct:$this->waterPct;
  122. //水印文字
  123. $text=$text?$text:$this->waterText;
  124. //浮水印圖片
  125. $waterImg=$waterImg?$waterImg:$this->waterImg;
  126. //驗證水印圖片
  127. $waterImgOn=$this->check($waterImg);
  128. //水印文字顏色
  129. $textColor=$textColor?$textColor:$this->waterTextColor;
  130. //原圖資訊
  131. $imgInfo=getimagesize($img);
  132. //原圖寬度
  133. $imgWidth=$imgInfo[0];
  134. //原圖高度
  135. $imgHeight=$imgInfo[1];
  136. switch($imgInfo[2]){
  137. case 1:
  138. $resImg=imagecreatefromgif($img);
  139. break;
  140. case 2:
  141. $resImg=imagecreatefromjpeg($img);
  142. break;
  143. case 3:34ase break; ??> $resImg=imagecreatefrompng($img);
  144. break;
  145. }
  146. if($waterImgOn){//水印圖片有效
  147. //水印資訊
  148. $waterInfo=getgetgesize($ waterImg);
  149. //浮水印寬度
  150. $waterWidth=$waterInfo[0];
  151. //浮水印高度
  152. $waterHeight=$waterInfo[1];
  153. //依不同的情況建立不同的類型gif jpeg png
  154. $w_img=null;
  155. switch($waterInfo[2]){
  156. case 1:
  157. $w_img=imagecreatefromgif($waterImg);
  158. $w_img=imagecreatefromgif($waterImg);
  159. break;
  160. case 2:
  161. $w_img=imagecreatefromjpeg($waterImg);
  162. break;
  163. case 3:
  164. $w_img=imagecreatefrompng($waterImg); {//浮水印圖片失效,使用文字浮水印
  165. if(empty($text)||strlen($textColor)!==7){
  166. return false;
  167. }
  168. //取得文字水印盒子資訊
  169. $textInfo=imagettfbbox($this->waterTextSize,0,$this->waterFont,$text);
  170. //文字資訊寬度
  171. $textWidth=$textInfo[2]-$ textInfo[6];
  172. //文字資訊高度
  173. $textHeight=$textInfo[3]-$textInfo[7];
  174. }
  175. //浮水印位置
  176. $x=$y=20;
  177. switch($pos){
  178. case 1:
  179. break;
  180. case 2:
  181. $x= ($imgWidth-$waterWidth)/2;
  182. 中斷;
  183. 情況3:
  184. $y=$imgWidth-$waterWidth-10;
  185. 中斷;
  186. 狀況4:
  187. $ x=($imgHeight-$waterHeight)/2;
  188. 中斷;
  189. 情況5:
  190. $x=($imgWidth-$waterWidth)/2;
  191. $y=($imgHeight-$水高)/2;
  192. 中斷;
  193. 案例6:
  194. $x=$imgWidth-$waterWidth-10;
  195. $y=($imgHeight-$waterHeight)/2;
  196. 中斷;
  197. 案例7:
  198. $x=$imgHeight-$waterHeight-10;
  199. 中斷;
  200. 情況8:
  201. $x=($imgWidth-$waterWidth)/2;
  202. $y=$ imgHeight-$waterHeight-10;
  203. 中斷;
  204. 情況9:
  205. $x=$imgWidth-$waterWidth-10;
  206. $y=$imgHeight-$waterHeight- 10;
  207. 中斷;
  208. 預設值:
  209. $x=mt_rand(20,$imgWidth-$waterWidth);
  210. $y=mt_rand(20,$imgHeight-$waterHeight);
  211. $y=mt_rand(20,$imgHeight-$waterHeight);
  212. }
  213. if($waterImgOn){//當水印圖片有效時,以圖片形式加水印
  214. if($waterInfo[2]==3){
  215. imagecopy($resImg,$ w_img,$ x,$y,0,0,$waterWidth,$waterHeight);
  216. }else{
  217. imagecopymerge($resImg,$w_img,$x,$y,0,0,$waterInfo,$ waterHeight,$ pct);
  218. }
  219. }else{//水印圖片無效,以文字浮水印加
  220. $red=hexdec(substr($this->waterTextColor,1,2));
  221. $greem =hexdec(substr($this->waterTextColor,3,2));
  222. $blue=hexdec(substr($this->waterTextColor,5,2));
  223. $color =imagecolorallocate($resImg, $red,$green,$blue);
  224. imagettftext($resImg,$this->waterTextSize,0,$x,$y,$color,$this->waterFont,$ text);
  225. }
  226. // 輸出圖片
  227. switch($imgInfo[2]){
  228. case 1:
  229. imagegif($resImg,$outImg);
  230. ;
  231. 狀況2:
  232. imagejpeg($resImg,$outImg);
  233. 中斷;
  234. 情況3:
  235. imagepng($resImg,$outImg);
  236. 中斷;
  237. }
  238. // 垃圾回收> 垃圾回收量> if(isset($resImg)){
  239. imagedestroy($resImg);
  240. }
  241. if(isset($w_img)){
  242. imagedestroy($w_img);
  243. }
  244. 回傳true;
  245. }
  246. }
  247. ?>
複製程式碼

  1. return array(
  2. //水印處理
  3. "WATER_ON"=>1,//水印開關
  4. "WATERIMG "=>"./data/logo.png",//浮水印圖片
  5. "WATER_POS"=>9,//水印位置
  6. "WATER_PCT"=>80,//水印圖案
  7. "WATER_TEXT "=>"http://www.caoxiaobin.cn",
  8. "WATER_FONT"=>"./data/simsunb.ttf",//水印字體
  9. "WATER_TEXT_COLOR" =>"#333333", //文字顏色16睡眠表示
  10. "WATER_TEXT_SIZE"=>16,//文字大小
  11. "WATER_QUA"=>80,//圖片壓縮比
  12. //最小
  13. "THUMB_WIDTH"=> 150,//縮率圖寬度
  14. "THUMB_HEIGHT"=>150,//最小高度
  15. "THUMB_TYPE"=>1,//處理每小時1 寬度固定,高度自增2 高度固定,寬度自增// 每小時尺寸,將原圖裁切
  16. "THUMB_ENDFIX"=>"_thmub"http://每小時後綴
  17. );
  18. ?>
複製程式碼

  1. /*
  2. * 不區(qū)分大小寫的資料鍵偵測
  3. */
  4. function array_key_exists_d($key,$arr){
  5. $_key=strtolower($key);
  6. foreach ($arr as $k=>$v){
  7. if($_key==strtolower($k)){
  8. return true;
  9. }
  10. }
  11. }
  12. /*
  13. * 遞歸更改陣列的KEY(鍵名)
  14. * @param array;
  15. * @stat int 0小寫1大寫
  16. * /
  17. function array_change_key_case_d($arr,$stat=0){
  18. $func=$stat?"strtoupper":"strtolower";
  19. $_newArr=array();
  20. if(!is_array ($arr)||empty($arr)){
  21. return $_newArr;
  22. }
  23. foreach($arr as $k=>$v){
  24. $_k=$func($ k);//透過變數(shù)函數(shù)轉換KEY大小寫
  25. $_newArr[$_k]= is_array($v)?array_change_key_case_d($v):$v;
  26. }
  27. return $_newArr;
  28. }
  29. /*
  30. * 讀取與設定設定項
  31. * @param $name void 設定項名稱,如果不填入傳回所有設定項
  32. * @param $value void 設定項的值
  33. * @param $value 值false null 只取$name值
  34. */
  35. function C($name=null,$value=null){
  36. static $config=array();/ /靜態(tài)變數(shù)$config儲存所有設定項
  37. if(is_null($name)){
  38. return $config;
  39. }
  40. //如果$name為陣列
  41. if(is_array($ name)){
  42. return $config=array_merge($config,array_change_key_case_d($name,1));
  43. }
  44. //$name為字串2種情況$value無值表示取得設定項的值,有值表示更改配置項
  45. if(is_string($name)){
  46. $name= strtoupper($name);
  47. //取得設定項的值
  48. if(is_null( $value)){
  49. return array_key_exists_d($name,$config)?$config[$name]:null;
  50. }else{
  51. //設定值
  52. $config[$name]= $value;
  53. return true;
  54. }
  55. }
  56. }
複製程式碼

PHP, 縮率圖


本網(wǎng)站聲明
本文內容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1601
29
PHP教程
1502
276
PHP變量範圍解釋了 PHP變量範圍解釋了 Jul 17, 2025 am 04:16 AM

PHP變量作用域常見問題及解決方法包括:1.函數(shù)內部無法訪問全局變量,需使用global關鍵字或參數(shù)傳入;2.靜態(tài)變量用static聲明,只初始化一次並在多次調用間保持值;3.超全局變量如$_GET、$_POST可在任何作用域直接使用,但需注意安全過濾;4.匿名函數(shù)需通過use關鍵字引入父作用域變量,修改外部變量則需傳遞引用。掌握這些規(guī)則有助於避免錯誤並提升代碼穩(wěn)定性。

如何在PHP中牢固地處理文件上傳? 如何在PHP中牢固地處理文件上傳? Jul 08, 2025 am 02:37 AM

要安全處理PHP文件上傳需驗證來源與類型、控製文件名與路徑、設置服務器限制並二次處理媒體文件。 1.驗證上傳來源通過token防止CSRF並通過finfo_file檢測真實MIME類型使用白名單控制;2.重命名文件為隨機字符串並根據(jù)檢測類型決定擴展名存儲至非Web目錄;3.PHP配置限制上傳大小及臨時目錄Nginx/Apache禁止訪問上傳目錄;4.GD庫重新保存圖片清除潛在惡意數(shù)據(jù)。

在PHP中評論代碼 在PHP中評論代碼 Jul 18, 2025 am 04:57 AM

PHP註釋代碼常用方法有三種:1.單行註釋用//或#屏蔽一行代碼,推薦使用//;2.多行註釋用/.../包裹代碼塊,不可嵌套但可跨行;3.組合技巧註釋如用/if(){}/控制邏輯塊,或配合編輯器快捷鍵提升效率,使用時需注意閉合符號和避免嵌套。

發(fā)電機如何在PHP中工作? 發(fā)電機如何在PHP中工作? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

撰寫PHP評論的提示 撰寫PHP評論的提示 Jul 18, 2025 am 04:51 AM

寫好PHP註釋的關鍵在於明確目的與規(guī)範,註釋應解釋“為什麼”而非“做了什麼”,避免冗餘或過於簡單。 1.使用統(tǒng)一格式,如docblock(/*/)用於類、方法說明,提升可讀性與工具兼容性;2.強調邏輯背後的原因,如說明為何需手動輸出JS跳轉;3.在復雜代碼前添加總覽性說明,分步驟描述流程,幫助理解整體思路;4.合理使用TODO和FIXME標記待辦事項與問題,便於後續(xù)追蹤與協(xié)作。好的註釋能降低溝通成本,提升代碼維護效率。

學習PHP:初學者指南 學習PHP:初學者指南 Jul 18, 2025 am 04:54 AM

易於效率,啟動啟動tingupalocalserverenverenvirestoolslikexamppandacodeeditorlikevscode.1)installxamppforapache,mysql,andphp.2)uscodeeditorforsyntaxssupport.3)

如何通過php中的索引訪問字符串中的字符 如何通過php中的索引訪問字符串中的字符 Jul 12, 2025 am 03:15 AM

在PHP中獲取字符串特定索引字符可用方括號或花括號,但推薦方括號;索引從0開始,超出範圍訪問返回空值,不可賦值;處理多字節(jié)字符需用mb_substr。例如:$str="hello";echo$str[0];輸出h;而中文等字符需用mb_substr($str,1,1)獲取正確結果;實際應用中循環(huán)訪問前應檢查字符串長度,動態(tài)字符串需驗證有效性,多語言項目建議統(tǒng)一使用多字節(jié)安全函數(shù)。

快速PHP安裝教程 快速PHP安裝教程 Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

See all articles