保存图片函数

if (!function_exists('save_image')) {

/**
* 保存图片
* @param string $img_url 网络图片地址
* @param string $save_dir 图片保存目录
* @return string 返回路径
*/
function save_image($img_url, $save_dir = '/')
{
if (!$img_url) {
return false;
}
$save_dir = trim($save_dir, "/");
$imgExt = pathinfo($img_url, PATHINFO_EXTENSION);
// 是否是本站图片
if (strpos($img_url, IMG_URL) !== false) {
// 是否是临时文件
if (strpos($img_url, 'temp') === false) {
return str_replace(IMG_URL, "", $img_url);
}
$new_path = create_image_path($save_dir, $imgExt);
$old_path = str_replace(IMG_URL, ATTACHMENT_PATH, $img_url);
if (!file_exists($old_path)) {
return false;
}
rename($old_path, IMG_PATH . $new_path);
return str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $new_path;
} else {
// 保存远程图片
$new_path = save_remote_image($img_url, $save_dir);
}
return $new_path;
}
}

if (!function_exists('create_image_path')) {

/**
* 创建图片存储目录
* @param string $save_dir 存储目录
* @param string $image_ext 图片后缀
* @param string $image_root 图片存储根目录路径
* @return string 返回文件目录
*/
function create_image_path($save_dir = "", $image_ext = "", $image_root = IMG_PATH)
{
$image_dir = date("/Ymd/");
if ($image_dir) {
$image_dir = ($save_dir ? "/" : '') . $save_dir . $image_dir;
}
// 未指定后缀默认使用JPG
if (!$image_ext) {
$image_ext = "jpg";
}
$image_path = $image_root . $image_dir;
if (!is_dir($image_path)) {
// 创建目录并赋予权限
mkdir($image_path, 0777, true);
}
$file_name = substr(md5(time() . rand(0, 999999)), 8, 16) . rand(100, 999) . ".{$image_ext}";
$file_path = $image_dir . $file_name;
return $file_path;
}
}

if (!function_exists('save_remote_image')) {

/**
* 保存网络图片到本地
* @param string $img_url 网络图片地址
* @param string $save_dir 保存目录
* @return bool|string 图片路径
*/
function save_remote_image($img_url, $save_dir = '/')
{
$content = file_get_contents($img_url);
if (!$content) {
return false;
}
if ($content{0} . $content{1} == "\xff\xd8") {
$image_ext = 'jpg';
} elseif ($content{0} . $content{1} . $content{2} == "\x47\x49\x46") {
$image_ext = 'gif';
} elseif ($content{0} . $content{1} . $content{2} == "\x89\x50\x4e") {
$image_ext = 'png';
} else {
// 不是有效图片
return false;
}
$save_path = create_image_path($save_dir, $image_ext);
return file_put_contents(IMG_PATH . $save_path, $content) ? str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $save_path : false;
}
}

if (!function_exists('save_image_content')) {

/**
* 富文本信息处理
* @param string $content 富文本内容
* @param bool $title 标题
* @param string $path 图片存储路径
* @return bool|int 返回结果
*/
function save_image_content(&$content, $title = false, $path = 'article')
{
// 图片处理
preg_match_all("/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match);
if ($match[1]) {
foreach ($match[1] as $id => $val) {
$save_image = save_image($val, $path);
if ($save_image) {
$content = str_replace($val, "[IMG_URL]" . $save_image, $content);
}
}
}
// 视频处理
preg_match_all("/<embed .*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i", str_ireplace("\\", "", $content), $match2);
if ($match2[1]) {
foreach ($match2[1] as $vo) {
$save_video = save_image($vo, $path);
if ($save_video) {
$content = str_replace($vo, "[IMG_URL]" . str_replace(ATTACHMENT_PATH, "", IMG_PATH) . $save_video, $content);
}
}
}
// 提示标签替换
if ((strpos($content, 'alt=\"\"') !== false) && $title) {
$content = str_replace('alt=\"\"', 'alt=\"' . $title . '\"', $content);
}
return true;
}
}


相关推荐

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3

  • Thinkphp5.1路径常量

    1 配置文件位置根目录/config/template.php2 配置文件内容&lt;?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reser

  • Thinkphp5.0路径常量

    1 配置文件位置根目录/application/模块名/config.php2 配置文件内容&lt;?php//配置文件return [ // 后台视图输出字符串内容替换 'view_replace_str' =&gt; [ '__PUBLIC__' =&gt; '/', '__STATIC__' =&gt; '/static', '__CONSOLE__' =&gt; '/static/console', '__CONSOLE_CSS__' =&gt; '/static/console/css', '__CONSOLE_IMAGES__' =&gt; '/static/console/ima

  • wp站点防止别人进行DDOS攻击

    1 简介wp站点防止别人进行DDOS攻击。2 配置位置位置:根目录/wp-config.php3 配置内容在【根目录/wp-config.php】文件的开头添加如下代码:if(strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false){ $protocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; if(!in_array($protocol, ['HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3'], true)){ $protocol = 'HTTP/1.0'; } hea

  • 只读属性

    1 只读属性简介只读属性的声明方式类似于普通属性,但需要使用 readonly 关键字。2 只读属性例子class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this-&gt;x = $x; $this-&gt;y = $y; }}$point = new Point(3.5, 2.8);echo $point-&gt;x; // 输出: 3.5echo $point-&gt;y; // 输出: 2.8// 下面的尝