首页 > 编程技术 > php

百度UEDITOR编辑器上传图片添加加水印修改

发布时间:2016-11-25 17:18

最后在把网站编辑器由以前的fck改成UEDITOR编辑器了,但是发现UEDITOR编辑器上传图片是自动提取的,但是图片没有水印功能,下面小编和各位一起来看看。


UEditor编辑器没有上传图片加水印的功能,需要进行二次开发,本例是在PHPCMS系统中对百度编辑器进行二次开发,添加上传图片加水印功能。


首先打开UEditor编辑器文件目录的php文件夹,打开Uploader.class.php,把PHPCMS添加水印的方法复制过来,加到这个类所有成员方法最后面,然后进行修改如下:


//图片加水印
public function watermark($source, $target = '', $w_pos = '', $w_img = '', $w_text = '99danji',$w_font = 8, $w_color = '#ff0000') {
    $this->w_img = 'watermark.png';
    $this->w_pos = 9;
    $this->w_minwidth = 400;
    $this->w_minheight = 200;
    $this->w_quality = 80;
    $this->w_pct = 85;
 
    $w_pos = $w_pos ? $w_pos : $this->w_pos;
    $w_img = $w_img ? $w_img : $this->w_img;
    //if(!$this->watermark_enable || !$this->check($source)) return false;
    if(!$target) $target = $source;
    //$w_img = PHPCMS_PATH.$w_img;
    //define('WWW_PATH', dirname(dirname(dirname(__FILE__)));
    $w_img = '../../../images/water/'.$w_img;
    $source_info = getimagesize($source);
    $source_w    = $source_info[0];
    $source_h    = $source_info[1];
    //if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false;
    switch($source_info[2]) {
        case 1 :
            $source_img = imagecreatefromgif($source);
            break;
        case 2 :
            $source_img = imagecreatefromjpeg($source);
            break;
        case 3 :
            $source_img = imagecreatefrompng($source);
            break;
        default :
            return false;
    }
    if(!empty($w_img) && file_exists($w_img)) {
        $ifwaterimage = 1;
        $water_info   = getimagesize($w_img);
        $width        = $water_info[0];
        $height       = $water_info[1];
        switch($water_info[2]) {
            case 1 :
                $water_img = imagecreatefromgif($w_img);
                break;
            case 2 :
                $water_img = imagecreatefromjpeg($w_img);
                break;
            case 3 :
                $water_img = imagecreatefrompng($w_img);
                break;
            default :
                return;
        }
    } else {       
        $ifwaterimage = 0;
        $temp = imagettfbbox(ceil($w_font*2.5), 0, PC_PATH.'libs/data/font/elephant.ttf', $w_text);
        $width = $temp[2] - $temp[6];
        $height = $temp[3] - $temp[7];
        unset($temp);
    }
    switch($w_pos) {
        case 1:
            $wx = 5;
            $wy = 5;
            break;
        case 2:
            $wx = ($source_w - $width) / 2;
            $wy = 0;
            break;
        case 3:
            $wx = $source_w - $width;
            $wy = 0;
            break;
        case 4:
            $wx = 0;
            $wy = ($source_h - $height) / 2;
            break;
        case 5:
            $wx = ($source_w - $width) / 2;
            $wy = ($source_h - $height) / 2;
            break;
        case 6:
            $wx = $source_w - $width;
            $wy = ($source_h - $height) / 2;
            break;
        case 7:
            $wx = 0;
            $wy = $source_h - $height;
            break;
        case 8:
            $wx = ($source_w - $width) / 2;
            $wy = $source_h - $height;
            break;
        case 9:
            $wx = $source_w - $width;
            $wy = $source_h - $height;
            break;
        case 10:
            $wx = rand(0,($source_w - $width));
            $wy = rand(0,($source_h - $height));
            break;             
        default:
            $wx = rand(0,($source_w - $width));
            $wy = rand(0,($source_h - $height));
            break;
    }
    if($ifwaterimage) {
        if($water_info[2] == 3) {
            imagecopy($source_img, $water_img, $wx, $wy, 0, 0, $width, $height);
        } else {
            imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
        }
    } else {
        if(!empty($w_color) && (strlen($w_color)==7)) {
            $r = hexdec(substr($w_color,1,2));
            $g = hexdec(substr($w_color,3,2));
            $b = hexdec(substr($w_color,5));
        } else {
            return;
        }
        imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
    }
   
    switch($source_info[2]) {
        case 1 :
            imagegif($source_img, $target);
            break;
        case 2 :
            imagejpeg($source_img, $target, $this->w_quality);
            break;
        case 3 :
            imagepng($source_img, $target);
            break;
        default :
            return;
    }
 
    if(isset($water_info)) {
        unset($water_info);
    }
    if(isset($water_img)) {
        imagedestroy($water_img);
    }
    unset($source_info);
    imagedestroy($source_img);
    return true;
}
 
