繁体   English   中英

调整大小并保存图像

[英]Resize and save image

我尝试使用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';
}

我将此作为评论,但认为值得回答:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM