121 lines
4.2 KiB
PHP
Executable File
121 lines
4.2 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: linfan
|
|
* Date: 2018-12-03
|
|
* Time: 21:39
|
|
*/
|
|
|
|
if (!function_exists('upload_content_image')) {
|
|
|
|
/**
|
|
* Notes:抓取网络图片
|
|
* Created on: 2020/1/14 14:21
|
|
* Created by: dengbw
|
|
* @param $content
|
|
* @return string|string[]|null
|
|
*/
|
|
function fetch_content_image($content, $path = '')
|
|
{
|
|
//解析内容
|
|
$content = preg_replace('/data-.*?["|\'](.*?)["|\']/', '', $content);
|
|
preg_match_all('/\<[img|IMG].*?src=["|\'](.*?)["|\'].*?\>/', $content, $match);
|
|
$ci = &get_instance();
|
|
$ci->load->library('qiniu');
|
|
if ($match[0]) {
|
|
foreach ($match[1] as $key => $val) {
|
|
if (!strstr($val, 'qimg')) {
|
|
$file = $ci->qiniu->fetch($val, getFileName($val, $path));
|
|
if (!$file) {
|
|
$content = str_replace($match[1][$key], "", $content);
|
|
continue;
|
|
} else {
|
|
$content = str_replace($match[1][$key], $file['url'], $content);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
preg_match_all('/url\((.*?)\)/', $content, $match2);
|
|
if ($match2[0]) {
|
|
foreach ($match2[1] as $key => $val) {
|
|
if (!strstr($val, 'qimg')) {
|
|
$file = $ci->qiniu->fetch($val, getFileName($val, $path));
|
|
if (!$file) {
|
|
$content = str_replace($match2[1][$key], "", $content);
|
|
continue;
|
|
} else {
|
|
$content = str_replace($match2[1][$key], '"' . $file['url'] . '"', $content);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $content;
|
|
}
|
|
|
|
function getFileName($file, $path = '')
|
|
{
|
|
$list = ['gif', 'jpg', 'png', 'bmp', 'jpeg']; //定义图片后缀数组
|
|
$pattern = "/" . implode("|", $list) . "/i"; //定义正则表达式
|
|
$patternList = array();
|
|
if (preg_match_all($pattern, $file, $matches)) { //匹配到了结果
|
|
$patternList = $matches[0]; //匹配到的数组
|
|
}
|
|
$ext = $patternList ? $patternList[0] : 'jpg';
|
|
$phoId = md5(uniqid() . mt_rand(0, 10000) . time());
|
|
$oriKey = 'p_' . $phoId . '.' . $ext;
|
|
// 上传到七牛后保存的文件名
|
|
$path = $path ? $path . "/" : '';
|
|
$photo = $path . date('Ym') . "/" . $oriKey;
|
|
return $photo;
|
|
}
|
|
|
|
|
|
function upload_content_image($content)
|
|
{
|
|
//解析内容
|
|
$content = preg_replace('/data-.*?["|\'](.*?)["|\']/', '', $content);
|
|
preg_match_all('/\<[img|IMG].*?src=["|\'](.*?)["|\'].*?\>/', $content, $match);
|
|
|
|
if ($match[0]) {
|
|
$ci = &get_instance();
|
|
$ci->load->library('qiniu');
|
|
|
|
foreach ($match[1] as $key => $val) {
|
|
if (!strstr($val, 'qimg')) {
|
|
$img = @file_get_contents($val);
|
|
$filename = $ci->qiniu->getFileName($val);
|
|
$filename = date('Y/m') . '/' . $filename;
|
|
$file = $ci->qiniu->save($filename, $img);
|
|
|
|
if (!$file) {
|
|
$content = str_replace($match[1][$key], "", $content);
|
|
continue;
|
|
} else {
|
|
$content = str_replace($match[1][$key], $file['url'], $content);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
preg_match_all('/url\((.*?)\)/', $content, $match2);
|
|
if ($match2[0]) {
|
|
foreach ($match2[1] as $key => $val) {
|
|
if (!strstr($val, 'qimg')) {
|
|
$img = @file_get_contents($val);
|
|
$filename = $ci->qiniu->getFileName($val);
|
|
$filename = date('Y/m') . '/' . $filename;
|
|
$file = $ci->qiniu->save($filename, $img);
|
|
|
|
if (!$file) {
|
|
$content = str_replace($match2[1][$key], "", $content);
|
|
continue;
|
|
} else {
|
|
$content = str_replace($match2[1][$key], '"' . $file['url'] . '"', $content);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $content;
|
|
}
|
|
} |