Code前端首页关于Code前端联系我们

WordPress 代码:在媒体库中本地化自动复制粘贴到文章中的外部链接图像

terry 2年前 (2023-09-24) 阅读数 58 #后端开发

通过 Wp 发布文章时,文章往往是从其他地方复制的。文章中通常也会有图片,那么如何自动粘贴图片呢?本地化上传到媒体库怎么样? /**
* 钩子函数:上传显示服务器外域名post_content到服务器并替换url
*
* @param Int $post_id
* @param Object $post
*
*/
function ecp_save_post($post_id, $post) {
// WordPress 全局变量 wpdb class
global $wpdb; ///仅在单击时 update 执行以下操作
if( $post ->post_status == 'publish') {
// 匹配 , src 并将其存储在数组 $matches,
$p = ' /post_content , $matches);

if ($num) {
// 用于构建url的本地上传路径信息(数组)
$wp_upload_dir = wp_upload_dir();

//脚本没有执行时间限制
SET_TIME_LIMIT (0);

// 构造curl,参数configure $ch=curl_init();
curl_setopt($ch,curlopt_header,false);
curl_setopt($ch, crlopt_returntransfer, true); ? // 最多跳 20 次
curl_setopt($ch, CURLOPT_MAXREDIRS,20);
// 连接前的最大等待时间
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

$ecp_ERVEROST' =] foreach ($matches[1] if $src) {
if (isset($src) && strpos($src, $ecp_options) === false) {
// 如果图片的域名不在本站 DomainName

// 检查 src URL 是否有扩展名。如果没有,请重命名该文件
// 注意。如果 URL 有扩展名但格式为 webp,则返回 file_info 数组 ['ext ' =>'','type' =>'']
$file_info = wp_check_filetype(basename($src), null );
if ($file_info['ext'] == false) {
// 无扩展名且 webp 格式的图像将作为无扩展名文件处理
date_default_timezone_set('PRC');
$file_name = date( 'YmdHis-').dechex(mt_rand(100000, 999999) ).'.tmp'; { // 带扩展名的图片会被重命名,以避免与本地文件名冲突
$file_name = dechex(mt_rand(100000, 999999 )) '-'。 basename($src);
}

//拍摄图片并写入本地文件
curl_setopt($ch, CURLOPT_URL, $src);
$file_path = $ wp_upload'_dir[' . '/'。 $file_name;
$img = fopen($file_path, 'wb');
//curl 写入 $img
curl_setopt($ch, CURLOPT_FILE, $img);
$img_data = curl_exec($ch);
fclose($img);

if (file_exists($file_path) && filesize($file_path) > 0) {
// 将扩展名更改为 tmp 和 webp 转换图像 jpeg 文件并将其重命名为
$t = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$arr =explode('/', $t);
// 它没有扩展名 url 或处理图像带有网络扩展名
if (pathinfo($file_path, PATHINFO_EXTENSION) == 'tmp') {
$file_path = ecp_handle_ext($file_path, $arr[1], $wp_path_d '], $file_name, 'tmp' ) ;
} elseif (pathinfo($file_path, PATHINFO_EXTENSION) == 'webp') {
$file_path = ecp_handle_ext($file_path, $arr_up ['path'], $file_name, 'webp');
}

//替换帖子内容中的src
$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_metadataid,$ttatatafitee;
// 直接调用WordPress函数将元数据信息写入数据库
$ss = wp_update_attachment_metadata($attach_id, $attach_data);
}
}
}
}} $ ch) ;

//更新posts数据表字段post_content
$wpdb->update( $wpdb->posts, array('post_content' => $post->post_content), array(' ID' = > $post- >ID));
}
}
}

/**
* 处理不带扩展名的图像:转换格式或更改扩展名
* @param string $file local图片绝对路径
* @param string $type 图片 mimetype
* @param string $file_dir 图片本地文件夹
* @param string $file_name 图片名称
* @param string $ext 图片扩展名
* @return string 已处理本地图像的绝对路径
*/
function ecp_handle_ext($file, $type, $file_directory, $file_name, $ext) {
switch $ext) {
case 'tmp':
if (rename($file, str_replace('tmp', $type, $file))) {
if ('webp' == $type) {
// 将 webp 图像转换为 jpeg
return ecp_image_convert('webp', 'jpeg', $file_directory . '/' . str_replace('tmp', $type, $file_name));
}
returns $file_dir 。 '/'。 str_replace('tmp', $type, $file_name);
}
case 'webp':
if ('webp' == $type) {
// 将 webp 图像转换为 jpeg -格式
return ecp_image_convert('webp', 'jpeg', $file);
} else {
if (rename($file, str_replace('webp', $type, $file))) {
返回 $file_dir 。 '/'。 str_replace('webp', $type, $file_name);
}
}
默认:
return $file;
}
}
}
} / } * 图片格式转换,目前只能将webp转换为jpeg。 *
* @param string $from
* @param string $to
* @param string $image 图像
的本地绝对路径 * @return string 转换图像
的绝对路径 */
function ecp_image_convert($from=' webp', $to='jpeg', $image) {
// 加载 WebP 文件
$im = imagecreatefromwebp($image);
// 转换100% 质量 jpeg 并删除原始 webp 文件
if (imagejpeg($im, str_replace('webp', 'jpeg) ', $image), 100)) {
尝试 {
unlink( $ image);
} catch (异常 $e) {
$error_msg = sprintf('删除本地文件 %s 时出错: %s', $image,
$e->getMessage());
error_log($error_msg);
}
}
imagedestroy($im);

return str_replace('webp', 'jpeg', $image);
}
}

/**
* 撰写图片发布参数 * * @param string $filename
* @param string $url
* @return array 图片post参数数组
*/
function ecp_get_attachment_post($filename, $urlfi) = wp_check_filetype($filename, null);
return array(
'guid' => $url,
'post_type' => '附件',
'post_mime_type' => type'],
'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
'post_content' => '',
'post_status' => '继承'
) ;
}

//钩子,在发布/草稿/预览时触发
add_action('save_post', 'ecp_save_post', 'ecp_save_0,,);只需将上面的代码添加到主题的functions.php文件中,或者您可以直接安装Easy复制粘贴插件。

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门