public function check($image) {
    return extension_loaded('gd') && preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) && file_exists($image) && function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
}
对比我修改的部分,www.111cn.net由于phpcms水印可以在后台管理设置,phpcms自带的水印方法通过读取配置文件获取路径,和读取数据库设置获取参数设置,那么这些地方需要手动进行设置。

对了,在upFile方法还要添加一段函数:

if ($this->watermark) {
    $this->watermark($this->filePath,$this->filePath);
}

然后打开UEditor百度编辑器php目录下的action_upload.php文件,加上是否添加水印的参数:

/* 上传配置 */
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
    case 'uploadimage':
        $config = array(
            "pathFormat" => $CONFIG['imagePathFormat'],
            "maxSize" => $CONFIG['imageMaxSize'],
            "allowFiles" => $CONFIG['imageAllowFiles']
        );
        $fieldName = $CONFIG['imageFieldName'];
        $watermark = true;
        break;
然后在后面还有一句要改成:

 

/* 生成上传实例对象并完成上传 */
$up = new Uploader($fieldName, $config, $base64, $watermark);

这样就大功告成了,本文主要是提供思路和参考。

phpexcel是php中专业来操作excel表格的一个php插件了,下文我们就来看看phpexcel读取excel表格时间的例子,希望下文能够帮助到各位。


编辑通过excel表格修改了大批的产品价格和促销时间,让我们技术批量导入到线上数据库。

这样对于我们来说是一件在简单不过的事情了,保护phpexcel导表利器,瞬间解决问题。

可是,进入数据库一看:蒙了,导入的时间格式有问题,展示的不是时间,是数字,郁闷中。

然后通过php输出,果然不是时间的格式。

百度一遍发现,phpexcel里面提供了这样的方法getFormattedValue()来读出时间的,将getValue()换成
getFormattedValue();

$abc = $currentSheet->getCell ( ‘A’ . $currentRow )->getFormattedValue ();

这样就可以顺利的读出excel表里的时间,重新更新数据库,OK。

下面看个例子

error_reporting(E_ALL);
date_default_timezone_set('Asia/shanghai');
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
$inputFileName = '6081076641077444758.xls';
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($inputFileName);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数

$tempArray = array();
for($j=2;$j<=$highestRow;$j++){
   for($k='A';$k<=$highestColumn;$k++){
    if($k=='M'||$k=='O') //M列和O列是时间
  $tempArray[] = excelTime($objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue());
 else
  $tempArray[] = $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
 }
 print_r($tempArray);
 unset($tempArray);
}


function excelTime($date, $time = false) {
 if(function_exists('GregorianToJD')){
  if (is_numeric( $date )) {
  $jd = GregorianToJD( 1, 1, 1970 );
  $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
  $date = explode( '/', $gregorian );
  $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
  ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
  ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
  . ($time ? " 00:00:00" : '');
  return $date_str;
  }
 }else{
  $date=$date>25568?$date+1:25569;
  /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
  $ofs=(70 * 365 + 17+2) * 86400;
  $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
 }
  return $date;
}

在php中对于简单的数据排序为我们提供了sort、asort、rsort、krsort、ksort函数了,下文我来给各位简单的介绍sort、asort、rsort、krsort、ksort函数的区别了。

在php中自带了大量了数组排序函数,下面我们一一来介绍一下关于php数组排序的用法吧。

 

sort() 函数用于对数组单元从低到高进行排序。
rsort() 函数用于对数组单元从高到低进行排序。
asort() 函数用于对数组单元从低到高进行排序并保持索引关系。
arsort() 函数用于对数组单元从高到低进行排序并保持索引关系。
ksort() 函数用于对数组单元按照键名从低到高进行排序。
krsort() 函数用于对数组单元按照键名从高到低进行排序。

sort()

PHP sort() 函数用于对数组单元从低到高进行排序,如果成功则返回 TRUE,失败则返回 FALSE。
注意:本函数会为排序的数组中的单元赋予新的键名,这将删除原有的键名而不仅是重新排序。
语法:
bool sort( array &array [, int sort_flags] )
可选参数 sort_flags 用于改变排序的行为:
sort_flags 取值 说明
SORT_REGULAR 正常比较单元
SORT_NUMERIC 单元被作为数字来比较
SORT_STRING 单元被作为字符串来比较
SORT_LOCALE_STRING 根据当前的区域(locale)设置来把单元当作字符串比较

例子:

$arr = array("b", "a", "c");
sort($arr);
print_r($arr);
?>

Array ( [0] => a [1] => b [2] => c )

在本例中, $arr 数组单元被按照字母顺序排序,而数组单元经过排序后,键值重新分配。

rsort()
PHP rsort() 函数行为与 sort() 相反,对数组单元进行由高到低排序,请参考 sort() 函数使用。

asort()
PHP asort() 函数用于对数组单元从低到高进行排序并保持索引关系,如果成功则返回 TRUE,失败则返回 FALSE。
语法:
bool asort( array &array [, int sort_flags] )
可选参数 sort_flags 用以改变排序的行为,具体参见 sort()。

例子:

$arr = array("b", "a", "c");
asort($arr);
print_r($arr);
?>

运行该例子输出:

Array ( [1] => a [0] => b [2] => c )

arsort()

PHP arsort() 函数行为与 asort() 相反,对数组单元进行由高到低排序并保持索引关系,请参考 asort() 函数使用。

ksort()

PHP ksort() 函数用于对数组单元按照键名从低到高进行排序,如果成功则返回 TRUE,失败则返回 FALSE。
本函数会保留原来的键名,因此常用于关联数组。

语法:
bool ksort( array &array [, int sort_flags] )

可选参数 sort_flags 用以改变排序的行为,具体参见 sort()。
例子:

$arr = array("b"=>18, "a"=>20, "c"=>25);
ksort($arr);
print_r($arr);
?>

运行该例子输出:

Array ( [a] => 20 [b] => 18 => 25 )

krsort()
PHP krsort() 函数行为与 ksort() 相反,对数组单元按照键名进行由高到低排序,请参考 ksort() 函数使用

fopen函数对于文件的读定操作是专业的并且速度是非常的快了,有时我们没用用到数据库只用到了txt文件了,下面我们来看看fopen从100万条记录的文本文件取出重复数最多的前10条的例子。

100万条记录的文本文件,取出重复数最多的前10条。

 

示例文本:

 

098
123
234
789
……
234
678
654
123

$fp = fopen('文件', 'r');

while($buf = fgets($fp)) {   $res[$buf]++;

}

fclose($fp);

arsort($res);

$res = array_keys(array_slice($res, 0, 10));

print_r($res);
$a = file('文件');

$res = array_count_values($a);

arsort($res);

$res = array_keys(array_slice($res, 0, 10));

print_r($res);

无限级分类功能就是用到无限级树了,在此我们一起来看看小编找到一个非常不错并且简单的php 实现无限级树型示例供各位学习参考。


php 实现无限级树型可以实现再主菜单里面有哪些二级才能。也可以开发留言板的时候获取父评论下的子评论!这个就是实现无限级树型的功能了!那么我们话就不多说了。请看看下面数据库的结构

