PHP は画像に透かしを追加し、サムネイルを生成します

WBOY
リリース: 2016-07-25 08:42:31
オリジナル
791 人が閲覧しました
1 画像にウォーターマークを追加する
2 生成一新的缩率图
  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;
  10. private $waterTextColor;
  11. private $qua;
  12. //缩略图配置项
  13. private $thumbWidth;
  14. private $thumbHeight;
  15. private $thumbType;
  16. private $thumbEndfix;
  17. / /构造関数数
  18. public function __construct(){
  19. $this->waterOn=C("WATER_ON");
  20. $this->waterImg=C("WATER_IMG");
  21. $this->waterPos=C( "WATER_POS");
  22. $this->waterPct=C("WATER_PCT");
  23. $this->waterText=C("WATER_TEXT");
  24. $this->waterFont=C("WATER_FONT");
  25. $this->waterTextSize=C("WATER_TEXT_SIZE");
  26. $this->waterTextColor=C("WATER_TEXT_COLOR");
  27. $this->qua=C("WATER_QUA");
  28. //缩率図
  29. $this->thumbWidth=C("THUMB_WIDTH");
  30. $this->thumbHeight=C("THUMB_HEIGHT");
  31. $this->thumbType=C("THUMB_TYPE");
  32. $this- >thumbEndFix=C("THUMB_ENDFIX");
  33. }
  34. /*
  35. *验证写真が合法かどうか
  36. */
  37. private function check($img){
  38. return is_file($img)&&getimagesize($img)&&extension_loaded("gd ");
  39. }
  40. /*
  41. *缩率图
  42. *@param string $img 原图
  43. *@param string $outFile 缩率その後に保存される画像
  44. *@param int $thumbWidth 缩率图宽度
  45. *@param int $thumbHeight 缩率图高
  46. *@param int $thumbType 那种方式で缩略処理
  47. */
  48. public functionThumb($img,$outFile="",$thumbWidth="",$thumbHeight="", $thumbType=""){
  49. if(!$this->check($img)){
  50. return false;
  51. }
  52. //缩率图处処理方式
  53. $thumbType=$thumbType?$thumbType:$this- >thumbType;
  54. //缩率图宽度
  55. $thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;
  56. //缩率图高
  57. $thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
  58. //获取原图情報
  59. $imgInfo=getimagesize($img);
  60. //原图宽度
  61. $imgWidth =$imgInfo[0];
  62. //原画像の高さ
  63. $imgHeight=$imgInfo[1];
  64. //原画像の種類
  65. を取得 $imgtype=image_type_to_extension($imgInfo[2]);
  66. //異なる缩に基づく略処理方式で、寸法(原図と略図の対応する寸法)を取得します
  67. $thumb_size=$this->thumbsize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
  68. //创建原図
  69. $func="imagecreatefrom".substr($imgtype,1);//变量関数数
  70. $resImg=$func($img);
  71. //创建缩率图画布
  72. if($imgtype==".gif") {
  73. $res_thumb=imagecreate($thumb_size[2],$thumb_size[3]);
  74. }else{
  75. $res_thumb=imagecreatetruecolor($thumb_size[2],$thumb_size[3]);
  76. }
  77. imagecopyresize($res_thumb ,$resImg,0,0,0,0,$thumb_size[2],$thumb_size[3],$thumb_size[0],$thumb_size[1]);
  78. $fileInfo=pathinfo($img);//文件情報
  79. $outFile=$outFile?$outFile:$fileInfo['filename'].$this->thumbEndFix.$fileInfo['extension'];//ファイル名
  80. $outFile=$fileInfo["dirname"]. "/".$outFile;//上目录
  81. $func="image".substr($imgtype,1);
  82. $func($res_thumb,$outFile);
  83. $outFile を返す;
  84. }
  85. プライベート関数thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
  86. //缩率图尺寸
  87. $w=$thumbWidth;
  88. $h=$thumbHeight;
  89. //原图尺寸
  90. $ img_w=$imgWidth;
  91. $img_h=$imgHeight;
  92. switch($thumbType){
  93. case 1:
  94. //宽度固定、高自增
  95. $h=$w/$imgWidth*$imgHeight;
  96. Break;
  97. case 2:// 高さ固定、宽度自
  98. $w=$h/$imgHeight*$imgWidth;
  99. Break;
  100. case 3:
  101. if($imgHeight/$thumbHeight>$imgWidth/$thumbWidth){
  102. $img_h=$ imgWidth/$thumbWidth*$thumbHeight;
  103. }else{
  104. $img_w=$imgHeight/$thumbHeight*$thumbWidth;
  105. }
  106. }
  107. return array($img_w,$img_h,$w,$h);
  108. }
  109. / *
  110. *@param string $img 原图
  111. *@param string $outImg 加完水印後生成の画像
  112. *@param int $pos 水印位置
  113. *@param int $pct 透明度
  114. *@param text $text 水印文字
  115. *@param string $waterImg水印图片
  116. */
  117. public function Water($img,$outImg=null,$pos="",$pct="",$text="",$waterImg="",$ textColor=""){
  118. if(!$this->check($img)){
  119. return false;
  120. }
  121. //加完水印後生成的图
  122. $outImg=$outImg?$outImg:$img ;
  123. //水印の位置
  124. $pos=$pos?$pos:$this->waterPos;
  125. //透明度
  126. $pct=$pct?$pct:$this->waterPct;
  127. //水印文字
  128. $text=$text?$text:$this->waterText;
  129. //水印图片
  130. $waterImg=$waterImg?$waterImg:$this->waterImg;
  131. //验证水印图片
  132. $waterImgOn=$this ->check($waterImg);
  133. //水印文字颜色
  134. $textColor=$textColor?$textColor:$this->waterTextColor;
  135. //原图情報
  136. $imgInfo=getimagesize($img);
  137. //原图宽度
  138. $imgWidth=$ imgInfo[0];
  139. //原图高さ
  140. $imgHeight=$imgInfo[1];
  141. switch($imgInfo[2]){
  142. case 1:
  143. $resImg=imagecreatefromgif($img);
  144. Break;
  145. case 2:
  146. $resImg=imagecreatefromjpeg($img);
  147. ブレーク;
  148. ケース 3:
  149. $resImg=imagecreatefrompng($img);
  150. ブレーク;
  151. }
  152. if($waterImgOn){//水印图片有效
  153. //水印情報
  154. $waterInfo=getimagesize($waterImg);
  155. //水印宽​​度
  156. $waterWidth=$waterInfo[0];
  157. //水印高
  158. $waterHeight=$waterInfo[1];
  159. //異なる状況に応じて異なる種類のgif jpeg png
  160. $w_img=null;
  161. switch($waterInfo[2]){
  162. case 1:
  163. $w_img=imagecreatefromgif($ WaterImg);
  164. ブレーク;
  165. ケース 2:
  166. $w_img=imagecreatefromjpeg($waterImg);
  167. ブレーク;
  168. ケース 3:
  169. $w_img=imagecreatefrompng($waterImg);
  170. }
  171. }else{//水印图片失效,文字水印を使用
  172. if(empty($text)||strlen($textColor)!==7){
  173. return false;
  174. }
  175. //获得文字水印盒子情報
  176. $textInfo=imagettfbbox($this->waterTextSize,0,$this->waterFont,$text);
  177. //文字情報宽度
  178. $textWidth=$textInfo[2]-$textInfo[6];
  179. / /文字情報高さ
  180. $textHeight=$textInfo[3]-$textInfo[7];
  181. }
  182. //水印の位置
  183. $x=$y=20;
  184. switch($pos){
  185. case 1:
  186. Break;
  187. case 2:
  188. $x=($imgWidth-$waterWidth)/2;
  189. Break;
  190. ケース 3:
  191. $y=$imgWidth-$waterWidth-10;
  192. ブレーク;
  193. ケース 4:
  194. $x=($imgHeight-$waterHeight)/2;
  195. ブレーク;
  196. ケース 5:
  197. $x=($imgWidth -$waterWidth)/2;
  198. $y=($imgHeight-$waterHeight)/2;
  199. ブレーク;
  200. ケース 6:
  201. $x=$imgWidth-$waterWidth-10;
  202. $y=($imgHeight-$waterHeight)/2;
  203. ブレーク;
  204. ケース 7:
  205. $x=$imgHeight-$waterHeight- 10;
  206. ブレーク;
  207. ケース 8:
  208. $x=($imgWidth-$waterWidth)/2;
  209. $y=$imgHeight-$waterHeight-10;
  210. ブレーク;
  211. ケース 9:
  212. $x=$imgWidth-$ WaterWidth-10;
  213. $y=$imgHeight-$waterHeight-10;
  214. ブレーク;
  215. デフォルト:
  216. $x=mt_rand(20,$imgWidth-$waterWidth);
  217. $y=mt_rand(20,$imgHeight-$waterHeight);
  218. }
  219. if($waterImgOn){//当水印画像がある場合、画像形式で水印
  220. if($waterInfo[2]==3){
  221. imagecopy($resImg,$w_img,$x,$y, 0,0,$waterWidth,$waterHeight);
  222. }else{
  223. imagecopymerge($resImg,$w_img,$x,$y,0,0,$waterInfo,$waterHeight,$pct);
  224. }
  225. }else{ //水印画片失效,文字に水印加
  226. $red=hexdec(substr($this->waterTextColor,1,2));
  227. $greem=hexdec(substr($this->waterTextColor,3,2) );
  228. $blue=hexdec(substr($this->waterTextColor,5,2));
  229. $color=imagecolorallocate($resImg,$red,$greem,$blue);
  230. imagettftext($resImg,$this ->waterTextSize,0,$x,$y,$color,$this->waterFont,$text);
  231. }
  232. //出射図片
  233. switch($imgInfo[2]){
  234. case 1:
  235. imagegif ($resImg,$outImg);
  236. ブレーク;
  237. ケース 2:
  238. imagejpeg($resImg,$outImg);
  239. ブレーク;
  240. ケース 3:
  241. imagepng($resImg,$outImg);
  242. ブレーク;
  243. }
  244. //垃圾回收
  245. if(isset($resImg)){
  246. imagedestroy($resImg);
  247. }
  248. if(isset($w_img)){
  249. imagedestroy($w_img);
  250. }
  251. return true;
  252. }
  253. }
  254. ?>
复制代

  1. return array(
  2. //水印处理
  3. "WATER_ON"=>1,//水印开关
  4. "WATER_IMG"=>>"./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 "//缩略图后缀
  17. );
  18. ?>
复制代码

    /*
  1. * 大文字と小文字を区別しないデータキー検出
  2. */
  3. function array_key_exists_d($key,$arr){
  4. $_key=strto lower($key);
  5. foreach ($arr as $k= > $v){
  6. if($_key==strto lower($k)){
  7. return true;
  8. }
  9. }
  10. }
  11. /*
  12. * 配列のKEY(キー名)を再帰的に変更します
  13. * @param array;
  14. * @stat int 0 小文字 1 大文字
  15. */
  16. function array_change_key_case_d($arr,$stat=0){
  17. $func=$stat?"strtoupper":"strto lower";
  18. $_newArr=array();
  19. if (!is_array($arr)||empty($arr)){
  20. return $_newArr;
  21. }
  22. foreach($arr as $k=>$v){
  23. $_k=$func($k);/ /変数関数で KEY ケースを変換
  24. $_newArr[$_k]= is_array($v)?array_change_key_case_d($v):$v;
  25. }
  26. return $_newArr;
  27. }
  28. /*
  29. * 設定項目を読み取って設定します
  30. * @param $name void 設定項目名。入力されていない場合は、すべての設定項目が返されます
  31. * @param $value void 設定項目の値
  32. * @param $value value false null $name 値のみを取得します
  33. */
  34. function C ($name=null,$value=null){
  35. static $config=array();//静的変数 $config にはすべての設定項目が格納されます
  36. if(is_null($name)){
  37. return $config;
  38. }
  39. / / $name が配列の場合
  40. if(is_array($name)){
  41. return $config=array_merge($config,array_change_key_case_d($name,1));
  42. }
  43. // $name が配列である 2 つのケースstring$value 値が無い場合は設定項目の値を取得することを意味し、値がある場合は設定項目を変更することを意味します
  44. if(is_string($name)){
  45. $name= strtoupper($name);
  46. //設定項目の値を取得する
  47. if(is_null($value )){
  48. return array_key_exists_d($name,$config)?$config[$name]:null;
  49. }else{
  50. //値を設定
  51. $config[$name]=$value;
  52. return true;
  53. }
  54. }
  55. }
コードをコピー

PHP、サムネイル画像

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!