欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

我自己亲自做的 把 百度编辑器 ueditor 的截图 由 png 改成 jpg 有大用 有大大用 有大大大用

/sites/all/modules/ueditor/includes/action_upload.inc 里面的函数,, 这个文件里面的代码没有变动

主要看看 Uploader 的构造函数

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

/**
* 得到上传文件所对应的各个参数,数组结构
* array(
*     "state" => "",          //上传状态,上传成功时必须返回"SUCCESS"
*     "url" => "",            //返回的地址
*     "title" => "",          //新文件名
*     "original" => "",       //原始文件名
*     "type" => ""            //文件类型
*     "size" => "",           //文件大小
* )
*/

/* 返回数据 */
return json_encode($up->getFileInfo());


/sites/all/modules/ueditor/includes/Uploader.class.inc  里面

public function __construct($fileField, $config, $type = "upload") {
   
$this->fileField = $fileField;
   
$this->config = $config;
   
$this->type = $type;

   
if ($type == "remote") {
       
$this->saveRemote();
   }
else if($type == "base64") {
       
$this->upBase64();
   }
else {
           //经测试,截图执行的是 upFile() 函数
       
$this->upFile();
   }
}

/sites/all/modules/ueditor/includes/Uploader.class.inc  里面,,,事实上我仅仅修改的 upFiles() 函数

 private function upFile() {
       
$file = $this->file = $_FILES[$this->fileField];
       
if (!$file) {
           
$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
           
return;
       }
       
if ($this->file['error']) {
           
$this->stateInfo = $this->getStateInfo($file['error']);
           
return;
       }
else if (!file_exists($file['tmp_name'])) {
           
$this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");
           
return;
       }
else if (!is_uploaded_file($file['tmp_name'])) {
           
$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");
           
return;
       }

       
$this->oriName = $file['name'];
       
$this->fileSize = $file['size'];
       
$this->fileType = $this->getFileExt();
       
$this->fullName = $this->getFullName();
       
$this->savePath = ueditor_get_savepath($this->fullName);
       
$this->filePath = $this->getFilePath();
       
$this->fileName = $this->getFileName();
       
$dirname = dirname($this->filePath);

       
//检查文件大小是否超出限制
       
if (!$this->checkSize()) {
           
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
           
return;
       }

       
//检查是否不允许的文件格式
       
if (!$this->checkType()) {
           
$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
           
return;
       }

       
//创建目录失败
       
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
           
$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
           
return;
       }
else if (!is_writeable($dirname)) {
           
$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
           
return;
       }
       
// Load the files contents
       
$imagedata = file_get_contents($file["tmp_name"]);

       
//截图的话,下一行的代码是 生成 png
       
$filedata = $this->savefileToDrupal($imagedata, $this->savePath);
           
       
//我仅仅增加的是下面的这一行代码 save_png_to_jpg() 是我自定义的函数  下面是自己改的, 把 png 变成 jpg,,,,因为jpg 比 png 小,,,文件名一样,就是后缀名由png 变成 jpg
       
$filedata = $this->save_png_to_jpg($this->savePath,$filedata);
//var_dump($this);exit;

       //移动文件
       
if (is_object($filedata)) { //移动成功
         
$success = FALSE;
         
$use_watermark = variable_get('ueditor_watermark',0);
         
if($use_watermark && $this->config['type'] == 'image'){
           
$success = $this->addWatermark($this->filePath);
         }
else{
           
$success = TRUE;
         }

         
if($success === TRUE){
           
$this->stateInfo = $this->stateMap[0];
         }
else{
           
$this->stateInfo = $this->getStateInfo($success);
         }
       }
else { //移动失败
         
$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
       }
   }


  // save_png_to_jpg()是我自己写的,代码如下

    /**
    * 把 png 转换为 jpg,,,还有返回 $filedata 的内容,,,设置 $this 的内容,还有 保存到数据库
    */
   
private function save_png_to_jpg($path,$filedata)
   {
       
$old_path = $path;
       
$new_path = preg_replace('/\.png([^.]*)$/', '.jpg$1', $old_path);
       
$public_path = variable_get('file_public_path', 'sites/default/files');
       
$real_path = $public_path.'/'.$path;
       
// 源PNG文件路径
       
$pngFile = $real_path;
// 输出JPG文件路径
       
$jpgFile = preg_replace('/\.png([^.]*)$/', '.jpg$1', $pngFile);

// 图片质量 (0-100)
       
$quality = 80;

// 从PNG创建图像资源
       
$image = imagecreatefrompng($pngFile);

// 创建白色背景(处理PNG透明部分)
       
$width = imagesx($image);
       
$height = imagesy($image);
       
$bg = imagecreatetruecolor($width, $height);
       
$white = imagecolorallocate($bg, 255, 255, 255);
       
imagefill($bg, 0, 0, $white);

// 将PNG图像复制到白色背景上
       
imagecopy($bg, $image, 0, 0, 0, 0, $width, $height);

// 保存为JPG格式
       
imagejpeg($bg, $jpgFile, $quality);

// 释放内存
       
imagedestroy($image);
       
imagedestroy($bg);

       
//删除png文件
       
unlink($pngFile);
       
//echo "PNG图片已成功转换为JPG格式!";



       
$filedata->uri = preg_replace('/\.png([^.]*)$/', '.jpg$1', $filedata->uri);
       
$filedata->filename = preg_replace('/\.png([^.]*)$/', '.jpg$1', $filedata->filename);
       
$filedata->filemime = "image/jpeg";
       
$filedata->filesize = filesize($jpgFile);;
       
$filedata->origname = preg_replace('/\.png([^.]*)$/', '.jpg$1', $filedata->origname);

       
$this->url= "/".$jpgFile;
       
$this->title= $filedata->filename;
       
$this->original= "image.png";
       
$this->type= ".jpg";
       
$this->size= $filedata->filesize;
       
$this->savePath= preg_replace('/\.png([^.]*)$/', '.jpg$1', $this->savePath);
       
$this->fullName=preg_replace('/\.png([^.]*)$/', '.jpg$1', $this->fullName);
       
$this->fileName=preg_replace('/\.png([^.]*)$/', '.jpg$1', $this->fileName);
       
$this->fileType="image/jpeg";
       
$fid= $filedata->fid;
       
$uri = 'public://'.$new_path;
       
$filesize = $filedata->filesize;
       
$timestamp = time();
       
$filename = $filedata->filename;
       
//数据库也要改下吧
       
$sql ="UPDATE file_managed set filename='{$filename}',uri='{$uri}',filemime='image/jpeg',filesize='{$filesize}',status=1,timestamp='{$timestamp}',origname='{$filename}' where fid='{$fid}' ";
       db_query(
$sql);
       
return $filedata;

   }


经过上面的操作,,,,,截图就由默认的 png 转换成了 jpg

普通分类: