diff --git a/application/models/files.php b/application/models/files.php index ee7d3224..99fab011 100644 --- a/application/models/files.php +++ b/application/models/files.php @@ -142,6 +142,73 @@ class files{ } }//文件上传 + /** + * getOne() + * + * 从数据库获取单个文件的信息 + * + * @param int id + * + * return array + */ + static function getOne($db,$id){ + //下载单个附件 + $sql = $db->quoteInto("select a.* from attachments a where a.id=? order by a.ts_created desc",$id); + $att = $db->fetchRow($sql); + if(empty($att['id'])) + { $att['error']=='文件不存在';} + return $att; + } + + + static function getImageType($filename) + { + + //通过mime判断是否是真正的图片 + + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $mime = finfo_file($finfo, $filename); + finfo_close($finfo); + $type = $mime; + if(!in_array($type,array('image/jpeg','image/png'))) + { + return array('error'=>'F'); + }else{ + switch ($type){ + case 'image/jpeg': + $image_create_func = 'imagecreatefromjpeg'; + $image_save_func = 'imagejpeg'; + $new_image_ext='jpg'; + $quality = 100; + break; + case 'image/gif': + $image_create_func = 'imagecreatefromgif'; + $image_save_func = 'imagegif'; + $new_image_ext = 'gif'; + break; + case 'image/png': + $image_create_func = 'imagecreatefrompng'; + $image_save_func = 'imagepng'; + $new_image_ext = 'png'; + $quality = 100; + break; + default: + $image_create_func = 'imagecreatefromjpeg'; + $image_save_func = 'imagejpeg'; + $new_image_ext='jpg'; + $quality = 100; + } + + $downloadcfg = array('creatfunc'=>$image_create_func, + 'savefunc'=>$image_save_func, + 'ext'=>$new_image_ext, + 'mime'=>$mime, + 'quality'=>$quality); + + return $downloadcfg; + } + + }//function getimagetype /**