调整大小并保存图像

时间:2020-03-25 14:42:40

标签: php wordpress

我正在尝试使用wp_get_image_editor来调整大小并在WordPress中保存图片,但是没有运气。该图像未保存,并且没有错误。我究竟做错了什么?您将如何处理?

代码

$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';
}

1 个答案:

答案 0 :(得分:1)

我有此评论,但认为值得一试:

文件路径不应是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';
}