最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

ckeditor上传文件重命名并加水印配置方法

时间:2014-06-09 16:40来源: 作者: 点击: 次
首先:我希望上传的文件根据日期来组织文件夹,请修改editoreditorfilemanagerconnectorsphp文件夹下的config.php文件,找到如下的内容: //Pathtouserfilesrelativetothedocumentroot. $Config [ UserFilesPath ]= 修改为: //

首先:我希望上传的文件根据日期来组织文件夹,请修改editoreditorfilemanagerconnectorsphp文件夹下的config.php文件,找到如下的内容:

  1. //Path to user files relative to the document root. 
  2. $Config['UserFilesPath'] = 

修改为:

  1. //Path to user files relative to the document root. 
  2. $Config['UserFilesPath'] = '/uploadfiles/'.date("Ym")."/" ; 

这样上传的文件就按照日期存放了,其次,重命名,请修改该文件夹下的io.php文件,找到如下代码:

  1. // Do a cleanup of the file name to avoid possible problems 
  2. function SanitizeFileName( $sNewFileName ) 
  3. { 
  4. global $Config ; 
  5. $sNewFileName = stripslashes( $sNewFileName ) ; 
  6. // Replace dots in the name with underscores (only one dot can be there... security issue). 
  7. if ( $Config['ForceSingleExtension'] ) 
  8.  $sNewFileName = preg_replace( '/\.(?![^.]*$)/', '_', $sNewFileName ) ; 
  9. // Remove / | : ? * " < > 
  10. $sNewFileName = preg_replace( '/\\|\/|\||\:|\?|\*|"|<|>/', '_', $sNewFileName ); 
  11. return $sNewFileName ; 
  12. } 

修改为如下代码:

  1. // Do a cleanup of the file name to avoid possible problems 
  2. function SanitizeFileName( $sNewFileName ) 
  3. { 
  4. global $Config ; 
  5. $sNewFileName = stripslashes( $sNewFileName ) ; 
  6. // Replace dots in the name with underscores (only one dot can be there... security issue). 
  7. if ( $Config['ForceSingleExtension'] ) 
  8.  $sNewFileName = preg_replace( '/\.(?![^.]*$)/', '_', $sNewFileName ) ; 
  9. $sExtension = substr( $sNewFileName, ( strrpos($sNewFileName, '.') + 1 ) ) ; 
  10. $sNewFileName = my_setfilename().'.'.$sExtension; 
  11. return $sNewFileName ; 
  12. } 
  13. function my_setfilename(){ 
  14. $gettime = explode(' ',microtime()); 
  15. $string = 'abcdefghijklmnopgrstuvwxyz0123456789'; 
  16. $rand = ''; 
  17. for ($x=0;$x<12;$x++) 
  18.  $rand .= substr($string,mt_rand(0,strlen($string)-1),1); 
  19. return date("ymdHis").substr($gettime[0],2,6).$rand; 
  20. } 

Fckeditor上传图片文件名重名及中文乱码解决方法

经测试Fckeditor2.6.6并没有解决上传文件中文名变为乱码的问题,这是由于Fckeditor实现上传功能时并没有将文件重命名,容易导致上传图片文件重名及乱码问题。

上传图片文件重名和乱码解决方法如下

打开editor/filemanager/connectors/php目录下commands.php,找到FileUpload函数,在

  1. $sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ; 
  2. $sExtension = strtolower( $sExtension ) ; 
  3.  

后添加

$sFileName = rand(0,100).".".$sExtension;,此处rand函数可根据需要自行改变重命名规则。

另一种上传图片文件名乱码解决方法为使用iconv函数对文件名进行编码转换,但仍然存在重名问题,所以针对Fckeditor上传图片文件名最好还是重命名。

Fckeditor上传图片添加水印功能