CREATE TABLE `emlog_hudhf` (

  `id` mediumint(10) unsigned NOT NULL AUTO_INCREMENT,

  `sslog` varchar(64) NOT NULL,

  `sid` varchar(64) NOT NULL,

  `s` varchar(64) NOT NULL,

  `z` varchar(64) NOT NULL,

  `n` text NOT NULL,

  `puser` varchar(64) NOT NULL,

  `buser` varchar(64) NOT NULL,

  PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=128 DEFAULT CHARSET=utf8 AUTO_INCREMENT=128 ;

-- 

-- 导出表中的数据 `emlog_hudhf`

--

INSERT INTO `emlog_hudhf` VALUES (46, '5', '0', '1397957923', '0', '评论了说说啦!', '1', '1');

INSERT INTO `emlog_hudhf` VALUES (82, '13', '0', '1398523128', '0', '什么和什么', '1', '1');

INSERT INTO `emlog_hudhf` VALUES (48, '6', '0', '1397958405', '0', '你看评论啊', '257', '257');

INSERT INTO `emlog_hudhf` VALUES (70, '8', '69', '1397983195', '0', '我又回复你了', '1', '1');

INSERT INTO `emlog_hudhf` VALUES (117, '8', '66', '1398561020', '0', '我也回复你', '1', '497');

INSERT INTO `emlog_hudhf` VALUES (66, '8', '0', '1397982730', '0', '我要评论你XXXXXXXXHappySky', '497', '1');

INSERT INTO `emlog_hudhf` VALUES (68, '7', '0', '1397982850', '0', '测试评论功能 可以恢复不', '1', '497');

INSERT INTO `emlog_hudhf` VALUES (69, '8', '65', '1397982864', '0', '回复“什么呀”~~~~', '497', '1');

INSERT INTO `emlog_hudhf` VALUES (120, '8', '0', '1398561155', '0', '我也评论一下自己的说说', '1', '1');

INSERT INTO `emlog_hudhf` VALUES (121, '8', '0', '1398561255', '0', '说说', '1', '1');

INSERT INTO `emlog_hudhf` VALUES (124, '8', '120', '1398561490', '0', '很好吧', '1', '1');

INSERT INTO `emlog_hudhf` VALUES (123, '8', '66', '1398561328', '0', '去你妈妈个B', '1', '497');


 
这个是数据库的图片结构
 
QQ图片20140427155330.jpg 
然后思路是这样的:sid为0的表示这个是父评论不是0就是子评论了!然后怎么知道那个子评论是属于那个父评论的呢?再sid不为0的话那么就根据这个sid的值去查询等于这个值得id看看下面的列子

INSERT INTO `emlog_hudhf` VALUES (120, '8', '0', '1398561155', '0', '我也评论一下自己的说说', '1', '1');

INSERT INTO `emlog_hudhf` VALUES (124, '8', '120', '1398561490', '0', '很好吧', '1', '1');


第二条的sid等于第一条的id 那么 这个二条的就是第一条的子评论了!
 
然后要怎么输出呢。当然首先要获取你要输出数据的sql语句
 

//这一段是意思呢。sslog 等于 这个变量whidi的值得数据 然后按最新插入来显示出来

$gg = mysql_query("SELECT * FROM ".DB_PREFIX."hudhf WHERE sslog = '".$whidi."' order by id desc");

$data = array();

while($rs = mysql_fetch_assoc($gg)){  

          $data[] = $rs;//这个是把他转换成为数组后给另一个函数来输出 

}

getTree($data, 0,$whidi,$whiuser);

}

 

echo getTree($data, 0); 

 

function getTree($data, $pId)

{

$html = '';foreach($data as $k => $v)

{

   if($v['cate_ParentId'] == $pId)

   {         //父亲找到儿子

    $html .= "


 
然后这个就是结合html美化后的结果了


QQ图片20140427160240.jpg

标签:[!--infotagslink--]

您可能感兴趣的文章: