@@ -39,6 +39,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
3939 }
4040
4141 $ is_update = 'update ' === $ context ;
42+ $ target = isset ( $ _REQUEST ['target ' ] ) ? $ _REQUEST ['target ' ] : 'all ' ;
4243 if ( $ is_update ) {
4344 if ( empty ( $ metadata ['file ' ] ) ) {
4445 return $ metadata ;
@@ -53,8 +54,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
5354 $ file = get_attached_file ( $ attachment_id , true );
5455 }
5556
56- // File does not exist.
57- if ( ! file_exists ( $ file ) ) {
57+ // File does not exist and we are not editing only the thumbnail .
58+ if ( ' thumbnail ' !== $ target && ! file_exists ( $ file ) ) {
5859 return $ metadata ;
5960 }
6061
@@ -68,7 +69,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
6869 }
6970
7071 if ( in_array ( $ mime_type , $ valid_mime_transforms [ $ mime_type ], true ) ) {
71- if ( empty ( $ metadata ['sources ' ][ $ mime_type ] ) || $ is_update ) {
72+ if ( empty ( $ metadata ['sources ' ][ $ mime_type ] ) || ( $ is_update && ' thumbnail ' !== $ target ) ) {
7273 $ metadata ['sources ' ][ $ mime_type ] = array (
7374 'file ' => wp_basename ( $ file ),
7475 'filesize ' => filesize ( $ file ),
@@ -87,28 +88,30 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
8788 $ filename = pathinfo ( $ file , PATHINFO_FILENAME );
8889 $ allowed_mimes = array_flip ( wp_get_mime_types () );
8990
90- // Create the sources for the full sized image.
91- foreach ( $ valid_mime_transforms [ $ mime_type ] as $ targeted_mime ) {
92- // If this property exists no need to create the image again unless is an update.
93- if ( ! empty ( $ metadata ['sources ' ][ $ targeted_mime ] ) && ! $ is_update ) {
94- continue ;
95- }
91+ // Create the sources for the full sized image only if the target is not the thumbnail only.
92+ if ( 'thumbnail ' !== $ target ) {
93+ foreach ( $ valid_mime_transforms [ $ mime_type ] as $ targeted_mime ) {
94+ // If this property exists no need to create the image again unless is an update.
95+ if ( ! empty ( $ metadata ['sources ' ][ $ targeted_mime ] ) && ! $ is_update ) {
96+ continue ;
97+ }
9698
97- // The targeted mime is not allowed in the current installation.
98- if ( empty ( $ allowed_mimes [ $ targeted_mime ] ) ) {
99- continue ;
100- }
99+ // The targeted mime is not allowed in the current installation.
100+ if ( empty ( $ allowed_mimes [ $ targeted_mime ] ) ) {
101+ continue ;
102+ }
101103
102- $ extension = explode ( '| ' , $ allowed_mimes [ $ targeted_mime ] );
103- $ destination = trailingslashit ( $ original_directory ) . "{$ filename }. {$ extension [0 ]}" ;
104- $ image = webp_uploads_generate_additional_image_source ( $ attachment_id , $ original_size_data , $ targeted_mime , $ destination );
104+ $ extension = explode ( '| ' , $ allowed_mimes [ $ targeted_mime ] );
105+ $ destination = trailingslashit ( $ original_directory ) . "{$ filename }. {$ extension [0 ]}" ;
106+ $ image = webp_uploads_generate_additional_image_source ( $ attachment_id , $ original_size_data , $ targeted_mime , $ destination );
105107
106- if ( is_wp_error ( $ image ) ) {
107- continue ;
108- }
108+ if ( is_wp_error ( $ image ) ) {
109+ continue ;
110+ }
109111
110- $ metadata ['sources ' ][ $ targeted_mime ] = $ image ;
111- wp_update_attachment_metadata ( $ attachment_id , $ metadata );
112+ $ metadata ['sources ' ][ $ targeted_mime ] = $ image ;
113+ wp_update_attachment_metadata ( $ attachment_id , $ metadata );
114+ }
112115 }
113116
114117 // Make sure we have some sizes to work with, otherwise avoid any work.
@@ -122,6 +125,11 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
122125 }
123126
124127 foreach ( $ metadata ['sizes ' ] as $ size_name => $ properties ) {
128+
129+ if ( 'thumbnail ' === $ target && 'thumbnail ' !== $ size_name ) {
130+ continue ;
131+ }
132+
125133 // This image size is not defined or not an array.
126134 if ( ! is_array ( $ properties ) ) {
127135 continue ;
@@ -160,12 +168,32 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
160168 }
161169
162170 foreach ( $ valid_mime_transforms [ $ mime_type ] as $ mime ) {
163- // If this property exists no need to create the image again.
171+ // If this property exists no need to create the image again unless is an update with a different mime .
164172 if ( ! empty ( $ properties ['sources ' ][ $ mime ] ) && ! $ is_update ) {
165173 continue ;
166174 }
167175
168- $ source = webp_uploads_generate_image_size ( $ attachment_id , $ size_name , $ mime );
176+ if ( $ mime === $ current_mime ) {
177+ continue ;
178+ }
179+
180+ if ( 'update ' === $ context && 'thumbnail ' === $ target ) {
181+ /**
182+ * When only the thumbnail requires additional image, make sure that the base image to create additional
183+ * mime types is the thumbnail with the original mime type due this image is the only one that was modified
184+ * using the attached image or original image would be. The filename should match the original image with
185+ * the only difference of the extension on the filename instead, so the new created image does not have multiple
186+ * suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp
187+ */
188+ $ original_extension = explode ( '| ' , $ allowed_mimes [ $ current_mime ] );
189+ $ target_extension = explode ( '| ' , $ allowed_mimes [ $ mime ] );
190+ $ file_path = path_join ( $ original_directory , $ properties ['file ' ] );
191+ $ destination = preg_replace ( "/\. {$ original_extension [0 ]}$/ " , ". {$ target_extension [0 ]}" , $ file_path );
192+ $ source = webp_uploads_generate_image_size ( $ attachment_id , $ size_name , $ mime , $ file_path , $ destination );
193+ } else {
194+ $ source = webp_uploads_generate_image_size ( $ attachment_id , $ size_name , $ mime );
195+ }
196+
169197 if ( is_wp_error ( $ source ) ) {
170198 continue ;
171199 }
@@ -229,13 +257,15 @@ function webp_uploads_filter_image_editor_output_format( $output_format, $filena
229257 *
230258 * @see wp_create_image_subsizes()
231259 *
232- * @param int $attachment_id The ID of the attachment we are going to use as a reference to create the image.
233- * @param string $size The size name that would be used to create this image, out of the registered subsizes.
234- * @param string $mime A mime type we are looking to use to create this image.
260+ * @param int $attachment_id The ID of the attachment we are going to use as a reference to create the image.
261+ * @param string $size The size name that would be used to create this image, out of the registered subsizes.
262+ * @param string $mime A mime type we are looking to use to create this image.
263+ * @param string $file_path The path to the file used to create the image with.
264+ * @param string $destination_file_name The name used to store the created file it should include the full path.
235265 *
236266 * @return array|WP_Error
237267 */
238- function webp_uploads_generate_image_size ( $ attachment_id , $ size , $ mime ) {
268+ function webp_uploads_generate_image_size ( $ attachment_id , $ size , $ mime, $ file_path = null , $ destination_file_name = null ) {
239269 $ sizes = wp_get_registered_image_subsizes ();
240270 $ metadata = wp_get_attachment_metadata ( $ attachment_id );
241271
@@ -251,6 +281,7 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) {
251281 'width ' => 0 ,
252282 'height ' => 0 ,
253283 'crop ' => false ,
284+ 'file ' => $ file_path ,
254285 );
255286
256287 if ( isset ( $ sizes [ $ size ]['width ' ] ) ) {
@@ -269,7 +300,7 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) {
269300 $ size_data ['crop ' ] = (bool ) $ sizes [ $ size ]['crop ' ];
270301 }
271302
272- return webp_uploads_generate_additional_image_source ( $ attachment_id , $ size_data , $ mime );
303+ return webp_uploads_generate_additional_image_source ( $ attachment_id , $ size_data , $ mime, $ destination_file_name );
273304}
274305
275306/**
@@ -340,7 +371,11 @@ function webp_uploads_generate_additional_image_source( $attachment_id, array $s
340371 return new WP_Error ( 'image_mime_type_not_supported ' , __ ( 'The provided mime type is not supported. ' , 'performance-lab ' ) );
341372 }
342373
343- $ image_path = wp_get_original_image_path ( $ attachment_id );
374+ if ( empty ( $ size_data ['file ' ] ) ) {
375+ $ image_path = wp_get_original_image_path ( $ attachment_id );
376+ } else {
377+ $ image_path = $ size_data ['file ' ];
378+ }
344379
345380 // File does not exist.
346381 if ( ! file_exists ( $ image_path ) ) {
0 commit comments