Hello. I run bulk convert and get PHP Fatal error
-
I run test on my Localhost [12-Jul-2024 14:23:42 UTC] PHP Fatal error: Palette image not supported by webp in X:\xampp\htdocs\wp-includes\class-wp-image-editor.php on line 592
-
It is necessary to add support for
$truecolor_image = imagecreatetruecolor(imagesx($image), imagesy($image));
so that it supports conversion or highlights all such separate moments in a pass and statistics to see which files it cannot process. The problem is that it stops at 36 percent and logs the errorPHP Fatal error: Palette image not supported by webp
, and then the process just doesn’t start and constantly says “Maybe you have some plugin that is interfering, because I didn’t get a valid json back. Try disabling the debug mode of the site or press ok to try to continue.”Maybe that help:
// Function to convert palette images to true color images
private function convert_to_truecolor($image) {
if (imageistruecolor($image)) {
return $image;
}
$width = imagesx($image);
$height = imagesy($image);
$truecolor_image = imagecreatetruecolor($width, $height);
// Preserve transparency
imagealphablending($truecolor_image, false);
imagesavealpha($truecolor_image, true);
// Copy the palette image to the true color image
imagecopy($truecolor_image, $image, 0, 0, 0, 0, $width, $height);
imagedestroy($image);
return $truecolor_image;
}Modify the handle_upload_prefilter function to use the convert_to_truecolor function before processing the image:
function handle_upload_prefilter($file) {
global $bir_options;
if (substr($file['type'], 0, 5) != 'image') return $file;
if ($bir_options == null) $bir_options = new Bir_options_var();
self::$original_image = null;
self::$original_image_name = null;
self::$uniq_id = null;
// Specifica le dimensioni desiderate per l'immagine ridimensionata
$resize = false;
$quality = 100;
if ($bir_options->resize_active == 1) {
$max_width = $bir_options->max_width;
$max_height = $bir_options->max_height;
$quality = $bir_options->quality;
$resize = true;
} else if ($bir_options->optimize_active == 1 || $bir_options->webp_active == 1) {
$max_width = 5000;
$max_height = 5000;
$quality = $bir_options->quality;
$resize = true;
}
// Percorso completo del file temporaneo
$file_path = $file['tmp_name'];
$ris_filter = apply_filters('op_bir_resize_image_bulk', wp_basename($file_path), 0);
if (is_array($ris_filter) && count($ris_filter) == 2) {
if (array_key_exists('width', $ris_filter) && array_key_exists('height', $ris_filter)) {
$max_width = $ris_filter['width'];
$max_height = $ris_filter['height'];
} else {
$max_width = array_shift($ris_filter);
$max_height = array_shift($ris_filter);
}
if (array_key_exists('quality', $ris_filter) && $ris_filter['quality'] > 0 && $ris_filter['quality'] <= 100) {
$quality = $ris_filter['quality'];
}
} elseif (is_bool($ris_filter)) {
$resize = $ris_filter;
}
if (substr($file['name'], -4) == '.gif' || $file["type"] == 'image/gif') {
$resize = false;
}
if (substr($file['name'], -4) == '.svg' || stripos($file["type"], 'svg') !== false) {
$resize = false;
}
self::$original_image_name = $file['name'];
if ($resize) {
$ext2 = pathinfo($file['tmp_name'], PATHINFO_EXTENSION);
$file_original = str_replace(".".$ext2, '', $file['tmp_name']);
$file_original .= '-original.'.$ext2;
copy($file['tmp_name'], $file_original);
self::$original_image = $file_original;
// Load the image and convert it to true color if necessary
$image = wp_get_image_editor($file_path);
if (!is_wp_error($image)) {
$image_resource = $image->get_image();
$image_resource = $this->convert_to_truecolor($image_resource);
$image->save($file_path, null, $quality);
}
$source_image = wp_get_image_editor($file_path);
if (!is_wp_error($source_image)) {
$source_image->resize($max_width, $max_height);
if ($quality != 100) $source_image->set_quality($quality);
$mime_type = $file["type"];
if ($bir_options->webp_active == 1) {
$mime_type = 'image/webp';
$file["type"] = $mime_type;
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$file['name'] = str_replace('.'.$ext, '', $file['name']);
$file['name'] .= '.webp';
}
$new_image = $source_image->save($file["tmp_name"], $mime_type);
if ($bir_options->webp_active == 1) {
$get_image = imagecreatefromwebp($new_image['path']);
imagewebp($get_image, $new_image['path'], $quality);
}
unlink($file["tmp_name"]);
rename($new_image['path'], $file["tmp_name"]);
$file['size'] = $new_image['filesize'];
}
}
if ($bir_options->rename_active == 1) {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$post_id = (isset($_REQUEST['post'])) ? absint($_REQUEST['post']) : 0;
$file['name'] = Bir_rename_functions::get_name_from_file($bir_options->rename, $file["tmp_name"], 0, $post_id, $file["name"]).'.'.$ext;
self::$uniq_id = Bir_rename_functions::$current_uniq;
}
return $file;
}These changes add support for converting palette images to true color format before any further processing such as resizing or format conversion to avoid the PHP Fatal error: Palette image not supported by webp
Here’s the detailed explanation the issues with Palette image format support for WebP in your plugin’s code: Key Areas in the Code
- Function
check_image_editor
:static function check_image_editor() { // ... (other code parts)$img = wp_get_image_editor($path_check_img_url); if (is_wp_error($img)) { self::$global_bulk_image_resizer_check_editor = $img->get_error_message(); if ($img->get_error_code() == 'image_no_editor') { if ( (! extension_loaded( 'gd' ) || ! function_exists( 'gd_info' )) && ( ! extension_loaded( 'imagick' ) || ! class_exists( 'Imagick', false ) || ! class_exists( 'ImagickPixel', false )) ) { self::$global_bulk_image_resizer_check_editor = __('There seems to be no php library for manipulating images.', 'bulk-image-resizer'); } } return self::$global_bulk_image_resizer_check_editor; } else { self::$global_bulk_image_resizer_check_editor = ''; return ''; }}
- Function
op_get_image_info
:static function op_get_image_info($path_img) { // ... (other code parts) $img = wp_get_image_editor($path_img); if (is_wp_error($img)) { return $result; } $result['is_valid'] = true; // ... (other code parts) }
- Additional code for handling truecolor images:
php if ($img->get_error_code() == 'image_palette_not_supported') { $truecolor_image = imagecreatetruecolor(imagesx($image), imagesy($image)); imagecopy($truecolor_image, $image, 0, 0, 0, 0, imagesx($image), imagesy($image)); $image = $truecolor_image; }
Comments for Developers
- Checking and handling image editor support:
In thecheck_image_editor
function, image editor support is verified. It’s crucial to add checks and error handling for images in Palette format for WebP. This can be achieved by converting such images to the truecolor format before further processing. - Image processing in
op_get_image_info
function:
Theop_get_image_info
function processes images usingwp_get_image_editor
. In case of errors with theimage_palette_not_supported
code, logic should be added to convert images to truecolor format to avoid errors during processing. - General handling of WebP images:
Add checks and conversion to truecolor format in all functions where WebP image processing occurs to prevent issues related to Palette image support.
These changes will help mitigate errors related to processing Palette format images in WebP by converting them to truecolor format where necessary.
Thank you. I’ll need some time to analyze all the work you’ve done and integrate it into a new version, but it looks great!
Above all I have to find an image that generates the same error (maybe a webp image with a transparent background?) to do the tests.Anyway thanks again, I’ll let you know when I have the new release ready.
Hi. Thanks for respond. I can find this version of files what do that issues maybe need make debug version what file make error.
To address the issue of
Palette image not supported by webp
in your code, you can add a function to convert palette images to true color format, as suggested. Here’s how you can integrate the proposed changes into your code:- Add the
convert_to_truecolor
function for image conversion:
private function convert_to_truecolor($image) { if (imageistruecolor($image)) { return $image; } $width = imagesx($image); $height = imagesy($image); $truecolor_image = imagecreatetruecolor($width, $height); // Preserve transparency imagealphablending($truecolor_image, false); imagesavealpha($truecolor_image, true); // Copy the palette image to the true color image imagecopy($truecolor_image, $image, 0, 0, 0, 0, $width, $height); imagedestroy($image); return $truecolor_image; }
- Modify the
handle_upload_prefilter
function to use this function before image processing:
function handle_upload_prefilter($file) { global $bir_options; if (substr($file['type'], 0, 5) != 'image') return $file; if ($bir_options == null) $bir_options = new Bir_options_var(); self::$original_image = null; self::$original_image_name = null; self::$uniq_id = null; // Specify desired dimensions for resized image $resize = false; $quality = 100; if ($bir_options->resize_active == 1) { $max_width = $bir_options->max_width; $max_height = $bir_options->max_height; $quality = $bir_options->quality; $resize = true; } else if ($bir_options->optimize_active == 1 || $bir_options->webp_active == 1) { $max_width = 5000; $max_height = 5000; $quality = $bir_options->quality; $resize = true; } // Full path of the temporary file $file_path = $file['tmp_name']; $ris_filter = apply_filters('op_bir_resize_image_bulk', wp_basename($file_path), 0); if (is_array($ris_filter) && count($ris_filter) == 2) { if (array_key_exists('width', $ris_filter) && array_key_exists('height', $ris_filter)) { $max_width = $ris_filter['width']; $max_height = $ris_filter['height']; } else { $max_width = array_shift($ris_filter); $max_height = array_shift($ris_filter); } if (array_key_exists('quality', $ris_filter) && $ris_filter['quality'] > 0 && $ris_filter['quality'] <= 100) { $quality = $ris_filter['quality']; } } elseif (is_bool($ris_filter)) { $resize = $ris_filter; } if (substr($file['name'], -4) == '.gif' || $file["type"] == 'image/gif') { $resize = false; } if (substr($file['name'], -4) == '.svg' || stripos($file["type"], 'svg') !== false) { $resize = false; } self::$original_image_name = $file['name']; if ($resize) { $ext2 = pathinfo($file['tmp_name'], PATHINFO_EXTENSION); $file_original = str_replace(".".$ext2, '', $file['tmp_name']); $file_original .= '-original.'.$ext2; copy($file['tmp_name'], $file_original); self::$original_image = $file_original; // Load the image and convert it to true color if necessary $image = wp_get_image_editor($file_path); if (!is_wp_error($image)) { $image_resource = $image->get_image(); $image_resource = $this->convert_to_truecolor($image_resource); $image->save($file_path, null, $quality); } $source_image = wp_get_image_editor($file_path); if (!is_wp_error($source_image)) { $source_image->resize($max_width, $max_height); if ($quality != 100) $source_image->set_quality($quality); $mime_type = $file["type"]; if ($bir_options->webp_active == 1) { $mime_type = 'image/webp'; $file["type"] = $mime_type; $ext = pathinfo($file['name'], PATHINFO_EXTENSION); $file['name'] = str_replace('.'.$ext, '', $file['name']); $file['name'] .= '.webp'; } $new_image = $source_image->save($file["tmp_name"], $mime_type); if ($bir_options->webp_active == 1) { $get_image = imagecreatefromwebp($new_image['path']); imagewebp($get_image, $new_image['path'], $quality); } unlink($file["tmp_name"]); rename($new_image['path'], $file["tmp_name"]); $file['size'] = $new_image['filesize']; } } if ($bir_options->rename_active == 1) { $ext = pathinfo($file['name'], PATHINFO_EXTENSION); $post_id = (isset($_REQUEST['post'])) ? absint($_REQUEST['post']) : 0; $file['name'] = Bir_rename_functions::get_name_from_file($bir_options->rename, $file["tmp_name"], 0, $post_id, $file["name"]).'.'.$ext; self::$uniq_id = Bir_rename_functions::$current_uniq; } return $file; }
This code checks if the image is palette-based and converts it to true color format before further processing, including resizing or converting to WebP format. This should help prevent the
Palette image not supported by webp
error.Hi @power2009 i tried your code that you provided and well i’m stuck with this error
// Load the image and convert it to true color if necessary
$image = wp_get_image_editor($file_path);
if (!is_wp_error($image)) {
? ? ? ? ? ? ? ? $image_resource = $image->get_image(); —-> this is giving me error
$image_resource = $this->convert_to_truecolor($image_resource);
$image->save($file_path, null, $quality);
? ? ? ? ? ? }
probably because it doesn’t exist. can you help me get this right since i don’t know code that well.
if there are other things i need to do before that please let me know @power2009
@power2009 thanks again for the valuable suggestions.
I believe these images that generate this error are rare. Maybe you can try replacing the images that generate the error manually and see if the plugin starts working correctly again.@justinbitter do you also have the same type of error?
@giuliopanda yes i get the same type of error when trying to bulkresize the images
“Maybe you can try replacing the images that generate the error manually and see if the plugin starts working correctly again.” – you must make “if” for solution skip TrueColors file:
My solution in another plugin i used:
private function create_webp(string $filename, string $mime_type, string $filename_webp): bool { if (!file_exists($filename) || file_exists($filename_webp)) { return false; } $pluswebp_settings = get_option('pluswebp'); @set_time_limit(60); wp_raise_memory_limit('pluswebp'); $src = null; $img = null; $ret = false; try { switch ($mime_type) { case 'image/jpeg': $src = imagecreatefromjpeg($filename); break; case 'image/png': $src = imagecreatefrompng($filename); break; case 'image/bmp': if (function_exists('imagecreatefrombmp')) { $src = imagecreatefrombmp($filename); } break; case 'image/gif': $src = imagecreatefromgif($filename); break; default: return false; // Unsupported MIME type } if (!$src) { return false; // Failed to create source image } $img = imagecreatetruecolor(imagesx($src), imagesy($src)); if (!$img) { imagedestroy($src); return false; // Failed to create destination image } switch ($mime_type) { case 'image/jpeg': $bgcolor = imagecolorallocate($img, 255, 255, 255); imagefill($img, 0, 0, $bgcolor); imagealphablending($img, true); break; case 'image/png': imagealphablending($img, false); imagesavealpha($img, true); break; case 'image/gif': $bgcolor = imagecolorallocatealpha($img, 0, 0, 0, 127); imagefill($img, 0, 0, $bgcolor); imagecolortransparent($img, $bgcolor); break; } imagecopy($img, $src, 0, 0, 0, 0, imagesx($src), imagesy($src)); $ret = imagewebp($img, $filename_webp, $pluswebp_settings['quality']); } catch (Exception $e) { // Handle exception if needed } finally { if ($src) { imagedestroy($src); } if ($img) { imagedestroy($img); } } return $ret; }
And maybe you help that
- Function
- The topic ‘Hello. I run bulk convert and get PHP Fatal error’ is closed to new replies.