免插件实现wordpress外链图片本地化
我们在运营WordPress博客的时候,有的时候需要会使用外链引入一些图片,但是这样也有个弊端,那就是如果该图片外链失效了,图片也就没了,那么来我们看看wordpress外链图片本地化的方法吧,今天我来分享给大家一段代码,可以直接完成图片外链本地化!。
首先我们打开主题目录下的function.php文件,然后把下面的代码复制保存进去,一定要注意格式的规范性,加进去之后我们再次发布文章就会自动完成本地化
以下代码亲测有用,其他平台找了好几个要么语法错误,粘贴就报错,要么就是不全,下面的代码大家可以自行测试下
注意:保存本地指的不单单是服务器,如果您配置了wordpress的阿里云/腾讯云对象储存插件,那么对象储存就称之为“本地”。
//自动本地化外链图片7y5.cn
function ecp_save_post($post_id, $post) {
global $wpdb;
if($post->post_status == 'publish') {
$p = '/<img.*[\s]src=[\"|\'](.*)[\"|\'].*>/iU';
$num = preg_match_all($p, $post->post_content, $matches);
if ($num) {
$wp_upload_dir = wp_upload_dir();
set_time_limit(0);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS,20);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$ecp_options = $_SERVER['HTTP_HOST'];
foreach ($matches[1] as $src) {
if (isset($src) && strpos($src, $ecp_options) === false) {
$file_info = wp_check_filetype(basename($src), null);
if ($file_info['ext'] == false) {
date_default_timezone_set('PRC');
$file_name = date('YmdHis-').dechex(mt_rand(100000, 999999)).'.tmp';
} else {
$file_name = dechex(mt_rand(100000, 999999)) . '-' . basename($src);
}
curl_setopt($ch, CURLOPT_URL, $src);
$file_path = $wp_upload_dir['path'] . '/' . $file_name;
$img = fopen($file_path, 'wb');
curl_setopt($ch, CURLOPT_FILE, $img);
$img_data = curl_exec($ch);
fclose($img);
if (file_exists($file_path) && filesize($file_path) > 0) {
$t = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$arr = explode('/', $t);
if (pathinfo($file_path, PATHINFO_EXTENSION) == 'tmp') {
$file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir['path'], $file_name, 'tmp');
} elseif (pathinfo($file_path, PATHINFO_EXTENSION) == 'webp') {
$file_path = ecp_handle_ext($file_path, $arr[1], $wp_upload_dir['path'], $file_name, 'webp');
}
$post->post_content = str_replace($src, $wp_upload_dir['url'] . '/' . basename($file_path), $post->post_content);
$attachment = ecp_get_attachment_post(basename($file_path), $wp_upload_dir['url'] . '/' . basename($file_path));
$attach_id = wp_insert_attachment($attachment, ltrim($wp_upload_dir['subdir'] . '/' . basename($file_path), '/'), 0);
$attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
$ss = wp_update_attachment_metadata($attach_id, $attach_data);
}
}
}
curl_close($ch);
$wpdb->update( $wpdb->posts, array('post_content' => $post->post_content), array('ID' => $post->ID));
}
}
}
function ecp_handle_ext($file, $type, $file_dir, $file_name, $ext) {
switch ($ext) {
case 'tmp':
if (rename($file, str_replace('tmp', $type, $file))) {
if ('webp' == $type) {
return ecp_image_convert('webp', 'jpeg', $file_dir . '/' . str_replace('tmp', $type, $file_name));
}
return $file_dir . '/' . str_replace('tmp', $type, $file_name);
}
case 'webp':
if ('webp' == $type) {
return ecp_image_convert('webp', 'jpeg', $file);
} else {
if (rename($file, str_replace('webp', $type, $file))) {
return $file_dir . '/' . str_replace('webp', $type, $file_name);
}
}
default:
return $file;
}
}
function ecp_image_convert($from='webp', $to='jpeg', $image) {
$im = imagecreatefromwebp($image);
if (imagejpeg($im, str_replace('webp', 'jpeg', $image), 100)) {
try {
unlink($image);
} catch (Exception $e) {
$error_msg = sprintf('Error removing local file %s: %s', $image,
$e->getMessage());
error_log($error_msg);
}
}
imagedestroy($im);
return str_replace('webp', 'jpeg', $image);
}
function ecp_get_attachment_post($filename, $url) {
$file_info = wp_check_filetype($filename, null);
return array(
'guid' => $url,
'post_type' => 'attachement',
'post_mime_type' => $file_info['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => 'inherit'
);
}
add_action('save_post', 'ecp_save_post', 120, 2);
粘贴完成之后保存一下即可,后面再发布带图片外链的文章即可自动保存本地
阅读剩余
THE END