PHPIN.NET

 找回密码
 立即注册
查看: 9854|回复: 0

[7.X版] 帝国CMS缩略图函数改进(GD库改进)

[复制链接]

469

主题

31

回帖

5509

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5509
发表于 2014-2-22 16:55:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
帝国CMS缩略图函数改进(GD库改进)
本文关键字词:缩略图,缩略图函数,帝国CMS,帝国CMS,帝国CMS缩略图去黑边框

第一种方式 帝国默认:
sys_ResizeImg($r[titlepic],宽,高,0);//帝国默认的不裁剪缩放生成缩略图的方式

第二种方式 帝国默认:
sys_ResizeImg($r[titlepic],宽,高,1);//帝国默认的裁剪缩放生成缩略图的方式

第三种方式 去掉裁剪不够时的黑边并且从图片缩放后中间裁剪:
sys_ResizeImg($r[titlepic],宽,高,2);//新加去黑边裁剪生成缩略图的方式

第四种方式 只固定图片的宽,高度不限制(类似不规则瀑布流的图片形式),高填写为大于0的任意整数数字:
sys_ResizeImg($r[titlepic],宽,高,3);//新加去黑边生成类似瀑布流格式的方式

/e/class/gd.php 函数全部代码如下:


  1. <?php
  2. define('InEmpireCMSGd',TRUE);

  3. //原文件,新文件,宽度,高度,维持比例
  4. function ResizeImage($big_image_name, $new_name, $max_width = 400, $max_height = 400, $resize = 1){
  5.         $returnr['file']='';
  6.         $returnr['filetype']='';
  7.         if($temp_img_type = @getimagesize($big_image_name)) {preg_match('/\/([a-z]+)$/i', $temp_img_type[mime], $tpn); $img_type = $tpn[1];
  8.         }else{
  9.                 preg_match('/\.([a-z]+)$/i', $big_image_name, $tpn); $img_type = $tpn[1];
  10.         }
  11.         $all_type = array(
  12.                 "jpg" => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg" , "exn"=>".jpg"),
  13.                 "gif" => array("create"=>"ImageCreateFromGIF" , "output"=>"imagegif" , "exn"=>".gif"),
  14.                 "jpeg" => array("create"=>"ImageCreateFromjpeg", "output"=>"imagejpeg" , "exn"=>".jpg"),
  15.                 "png" => array("create"=>"imagecreatefrompng" , "output"=>"imagepng" , "exn"=>".png"),
  16.                 "wbmp" => array("create"=>"imagecreatefromwbmp", "output"=>"image2wbmp" , "exn"=>".wbmp")
  17.         );

  18.         $func_create = $all_type[$img_type]['create'];
  19.         if(empty($func_create) or !function_exists($func_create)) {
  20.                 return $returnr;
  21.         }
  22. //输出
  23.         $func_output = $all_type[$img_type]['output'];
  24.         $func_exname = $all_type[$img_type]['exn'];
  25.         if(($func_exname=='.gif'||$func_exname=='.png'||$func_exname=='.wbmp')&&!function_exists($func_output)) {
  26.                 $func_output='imagejpeg';
  27.                 $func_exname='.jpg';
  28.         }
  29.         $big_image = $func_create($big_image_name);
  30.         $big_width = imagesx($big_image);
  31.         $big_height = imagesy($big_image);
  32.         if($big_width <= $max_width and $big_height <= $max_height) {
  33.                 $func_output($big_image, $new_name.$func_exname);
  34.                 $returnr['file']=$new_name.$func_exname;
  35.                 $returnr['filetype']=$func_exname;
  36.                 return $returnr;
  37.         }
  38.         $ratiow = $max_width / $big_width;
  39.         $ratioh = $max_height / $big_height;
  40.         $new_width = ($ratiow > 1) ? $big_width : $max_width;
  41.         $new_height = ($ratioh > 1) ? $big_height : $max_height;
  42.         if($resize == 1) {
  43.                 if($big_width >= $max_width and $big_height >= $max_height) {
  44.                         if($big_width > $big_height) {
  45.                                 $tempx = $max_width / $ratioh;
  46.                                 $tempy = $big_height;
  47.                                 $srcX = ($big_width - $tempx) / 2;
  48.                                 $srcY = 0;
  49.                         } else {
  50.                                 $tempy = $max_height / $ratiow;
  51.                                 $tempx = $big_width;
  52.                                 $srcY = ($big_height - $tempy) / 2;
  53.                                 $srcX = 0;
  54.                         }
  55.                 } else {
  56.                         if($big_width > $big_height){
  57.                                 $tempx = $max_width;
  58.                                 $tempy = $big_height;
  59.                                 $srcX = ($big_width - $tempx) / 2;
  60.                                 $srcY = 0;
  61.                         } else {
  62.                                 $tempy = $max_height;
  63.                                 $tempx = $big_width;
  64.                                 $srcY = ($big_height - $tempy) / 2;
  65.                                 $srcX = 0;
  66.                         }
  67.                 }
  68.         }elseif($resize == 2){//同比例缩放超出裁切

  69.                 if($big_width >= $max_width and $big_height >= $max_height){
  70.                         if($max_width >= ($big_width * $ratioh)){
  71.                                 $tempx=$big_width;
  72.                                 $tempy=$max_height / $ratiow;
  73.                                 $srcX=0;
  74.                                 $srcY=($big_height - $tempy) / 2;
  75.                         }elseif($max_height >= ( $big_height * $ratiow)){
  76.                                 $tempx=$max_width / $ratioh;
  77.                                 $tempy=$big_height;
  78.                                 $srcX=($big_width - $tempx) / 2;
  79.                                 $srcY=0;
  80.                         }else{
  81.                                 $tempx=$max_width;
  82.                                 $tempy=$big_height;
  83.                                 $srcX=($big_width - $tempx) / 2;
  84.                                 $srcY=0;
  85.                         }
  86.                 }else{
  87.                         if($max_height >= $big_height){
  88.                                 $tempx=$max_width;
  89.                                 $tempy=$big_height;
  90.                                 $srcX=($big_width - $max_width) / 2;
  91.                                 $srcY=0;
  92.                         }elseif($max_width >= $big_width){
  93.                                 $tempx=$big_width;
  94.                                 $tempy=$max_height;
  95.                                 $srcX=0;
  96.                                 $srcY=($big_height - $max_height) / 2;
  97.                         }
  98.                 }
  99.         }elseif($resize == 3){//宽度固定 高度同比例任意
  100.                 $srcX = 0;
  101.                 $srcY = 0;
  102.                 $tempx = $big_width;
  103.                 $tempy = $big_height;
  104.                 if($big_width >= $max_width){
  105.                         $new_height=$big_height*$ratiow;
  106.                 }else{
  107.                         $new_height=$big_height;
  108.                 }
  109.         }else { //不保持比例
  110.                 $srcX = 0;
  111.                 $srcY = 0;
  112.                 $tempx = $big_width;
  113.                 $tempy = $big_height;
  114.         }
  115.         if(function_exists("imagecopyresampled")){
  116.                 $temp_image = imagecreatetruecolor($new_width, $new_height);
  117.                 //echo $tempx;exit;
  118.                 imagecopyresampled($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);
  119.         } else {
  120.                 $temp_image = imagecreate($new_width, $new_height);
  121.                 imagecopyresized($temp_image, $big_image, 0, 0, $srcX, $srcY, $new_width, $new_height, $tempx, $tempy);
  122.         }
  123.         /*增加高质量输出图像*/
  124.         $zhiliang=$func_exname=='.png'?9:100;
  125.         $func_output($temp_image, $new_name.$func_exname,$zhiliang);
  126. //$func_output($temp_image, $new_name.$func_exname);
  127.         ImageDestroy($big_image);
  128.         ImageDestroy($temp_image);
  129.         $returnr['file']=$new_name.$func_exname;
  130.         $returnr['filetype']=$func_exname;
  131.         return $returnr;
  132. }

  133. /*
  134. * 功能:图片加水印 (水印支持图片或文字)
  135. * 参数:
  136. * $groundImage背景图片,即需要加水印的图片,暂只支持GIF,JPG,PNG格式;
  137. * $waterPos水印位置,有10种状态,0为随机位置;
  138. *1为顶端居左,2为顶端居中,3为顶端居右;
  139. *4为中部居左,5为中部居中,6为中部居右;
  140. *7为底端居左,8为底端居中,9为底端居右;
  141. * $waterImage图片水印,即作为水印的图片,暂只支持GIF,JPG,PNG格式;
  142. * $waterText文字水印,即把文字作为为水印,支持ASCII码,不支持中文;
  143. * $textFont文字大小,值为1、2、3、4或5,默认为5;
  144. * $textColor文字颜色,值为十六进制颜色值,默认为#FF0000(红色);
  145. *
  146. * 注意:Support GD 2.0,Support FreeType、GIF Read、GIF Create、JPG 、PNG
  147. * $waterImage 和 $waterText 最好不要同时使用,选其中之一即可,优先使用 $waterImage。
  148. * 当$waterImage有效时,参数$waterString、$stringFont、$stringColor均不生效。
  149. * 加水印后的图片的文件名和 $groundImage 一样。
  150. */
  151. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=5,$textColor="#FF0000",$myfontpath="../data/mask/cour.ttf",$w_pct,$w_quality){
  152.         global $fun_r,$editor;
  153.         if($editor==1){$a='../';}
  154.         elseif($editor==2){$a='../../';}
  155.         elseif($editor==3){$a='../../../';}
  156.         else{$a='';}
  157.         $waterImage=$waterImage?$a.$waterImage:'';
  158.         $myfontpath=$myfontpath?$a.$myfontpath:'';
  159.         $isWaterImage = FALSE;
  160.         $formatMsg = $fun_r['synotdotype'];

  161. //读取水印文件
  162.         if(!empty($waterImage) && file_exists($waterImage)){
  163.                 $isWaterImage = TRUE;
  164.                 $water_info = getimagesize($waterImage);
  165.                 $water_w= $water_info[0];//取得水印图片的宽
  166.                 $water_h= $water_info[1];//取得水印图片的高

  167.                 switch($water_info[2]){//取得水印图片的格式
  168.                         case 1:$water_im = imagecreatefromgif($waterImage);break;
  169.                         case 2:$water_im = imagecreatefromjpeg($waterImage);break;
  170.                         case 3:$water_im = imagecreatefrompng($waterImage);break;
  171.                         default:echo $formatMsg;return "";
  172.                 }
  173.         }

  174. //读取背景图片
  175.         if(!empty($groundImage) && file_exists($groundImage)) {
  176.                 $ground_info = getimagesize($groundImage);
  177.                 $ground_w= $ground_info[0];//取得背景图片的宽
  178.                 $ground_h= $ground_info[1];//取得背景图片的高

  179.                 switch($ground_info[2]){//取得背景图片的格式
  180.                         case 1:$ground_im = imagecreatefromgif($groundImage);break;
  181.                         case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  182.                         case 3:$ground_im = imagecreatefrompng($groundImage);break;
  183.                         default:echo $formatMsg;return "";
  184.                 }
  185.         }else {
  186.                 echo $fun_r['synotdoimg'];
  187.                 return "";
  188.         }

  189. //水印位置
  190.         if($isWaterImage){//图片水印
  191.                 $w = $water_w;
  192.                 $h = $water_h;
  193.                 $label = "图片的";
  194.         }else{//文字水印
  195.                 $temp = imagettfbbox(ceil($textFont*2.5),0,$myfontpath,$waterText);//取得使用 TrueType 字体的文本的范围
  196.                 $w = $temp[2] - $temp[6];
  197.                 $h = $temp[3] - $temp[7];
  198.                 unset($temp);
  199.                 $label = "文字区域";
  200.         }
  201.         if( ($ground_w<$w) || ($ground_h<$h) ) {
  202.                 echo $fun_r['sytoosmall'];
  203.                 return '';
  204.         }
  205.         switch($waterPos) {
  206.                 case 0://随机
  207.                         $posX = rand(0,($ground_w - $w));
  208.                         $posY = rand(0,($ground_h - $h));
  209.                         break;
  210.                 case 1://1为顶端居左
  211.                         $posX = 0;
  212.                         $posY = 0;
  213.                         break;
  214.                 case 2://2为顶端居中
  215.                         $posX = ($ground_w - $w) / 2;
  216.                         $posY = 0;
  217.                         break;
  218.                 case 3://3为顶端居右
  219.                         $posX = $ground_w - $w;
  220.                         $posY = 0;
  221.                         break;
  222.                 case 4://4为中部居左
  223.                         $posX = 0;
  224.                         $posY = ($ground_h - $h) / 2;
  225.                         break;
  226.                 case 5://5为中部居中
  227.                         $posX = ($ground_w - $w) / 2;
  228.                         $posY = ($ground_h - $h) / 2;
  229.                         break;
  230.                 case 6://6为中部居右
  231.                         $posX = $ground_w - $w;
  232.                         $posY = ($ground_h - $h) / 2;
  233.                         break;
  234.                 case 7://7为底端居左
  235.                         $posX = 0;
  236.                         $posY = $ground_h - $h;
  237.                         break;
  238.                 case 8://8为底端居中
  239.                         $posX = ($ground_w - $w) / 2;
  240.                         $posY = $ground_h - $h;
  241.                         break;
  242.                 case 9://9为底端居右
  243.                         $posX = $ground_w - $w;
  244.                         $posY = $ground_h - $h;
  245.                         break;
  246.                 default://随机
  247.                         $posX = rand(0,($ground_w - $w));
  248.                         $posY = rand(0,($ground_h - $h));
  249.                         break;
  250.         }

  251. //设定图像的混色模式
  252.         imagealphablending($ground_im, true);

  253.         if($isWaterImage){//图片水印
  254.                 if($water_info[2]==3) {
  255.                         imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件
  256.                 }else {
  257.                         imagecopymerge($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h,$w_pct);//拷贝水印到目标文件
  258.                 }
  259.         }else{//文字水印
  260.                 if( !empty($textColor) && (strlen($textColor)==7) ) {
  261.                         $R = hexdec(substr($textColor,1,2));
  262.                         $G = hexdec(substr($textColor,3,2));
  263.                         $B = hexdec(substr($textColor,5));
  264.                 }else {
  265.                         echo $fun_r['synotfontcolor'];
  266.                         return "";
  267.                 }
  268.                 imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  269.         }

  270. //生成水印后的图片
  271.         @unlink($groundImage);
  272.         switch($ground_info[2]){//取得背景图片的格式
  273.                 case 1:imagegif($ground_im,$groundImage);break;
  274.                 case 2:imagejpeg($ground_im,$groundImage,$w_quality);break;
  275.                 case 3:imagepng($ground_im,$groundImage);break;
  276.                 default:echo $formatMsg;return "";
  277.         }

  278. //释放内存
  279.         if(isset($water_info)) unset($water_info);
  280.         if(isset($water_im)) imagedestroy($water_im);
  281.         unset($ground_info);
  282.         imagedestroy($ground_im);
  283. }
  284. ?>
复制代码

懒人下载包: gd.php (9.5 KB, 下载次数: 400)


2019/04/08  修复 宽高与原图宽高完全相同生成黑图的BUG,请及时更新!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-4-24 03:39

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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