php - 调整大小并保存图像
问题描述
我试图在 WordPress 中调整大小并保存图像,但wp_get_image_editor
没有成功。图像没有被保存,我没有得到任何错误。我究竟做错了什么?你会如何处理它?
代码
$original = 'http://dgli.local.com/wp-content/uploads/2020/03/IMG_7686.jpg';
if(!file_exists($original)) {
return;
}
$editor = wp_get_image_editor($original, array());
$result = $editor->resize(300, 300, true);
if(!is_wp_error($result)) {
$editor->save($editor->generate_filename());
echo 'success';
} else {
echo 'error';
}
解决方案
我将此作为评论,但认为值得回答:
文件路径不应该是 URL,它应该是文件的服务器路径。
// Get the upload directory.
$upload_dir = wp_get_upload_dir();
// Get the base directory.
$path = $upload_dir['basedir'];
// Append the directory to your file name.
$original = $path . '/2020/03/IMG_7686.jpg';
$editor = wp_get_image_editor($original, array());
$result = $editor->resize(300, 300, true);
if(!is_wp_error($result)) {
$editor->save($editor->generate_filename());
echo 'success';
} else {
echo 'error';
}
推荐阅读
- wordpress - 整个网站给出 404,因为无法打开 .htaccess 并且权限被拒绝
- javascript - 边距在 Safari 中不起作用:绝对 div
- typescript - 我有一个 Typescript NPM 模块,如何防止用户从错误的路径自动导入?
- python - 标头信息与数组不匹配。删除字符参数
- python - 如何使用 SQL Server 外部脚本的输出变量?
- networking - 如何从 OpenStreetMap 中提取街道图或网络?
- angular - Angular项目在同一个项目下指定多个主机名
- c++ - x86 处理器可以调用多少个子程序?
- javascript - 为什么查询选择器单击 div 但未触发任何操作?
- android - 有什么方法可以在 API Response 中获取 ArrayBuffer 吗?