ueditor抓取远程图片php版,ueditor远程图片本地化的实现

/**

* 拉取远程图片

* @return mixed

*/

private function saveRemote()

{

$imgUrl = htmlspecialchars($this->fileField);

$imgUrl = str_replace("&", "&", $imgUrl);

//http开头验证

if (strpos($imgUrl, "http") !== 0) {

$this->stateInfo = $this->getStateInfo("ERROR_HTTP_LINK");

return;

}

preg_match('/(^https*://[^:/]+)/', $imgUrl, $matches);

$host_with_protocol = count($matches) > 1 ? $matches[1] : '';

// 判断是否是合法 url

if (!filter_var($host_with_protocol, FILTER_VALIDATE_URL)) {

$this->stateInfo = $this->getStateInfo("INVALID_URL");

return;

}

preg_match('/^https*://(.+)/', $host_with_protocol, $matches);

$host_without_protocol = count($matches) > 1 ? $matches[1] : '';

// 此时提取出来的可能是 ip 也有可能是域名,先获取 ip

$ip = gethostbyname($host_without_protocol);

// 判断是否是私有 ip

if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {

$this->stateInfo = $this->getStateInfo("INVALID_IP");

return;

}

//获取请求头并检测死链

$heads = get_headers($imgUrl, 1);

if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {

$this->stateInfo = $this->getStateInfo("ERROR_DEAD_LINK");

return;

}

//格式验证(扩展名验证和Content-Type验证)

$fileType = strtolower(strrchr($imgUrl, '.'));

//13sai 20170712  秀米网链接接如下http://img.xiumi.us/xmi/ua/h4qG/i/b8f2af6986e8dba51615a9d85cc82f3b-sz_1952250.JPG?x-oss-process=style/xm ,我们完善下

$fileType = (strpos($fileType, '?') > 0)? strtolower(substr($fileType,0,strpos($fileType,'?'))) : strtolower($fileType);

//echo $fileType;die();

if (!in_array($fileType, $this->config['allowFiles']) || !isset($heads['Content-Type']) || !stristr($heads['Content-Type'], "image")) {            $this->stateInfo = $this->getStateInfo("ERROR_HTTP_CONTENTTYPE");

return;

}

//打开输出缓冲区并获取远程图片

ob_start();        $context = stream_context_create(

array('http' => array(                'follow_location' => false // don't follow redirects

))

);

readfile($imgUrl, false, $context);

$img = ob_get_contents();

ob_end_clean();

//13sai 20170712  此处正则有问题,修改如下

//preg_match("//[.]?[^./]*$/", $imgUrl, $m);

preg_match("//[A-za-z0-9-]+.".$fileType."/", strtolower($imgUrl), $m);

//var_dump($m);die();

$this->oriName = $m ? ltrim($m[0],'/'):"";

//$this->oriName = $m ? $m[1]:"";

//echo $this->oriName;

//die();

$this->fileSize = strlen($img);

$this->fileType = $this->getFileExt();

$this->fullName = $this->getFullName();

$this->filePath = $this->getFilePath();

$this->fileName = $this->getFileName();

$dirname = dirname($this->filePath);

//检查文件大小是否超出限制

if (!$this->checkSize()) {

$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");

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;

}

//移动文件

if (!(file_put_contents($this->filePath, $img) && file_exists($this->filePath))) { //移动失败

$this->stateInfo = $this->getStateInfo("ERROR_WRITE_CONTENT");

} else { //移动成功

$this->stateInfo = $this->stateMap[0];

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值