对于网站拥有者来说保护图片版权添加水印必不可少,我们可以利用PHP添加水印函数结合Fckeditor文件上传函数FileUpload实现图片添加水印功能,水印函数请参考PHP图片水印函数:支持以图片和文字方式添加水印一文,代码如下:

  1. function setWater($imgSrc,$markImg,$markText,$TextColor,$markPos,$fontType,$markType) 
  2. { 
  3.     $srcInfo = @getimagesize($imgSrc); 
  4.     $srcImg_w    = $srcInfo[0]; 
  5.     $srcImg_h    = $srcInfo[1]; 
  6.          
  7.     switch ($srcInfo[2])  
  8.     {  
  9.         case 1:  
  10.             $srcim =imagecreatefromgif($imgSrc);  
  11.             break;  
  12.         case 2:  
  13.             $srcim =imagecreatefromjpeg($imgSrc);  
  14.             break;  
  15.         case 3:  
  16.             $srcim =imagecreatefrompng($imgSrc);  
  17.             break;  
  18.         default:  
  19.             die("不支持的图片文件类型");  
  20.             exit;  
  21.     } 
  22.          
  23.     if(!strcmp($markType,"img")) 
  24.     { 
  25.         if(!file_exists($markImg) || emptyempty($markImg)) 
  26.         { 
  27.             return; 
  28.         } 
  29.              
  30.         $markImgInfo = @getimagesize($markImg); 
  31.         $markImg_w    = $markImgInfo[0]; 
  32.         $markImg_h    = $markImgInfo[1]; 
  33.              
  34.         if($srcImg_w < $markImg_w || $srcImg_h < $markImg_h) 
  35.         { 
  36.             return; 
  37.         } 
  38.              
  39.         switch ($markImgInfo[2])  
  40.         {  
  41.             case 1:  
  42.                 $markim =imagecreatefromgif($markImg);  
  43.                 break;  
  44.             case 2:  
  45.                 $markim =imagecreatefromjpeg($markImg);  
  46.                 break;  
  47.             case 3:  
  48.                 $markim =imagecreatefrompng($markImg);  
  49.                 break;  
  50.             default:  
  51.                 die("不支持的水印图片文件类型");  
  52.                 exit;  
  53.         } 
  54.              
  55.         $logow = $markImg_w; 
  56.         $logoh = $markImg_h; 
  57.     } 
  58.          
  59.     if(!strcmp($markType,"text")) 
  60.     { 
  61.         $fontSize = 16; 
  62.         if(!emptyempty($markText)) 
  63.         { 
  64.             if(!file_exists($fontType)) 
  65.             { 
  66.                 return; 
  67.             } 
  68.         } 
  69.         else { 
  70.             return; 
  71.         } 
  72.              
  73.         $box = @imagettfbbox($fontSize, 0, $fontType,$markText); 
  74.         $logow = max($box[2], $box[4]) - min($box[0], $box[6]); 
  75.         $logoh = max($box[1], $box[3]) - min($box[5], $box[7]); 
  76.     } 
  77.          
  78.     if($markPos == 0) 
  79.     { 
  80.         $markPos = rand(1, 9); 
  81.     } 
  82.          
  83.     switch($markPos) 
  84.     { 
  85.         case 1: 
  86.             $x = +5; 
  87.             $y = +5; 
  88.             break; 
  89.         case 2: 
  90.             $x = ($srcImg_w - $logow) / 2; 
  91.             $y = +5; 
  92.             break; 
  93.         case 3: 
  94.             $x = $srcImg_w - $logow - 5; 
  95.             $y = +15; 
  96.             break; 
  97.         case 4: 
  98.             $x = +5; 
  99.             $y = ($srcImg_h - $logoh) / 2; 
  100.             break; 
  101.         case 5: 
  102.             $x = ($srcImg_w - $logow) / 2; 
  103.             $y = ($srcImg_h - $logoh) / 2; 
  104.             break; 
  105.         case 6: 
  106.             $x = $srcImg_w - $logow - 5; 
  107.             $y = ($srcImg_h - $logoh) / 2; 
  108.             break; 
  109.         case 7: 
  110.             $x = +5; 
  111.             $y = $srcImg_h - $logoh - 5; 
  112.             break; 
  113.         case 8: 
  114.             $x = ($srcImg_w - $logow) / 2; 
  115.             $y = $srcImg_h - $logoh - 5; 
  116.             break; 
  117.         case 9: 
  118.             $x = $srcImg_w - $logow - 5; 
  119.             $y = $srcImg_h - $logoh -5; 
  120.             break; 
  121.         default:  
  122.             die("此位置不支持");  
  123.             exit; 
  124.     } 
  125.          
  126.     $dst_img = @imagecreatetruecolor($srcImg_w, $srcImg_h); 
  127.          
  128.     imagecopy ( $dst_img, $srcim, 0, 0, 0, 0, $srcImg_w, $srcImg_h); 
  129.          
  130.     if(!strcmp($markType,"img")) 
  131.     { 
  132.         imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh); 
  133.         imagedestroy($markim); 
  134.     } 
  135.          
  136.     if(!strcmp($markType,"text")) 
  137.     { 
  138.         $rgb = explode(',', $TextColor); 
  139.              
  140.         $color = imagecolorallocate($dst_img, $rgb[0], $rgb[1], $rgb[2]); 
  141.         imagettftext($dst_img, $fontSize, 0, $x, $y, $color, $fontType,$markText); 
  142.     } 
  143.          
  144.     switch ($srcInfo[2])  
  145.     {  
  146.         case 1: 
  147.             imagegif($dst_img, $imgSrc);  
  148.             break;  
  149.         case 2:  
  150.             imagejpeg($dst_img, $imgSrc);  
  151.             break;  
  152.         case 3:  
  153.             imagepng($dst_img, $imgSrc);  
  154.             break; 
  155.         default:  
  156.             die("不支持的水印图片文件类型");  
  157.             exit;  
  158.     } 
  159.          
  160.     imagedestroy($dst_img); 
  161.     imagedestroy($srcim); 
  162. } 

