I am generating avatars from an image file upload. When running the function, WordPress does create the first image image-32x32.gif
but fails on the remainder of the loop. I suspect it's due to concurrency.
public function process_uploaded_file($file)
{
$editor = wp_get_image_editor($file);
$sizes = [32, 64, 128, 256, 512];
foreach ($sizes as $size) {
$result = $editor->resize($size, $size, true); //LINE 217
if (!is_wp_error($result)) {
$editor->save($editor->generate_filename());
}
}
}
What am I doing wrong?
Fatal error: Uncaught Error: Call to undefined method WP_Error::resize() in /Users/.../class-gf-avatar-field.php on line 217
( ! ) Error: Call to undefined method WP_Error::resize() in /.../class-gf-avatar-field.php on line 217
Update 09-11-2019 I added a few checks, and create a new editor each time.
foreach ($sizes as $size) {
$editor = wp_get_image_editor($file);
if (!is_wp_error($editor)) {
$result = $editor->resize($size, $size, true);
if (!is_wp_error($result)) {
$editor->save($editor->generate_filename());
}
}
}
$editor
actually change throughout the loop? (Try avar_dump()
of it to error log) Given the error message, I would presume your$editor = wp_get_image_editor(..
line is already returning aWP_Error
.