wp_generate_attachment_metadata does not generate images with different sizes
-
Hi,
I am using the following codes to add an image to the media library:
function add_new_image_to_post(string $image_filename, int $parent_post_id) { // Create the new attachment post for the image // Check the type of file. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype(basename($image_filename), null); output_debug_text(sprintf('File type of the image: %s.', $filetype['type'])); // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); output_debug_text(sprintf('Upload dir: %s.' . PHP_EOL, $wp_upload_dir['url'])); // Prepare an array of post data for the attachment. $attachment = array( 'guid' => $wp_upload_dir['url'] . '/' . basename( $image_filename ), 'post_mime_type' => $filetype['type'], 'post_title' => '', 'post_content' => '', 'post_status' => 'inherit' ); // Insert the attachment. $attach_id = wp_insert_attachment( $attachment, $image_filename, $parent_post_id ); if ($attach_id) { output_debug_text(sprintf('Insert attachment successfully. Image file: %s. Parent post ID: %s. Attach ID: %s.' . PHP_EOL, $image_filename, $parent_post_id, $attach_id)); // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. require_once( ABSPATH . 'wp-admin/includes/image.php' ); // Generate the metadata for the attachment, and update the database record. $attach_data = wp_generate_attachment_metadata( $attach_id, $image_filename ); if ($attach_data) { output_debug_text('Generate attachment meta data successfully: ' . print_r($attach_data, true) . PHP_EOL); $result = wp_update_attachment_metadata( $attach_id, $attach_data ); // $result will be false, but the attachemnt is added successfully and all subimages are created // so we will ignore the case and always return $attach_id // Ref: https://www.ads-software.com/support/topic/wp_update_attachment_metadata-always-return-false/#post-17486245 $result = true; if ($result) { output_debug_text(sprintf('Set attachment metadata successfully.' . PHP_EOL)); return $attach_id; } else output_debug_text(sprintf('Fail to set attachment metadata.\n')); } else output_debug_text(sprintf('Fail to generate attachment metdata.\n')); } else output_debug_text(sprintf('Fail to insert image attachment.\n')); return 0; }
I dump the $attach_data. Then under a Windows 10 + Wampserver64, wp_generate_attachment_metadata will generate images with different sizes, as below:
Array ( [width] => 1190 [height] => 680 [file] => C:/wamp64/www/blogs/wp-content/uploads/2024/03/test11.jpg [filesize] => 122205 [sizes] => Array ( [medium] => Array ( [file] => test11-300x171.jpg [width] => 300 [height] => 171 [mime-type] => image/jpeg [filesize] => 14252 ) [large] => Array ( [file] => test11-1024x585.jpg [width] => 1024 [height] => 585 [mime-type] => image/jpeg [filesize] => 94116 ) [thumbnail] => Array ( [file] => test11-150x150.jpg [width] => 150 [height] => 150 [mime-type] => image/jpeg [filesize] => 7239 ) [medium_large] => Array ( [file] => test11-768x439.jpg [width] => 768 [height] => 439 [mime-type] => image/jpeg [filesize] => 59274 ) ) [image_meta] => Array ( [aperture] => 0 [credit] => [camera] => [caption] => [created_timestamp] => 0 [copyright] => [focal_length] => 0 [iso] => 0 [shutter_speed] => 0 [title] => [orientation] => 0 [keywords] => Array ( ) ) )
But in a CentOS 7 server(Linux), [filesize] is zero, and no sizes are generated:
Array ( [width] => 1190 [height] => 680 [file] => https://www.sample.com/blogs/wp-content/uploads/2024/03/outlook-pst-repair-2.jpg [filesize] => 0 [sizes] => Array ( ) )
I check the post for the image, it looks like this:
https://pasteboard.co/FgoTtHcaWH3W.png
It seems the meta data for the attachment is not generated successfully. But there is still an array returned. Why? How to diagnose the issue? I check https://developer.www.ads-software.com/reference/functions/wp_generate_attachment_metadata/ but cannot find how to obtain an error code if the function fails.
- The topic ‘wp_generate_attachment_metadata does not generate images with different sizes’ is closed to new replies.