Error: Call to undefined method WP_Error::set_quality
-
Hey,
I’m getting the following error when I click the crop button.
PHP Fatal error: Uncaught Error: Call to undefined method WP_Error::set_quality() in /wp-content/plugins/acf-image-crop-add-on/acf-image-crop-v5.php:526 Stack trace: #0 /wp-content/plugins/acf-image-crop-add-on/acf-image-crop-v5.php(502): acf_field_image_crop->generate_cropped_image('2756', '0', '1920', '221', '656', '1200', '270', true, 'medium') ... thrown in /Users/tom.benyon/Projects/WP_DEV_ENV/wp-content/plugins/acf-image-crop-add-on/acf-image-crop-v5.php on line 526
It’s referring to this code block and the last line here is throwing the error:
function generate_cropped_image($id, $x1, $x2, $y1, $y2, $targetW, $targetH, $saveToMediaLibrary, $previewSize){//$id, $x1, $x2, $y$, $y2, $targetW, $targetH){ require_once ABSPATH . "/wp-admin/includes/file.php"; require_once ABSPATH . "/wp-admin/includes/image.php"; // Create the variable that will hold the new image data $imageData = array(); // Fetch media library info $mediaDir = wp_upload_dir(); // Get original image info $originalImageData = wp_get_attachment_metadata($id); // Get image editor from original image path to crop the image $image = wp_get_image_editor( $mediaDir['basedir'] . '/' . $originalImageData['file'] ); // Set quality $image->set_quality( apply_filters('acf-image-crop/image-quality', 100) );
This means your error handling that follows this line is not working.
Because of that the front end gets hung in a state where cancel and Crop buttons are deactivated as a 500 error is getting returned from the AJAX response.
The suggested code below fixes two issues. Firstly it ensure your error handling still triggers if the $image is not grabbed correctly. Secondly it fixes an issue for users like me who have their images stored on a CDN. The way you were grabbing the image didn’t cater for this.
function generate_cropped_image($id, $x1, $x2, $y1, $y2, $targetW, $targetH, $saveToMediaLibrary, $previewSize){//$id, $x1, $x2, $y$, $y2, $targetW, $targetH){ require_once ABSPATH . "/wp-admin/includes/file.php"; require_once ABSPATH . "/wp-admin/includes/image.php"; // Create the variable that will hold the new image data $imageData = array(); // Fetch media library info $mediaDir = wp_upload_dir(); // Get original image info $originalImageData = wp_get_attachment_metadata($id); // Get image editor from original image path to crop the image $image = wp_get_image_editor( wp_get_attachment_image_src($id)[0] ); if (method_exists($image, 'set_quality')) { // Set quality $image->set_quality(apply_filters('acf-image-crop/image-quality', 100)); } else { $image = new WP_Error( '500', "Could not edit selected image. Does this image exist on the server?"); }
This does not fix everything but this was all the time I could justify investing in this.
The remaining issue is that although your code completes without error now, the image stored is simply a black rectangle.
For context I am using the AWS S3 Offload plugin for my image storage.
Thanks,
Tom
- The topic ‘Error: Call to undefined method WP_Error::set_quality’ is closed to new replies.