westdc-zf1/application/module/Files/Operation/FileOperate.php

126 lines
3.1 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Files\Operation;
use Files\Thumbnail;
class FileOperate implements \Files\Event\FileEvent
{
private $db; //传入PDO对象
private $DefaultFetchMode = \PDO::FETCH_BOTH; //默认检索模式防止出现sdtClass错误
private $config; //全局配置
public $def_allow_ext = array(
'gif', 'jpg', 'jpeg', 'png', 'bmp',
'flv','swf', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb',
'pdf','doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'
);
function __construct($db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
}
public function checkExt(\Zend_EventManager_Event $e){
$file = $e->getParam('file');
$ext_arr = $this->def_allow_ext;
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$filetype = finfo_file($finfo, $file['tmp_name']) ; //文件mime类型
finfo_close($finfo);
foreach($ext_arr as $v)
{
if(strpos($filetype,$v) !== false)
{
return true;
}
}
return array("error"=>"您上传的文件不在允许的范围内".$filetype." was not in ".join(",",$ext_arr));
}//checkParam
public function checkSize(\Zend_EventManager_Event $e){
$file = $e->getParam('file');
$file_size = $file['size'];
if(!isset($conf->maxsize) && !empty($conf->maxsize))
{
$max_size = (int)$conf->maxsize;
}else{
$max_size = 30*1024*1024;
}
if($file_size > $max_size)
{
return array("error"=>'上传的文件超过了允许的文件大小');
}
return true;
}//checkSize
public function makeThumb(\Zend_EventManager_Event $e)
{
$file_path = $e->getParam('file_path');
$conf = $e->getParam('conf');
$save_path = $conf->upload;
$dbsave = $e->getParam('db_path');
$file_ext = $e->getParam('file_ext');
$new_file_basename = $e->getParam('new_file_basename');
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$filetype = finfo_file($finfo, $file_path) ; //文件mime类型
finfo_close($finfo);
$thumb_conf = new \Zend_Config_Ini("../application/conf/thumb.ini", "thumb");
if($thumb_conf->makethumb != "on")
{
return array();
}
$allowmime = $thumb_conf->imagemime;
if(empty($allowmime))
{
$allowmime = array('image/jpeg','image/png','image/gif');
}else{
$allowmime = explode(",",$allowmime);
}
if(!in_array($filetype,$allowmime))
{
return array();
}
$thumb_size = explode(",",$thumb_conf->thumbsize);
$thumbnail = array();
foreach($thumb_size as $v)
{
$v = (int)$v;
$thumb = new Thumbnail($v, $v);
$image = file_get_contents($file_path);
$thumb->loadData($image, $filetype);
$thumbfile = $save_path.$dbsave.$new_file_basename.'_'.$v. '.' .$file_ext;
$thumbnail[$v] = array(
'size' => $v,
'ext' => $file_ext,
'file' => $thumbfile,
'url' => $dbsave.$new_file_basename.'_'.$v. '.' .$file_ext ,
);
$thumb->buildThumb($thumbfile);
}
return $thumbnail;
}//makeThumb
}