Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#44609 closed defect (bug) (invalid)

Different metadata and sizes coming when uploaded via media library and wp_insert_attachment()

Reported by: dipakbbsr's profile dipakbbsr Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9.7
Component: Media Keywords:
Focuses: Cc:

Description

My frontend requires a specific size for the Image Custom Field for a Custom Taxonomy. When Term is created at backend and image is added via Media Library, the size comes perfectly.

But when Term and Term image is added from a custom form ( Tried both custom PHP coding https://rudrastyh.com/wordpress/how-to-add-images-to-media-library-from-uploaded-files-programmatically.html and ACF Pro image field ) the size is coming different.

I tried checking the metadata for two Attachment Posts and their thumbnail sizes seems to be different.

I am adding the serialised meta data here, 1st one is when added from admin, 2nd one is when added from custom form.

a:5:{s:5:"width";s:3:"696";s:6:"height";s:3:"400";s:4:"file";s:25:"2018/01/bathbodyworks.jpg";s:5:"sizes";a:5:{s:9:"thumbnail";a:4:{s:4:"file";s:25:"bathbodyworks-150x150.jpg";s:5:"width";s:3:"150";s:6:"height";s:3:"150";s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:25:"bathbodyworks-300x172.jpg";s:5:"width";s:3:"300";s:6:"height";s:3:"172";s:9:"mime-type";s:10:"image/jpeg";}s:20:"wpcoupon_small_thumb";a:4:{s:4:"file";s:25:"bathbodyworks-200x115.jpg";s:5:"width";s:3:"200";s:6:"height";s:3:"115";s:9:"mime-type";s:10:"image/jpeg";}s:21:"wpcoupon_medium-thumb";a:4:{s:4:"file";s:25:"bathbodyworks-480x276.jpg";s:5:"width";s:3:"480";s:6:"height";s:3:"276";s:9:"mime-type";s:10:"image/jpeg";}s:20:"wpcoupon_blog_medium";a:4:{s:4:"file";s:25:"bathbodyworks-620x300.jpg";s:5:"width";s:3:"620";s:6:"height";s:3:"300";s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:11:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";}}
a:5:{s:5:"width";i:696;s:6:"height";i:400;s:4:"file";s:27:"2018/07/bath-body-works.jpg";s:5:"sizes";a:7:{s:9:"thumbnail";a:4:{s:4:"file";s:27:"bath-body-works-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";}s:6:"medium";a:4:{s:4:"file";s:27:"bath-body-works-300x300.jpg";s:5:"width";i:300;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}s:12:"medium_large";a:4:{s:4:"file";s:27:"bath-body-works-768x441.jpg";s:5:"width";i:768;s:6:"height";i:441;s:9:"mime-type";s:10:"image/jpeg";}s:5:"large";a:4:{s:4:"file";s:29:"bath-body-works-1024x1024.jpg";s:5:"width";i:1024;s:6:"height";i:1024;s:9:"mime-type";s:10:"image/jpeg";}s:20:"wpcoupon_small_thumb";a:4:{s:4:"file";s:27:"bath-body-works-200x115.jpg";s:5:"width";i:200;s:6:"height";i:115;s:9:"mime-type";s:10:"image/jpeg";}s:21:"wpcoupon_medium-thumb";a:4:{s:4:"file";s:27:"bath-body-works-480x480.jpg";s:5:"width";i:480;s:6:"height";i:276;s:9:"mime-type";s:10:"image/jpeg";}s:20:"wpcoupon_blog_medium";a:4:{s:4:"file";s:27:"bath-body-works-620x300.jpg";s:5:"width";i:620;s:6:"height";i:300;s:9:"mime-type";s:10:"image/jpeg";}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}

While the 1st one creates a medium size of 300x172, 2nd one is generating with 300x300.

Am i missing something that does not creating exact metadata as Media Library ?

Change History (1)

#1 @joemcgill
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hi @dipakbbsr, thanks for the report. It's interesting that there are encoding differences in the two serialized examples you provided, which might account for some strange behavior, but the intermediate sizes are created during this line, which should be consistent with what happens in the back end:

wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );

It could be a problem with the load order of the files, noting that you're manually including wp-admin/includes/image.php and some required functionality for properly handling resizing might not be available.

At this point, I would generally suggest trying to use the REST API for uploading files from the front end. You might find some helpful info here: https://wordpress.stackexchange.com/questions/209928/add-media-with-wp-rest-api-v2. Trying to handle the upload process manually falls outside the scope of what I would consider supported behavior.

I'm going to go ahead and close this for now, but if you do find that there is a specific bug in the way the metadata is generated that you think _should_ be supported, please feel free to reopen with more details.

Thanks!

Note: See TracTickets for help on using tickets.