$imgSrc:目标图片,可带相对目录地址,

$markImg:水印图片,可带相对目录地址,支持PNG和GIF两种格式,如水印图片在执行文件mark目录下,可写成:mark/mark.gif

$markText:给图片添加的水印文字

$TextColor:水印文字的字体颜色

$markPos:图片水印添加的位置,取值范围:0~9

0:随机位置,在1~8之间随机选取一个位置

1:顶部居左 2:顶部居中 3:顶部居右 4:左边居中

5:图片中心 6:右边居中 7:底部居左 8:底部居中 9:底部居右

$fontType:具体的字体库,可带相对目录地址

$markType:图片添加水印的方式,img代表以图片方式,text代表以文字方式添加水印

代码注释:

第4~6行:获取目标图片的宽度和高度

第8~22行:根据图片类型调用不同的函数,获得操作图像标识符

GetImageSize函数知识点:GetImageSize不需要安装 GD度就可使用,其返回值数组有四个元素。索引值0是图片高度。索引值1是图片的宽度。索引值2是图片的文件格式,其值1为GIF格式、2为JPEG/JPG格式、3为PNG格式。索引值3为图片的高与宽字符串,height=xxx width=yyy。返回的图片宽度和高度单位都是像素(pixel)

第24~58行:当选择图片方式给目标图片添加水印时,获取水印图片的宽度和高度,通常情况都是网站的logo。如果目标图片比水印图片宽度或者高度小或者水印图片不存在,则跳出这个函数。

return语句知识点:直接return 表示什么都不返回,直接结束这个函数。也可以理解成返回 NULL。

第60~77行:当选择文字方式给目标图片添加水印时,首先设定水印文字的大小,默认我设置为16px,你可以根据需要自行调整字体大小。如果字体文件不存在,跳出函数,最后通过imagettfbbox函数获得此设定格式的文字的虚拟长宽。

imagettfbbox函数知识点:此函数返回一个含有8个单元的数组表示文本外框的四个角,索引值含义:0代表左下角 X 位置,1代表坐下角 Y 位置,2代表右下角 X 位置,3代表右下角 Y 位置,4代表右上角 X 位置,5代表右上角 Y 位置,6代表左上角 X 位置,7代表左上角 Y 位置。此函数同时需要GD 库和FreeType库的支持

max函数返回参数中数值最大的值。

第79~125行:根据设定的图片水印位置计算具体坐标值,你可以根据效果具体细化水印的位置。

第127~129行:新建一个和目标图片大小一致的图片。

注:由于imagecreatetruecolor函数范围的是一个黑色图片,所以如果你的目标图片是透明的,则生成的新图将不会是透明色。

第131~162行:根据图片或者文字方式,最终生成添加了水印的图片。

调用说明:

以函数调用方式调用即可,当然你也可以以类的方式封装,或者你也可以根据需要将此函数进一步细分模块也可以。当然你现在这样用也是没有任何问题的,我已测试过,请放心使用。

其他说明:

由于imagettftext和imagettfbbox函数需要GD库和FreeType库的支持,如果你的运行环境不支持GD库和FreeType库则文字方式就无法实现,你可以用imagestring函数实现给图片添加文字水印,同时设定下text方式下的$logow和$logoh值即可。

imagejpeg函数也可以设置合成的图片质量。

PHP图片加水印函数思路总结:

首先计算目标图片、水印图片以及文字的宽度和高度,在根据具体位置计算最终水印出现的位置信息,即X和Y值。最后合成图片,新的图片就添加了水印。

(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容