Make WordPress Core

Opened 11 years ago

Last modified 4 weeks ago

#35959 new defect (bug)

Functon 'wp_generate_attachment_metadata(...)' silently swallowing errors from 'wp_get_image_editor(...)'.

Reported by: maratbn Owned by:
Priority: normal Milestone: Awaiting Review
Component: Media Version: 3.5
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

So the function wp_generate_attachment_metadata(...) in wp-admin/includes/image.php is silently ignoring / swallowing errors from the function wp_get_image_editor(...).

The logic is here:
https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/image.php?rev=36429#L123

This ignoring of these particular errors prevents user notification in the UI when image thumbnails could not be regenerated due to error "No editor could be selected" that is returned from the function wp_get_image_editor(...) when the PHP environment on the server is lacking graphics support. So the thumbnails are not getting regenerated, and because of this bug, there is notification to the user as to that fact or as to why.

I looked this up in the history, and noticed that this bug is present since this revision:
https://core.trac.wordpress.org/changeset/22192

What's odd is that this revision does add logic to relay these errors in its 1st change (to the function wp_crop_image(...)) in trunk/wp-admin/includes/image.php, but not in the 3rd change in that file (to the function wp_generate_attachment_metadata(...)). Not sure why the error is not being relayed there as well.

Attachments (2)

2016-02-25--01--patch--relay-editor-error.txt​ (866 bytes ) - added by maratbn 11 years ago.
Attaching patch to mitigate this problem.
35959.patch​ (2.2 KB ) - added by grapplerulrich 9 years ago.

Download all attachments as: .zip

Change History (9)

#1 @maratbn
11 years ago

Err meant to say "...So the thumbnails are not getting regenerated, and because of this bug, there is NO notification to the user as to that fact or as to why..."

Whoever has the permissions, please insert "no" into that part of the description.

#2 @obenland
11 years ago

  • Version trunk → 3.5

This ticket was mentioned in ​Slack in #core-media by desrosj. ​View the logs.


9 years ago

#4 @desrosj
9 years ago

  • Keywords has-patch needs-refresh needs-testing added

@maratbn Thanks for the ticket! Are you able to recreate the patch as a .diff or .patch file? See ​https://make.wordpress.org/core/handbook/contribute/#patches for help.

There are also a few minor style issues in the patch. if statements should always contain braces, and inline control structures are prohibited (See ​https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#brace-style).

#5 @grapplerulrich
9 years ago

  • Keywords needs-refresh removed

The attached patch should fix the issue.

I realized this problem with wp media regenerate 892 would not work on PDF. When running wp eval "var_dump(wp_generate_attachment_metadata(892,get_attached_file(892)));" I would get an empty array which did not make sense.

With the patch when I run wp eval "var_dump(wp_generate_attachment_metadata(892,get_attached_file(892)));" I get

object(WP_Error)#11341 (2) {
  ["errors"]=>
  array(1) {
    ["image_no_editor"]=>
    array(1) {
      [0]=>
      string(41) "No editor could be selected."
    }
  }
  ["error_data"]=>
  array(0) {
  }
}

Or for wp media regenerate 892

Found 1 image to regenerate.
Warning: No editor could be selected.
Error: No images regenerated.

The patch also makes sure that $uploaded = $editor->save( $preview_file, 'image/jpeg' ); does not fail silently too. Though I have not been able to test this.

#6 @r1k0
9 months ago

  • Keywords needs-refresh added; needs-testing removed

This [ patch] doesn't apply cleanly against current trunk (7.0-alpha-61215-src). Removing need-testing and adding needs-refresh until we have a refreshed patch to test.

Error:

patching file src/wp-admin/includes/image.php
Hunk #1 FAILED at 250.
Version 0, edited 9 months ago by r1k0 (next)

This ticket was mentioned in ​PR #14108 on ​WordPress/wordpress-develop by ​@irozum.


4 weeks ago
#7

  • Keywords has-unit-tests added; needs-refresh removed

wp_generate_attachment_metadata() calls wp_get_image_editor() at three points while building an attachment's sub-sizes (wp_create_image_subsizes() and _wp_make_subsizes() in src/wp-admin/includes/image.php). When the server has no usable image editor (e.g. missing GD/Imagick), wp_get_image_editor() returns a WP_Error, and all three call sites handle it by silently returning the metadata as-is — no sub-sizes, and nothing recorded to say why. Callers like wp media regenerate or the Media Library have no way to tell the user that thumbnail generation failed versus simply not being needed.

This adds the attachment's error key to the returned metadata array at each of the three early-return points, containing the editor's error message (e.g. "No editor could be selected."). The metadata return type stays an array (not a WP_Error), since every core caller (wp_update_attachment_metadata(), the REST API, XML-RPC, custom headers/backgrounds, etc.) passes the return value straight through without checking is_wp_error() — changing the return type would break all of them. This mirrors the fix already noted but never implemented: wp_create_image_subsizes()'s own docblock already states "If there was an error, it is added to the returned image metadata array," and _wp_make_subsizes()'s docblock already promised "an array of errors encountered while resizing" (now corrected to describe the single error key this surfaces).

Added a test (test_wp_generate_attachment_metadata_surfaces_image_editor_error) that forces wp_image_editors to return no editors and confirms the resulting metadata contains the error key instead of silently reporting empty sizes.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.

---

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

Note: See TracTickets for help on using tickets.