Initial commit
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
namespace Admin\Model;
|
||||
use Think\Model;
|
||||
class SaveRemoteImgModel{
|
||||
//editor字段保存相关操作
|
||||
public function doIt($content){
|
||||
preg_match_all ( "/\<[img|IMG].*?src=\"(.+?)\".*?>/", $content, $img_array );
|
||||
// 时间无限制
|
||||
set_time_limit ( 0 );
|
||||
foreach ( $img_array[0] as $key => $value ) {
|
||||
$matches=array(
|
||||
0=>$value,
|
||||
1=>$img_array[1][$key],
|
||||
);
|
||||
$newimgurl=$this->saveRemoteImg($matches);
|
||||
// 替换原来的图片地址
|
||||
$content = ereg_replace ( $img_array[1][$key], $newimgurl, $content );
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
//远程保存图片
|
||||
public function saveRemoteImg($matches){
|
||||
preg_match("/alt=\"(.*)\"/U", $matches[0],$result);
|
||||
$altName=$result[1]?$result[1]:time();
|
||||
//本地路径
|
||||
$this->TmpPath="./d/image/".date("Ymd")."/";
|
||||
if (! file_exists ($this->TmpPath)) {
|
||||
mkdir($this->TmpPath, 0777, true);
|
||||
}
|
||||
$result=$this->crabImage($matches[1], $this->TmpPath);
|
||||
if(!$result){
|
||||
return $matches[1];
|
||||
}
|
||||
//生成小图
|
||||
$image=new \Think\Image();
|
||||
$image->open($result[save_path]);
|
||||
$image->thumb(200,200);
|
||||
$small_path=$this->TmpPath."small_".$result[fileName];
|
||||
$image->save($small_path);
|
||||
if(!M("file")->where(array("md5"=>md5_file($result['save_path'])))->find()){
|
||||
M("file")->add(array(
|
||||
"pubid"=>$_POST["pubid"],
|
||||
"name"=>$altName,
|
||||
"ext"=>trim($result[ext],"."),
|
||||
"savename"=>$result[fileName],
|
||||
"smallpath"=>trim($small_path,"."),
|
||||
"size"=>filesize($result[save_path]),
|
||||
"md5"=>md5_file($result['save_path']),
|
||||
"sha1"=>sha1_file($result['save_path']),
|
||||
"filepath"=>trim($result[save_path],"."),
|
||||
"create_time"=>time(),
|
||||
));
|
||||
}
|
||||
return trim($result[save_path],".");
|
||||
}
|
||||
/**
|
||||
* PHP将网页上的图片攫取到本地存储
|
||||
* @param $imgUrl 图片url地址
|
||||
* @param string $saveDir 本地存储路径 默认存储在当前路径
|
||||
* @param null $fileName 图片存储到本地的文件名
|
||||
* @return mix
|
||||
*/
|
||||
function crabImage($imgUrl, $saveDir='./', $fileName=null){
|
||||
if(empty($imgUrl)){
|
||||
return false;
|
||||
}
|
||||
//获取图片信息大小
|
||||
$imgSize = getImageSize($imgUrl);
|
||||
if(!in_array($imgSize['mime'],array('image/jpg', 'image/gif', 'image/png', 'image/jpeg'),true)){
|
||||
return false;
|
||||
}
|
||||
//获取后缀名
|
||||
$_mime = explode('/', $imgSize['mime']);
|
||||
$_ext = '.'.end($_mime);
|
||||
if(empty($fileName)){ //生成唯一的文件名
|
||||
$fileName = uniqid(time(),true).$_ext;
|
||||
}
|
||||
//开始攫取
|
||||
ob_start();
|
||||
readfile($imgUrl);
|
||||
$imgInfo = ob_get_contents();
|
||||
ob_end_clean();
|
||||
if(!file_exists($saveDir)){
|
||||
mkdir($saveDir,0777,true);
|
||||
}
|
||||
$fp = fopen($saveDir.$fileName, 'a');
|
||||
$imgLen = strlen($imgInfo); //计算图片源码大小
|
||||
$_inx = 1024; //每次写入1k
|
||||
$_time = ceil($imgLen/$_inx);
|
||||
for($i=0; $i<$_time; $i++){
|
||||
fwrite($fp,substr($imgInfo, $i*$_inx, $_inx));
|
||||
}
|
||||
fclose($fp);
|
||||
return array(
|
||||
"fileName"=>$fileName,
|
||||
'ext'=>$_ext,
|
||||
'save_path'=>$saveDir.$fileName
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user