2121class Tiny_Image {
2222 const ORIGINAL = 0 ;
2323
24+ /** @var Tiny_Settings */
2425 private $ settings ;
2526 private $ id ;
2627 private $ name ;
@@ -179,7 +180,6 @@ public function compress() {
179180 $ success = 0 ;
180181 $ failed = 0 ;
181182
182- $ compressor = $ this ->settings ->get_compressor ();
183183 $ active_tinify_sizes = $ this ->settings ->get_active_tinify_sizes ();
184184
185185 if ( $ this ->settings ->get_conversion_enabled () ) {
@@ -191,20 +191,26 @@ public function compress() {
191191 $ unprocessed_sizes = $ this ->filter_image_sizes ( 'uncompressed ' , $ active_tinify_sizes );
192192 }
193193
194+ $ compressor = $ this ->settings ->get_compressor ();
195+ $ convert_to = $ this ->convert_to ();
196+
194197 foreach ( $ unprocessed_sizes as $ size_name => $ size ) {
195198 if ( ! $ size ->is_duplicate () ) {
196199 $ size ->add_tiny_meta_start ();
197200 $ this ->update_tiny_post_meta ();
198201 $ resize = $ this ->settings ->get_resize_options ( $ size_name );
199202 $ preserve = $ this ->settings ->get_preserve_options ( $ size_name );
200- $ convert_opts = $ this ->settings ->get_conversion_options ();
201203 try {
202204 $ response = $ compressor ->compress_file (
203205 $ size ->filename ,
204206 $ resize ,
205207 $ preserve ,
206- $ convert_opts
208+ $ convert_to
207209 );
210+
211+ // ensure that all conversion are in the same format as the first one
212+ $ convert_to = isset ($ response ['convert ' ]) ? array ($ response ['convert ' ]['type ' ]) : $ convert_to ;
213+
208214 $ size ->add_tiny_meta ( $ response );
209215 $ success ++;
210216 } catch ( Tiny_Exception $ e ) {
@@ -243,17 +249,19 @@ public function compress_retina( $size_name, $path ) {
243249 if ( ! isset ( $ this ->sizes [ $ size_name ] ) ) {
244250 $ this ->sizes [ $ size_name ] = new Tiny_Image_Size ( $ path );
245251 }
252+
246253 $ size = $ this ->sizes [ $ size_name ];
254+
255+ $ compressor = $ this ->settings ->get_compressor ();
256+ $ convert_to = $ this ->convert_to ();
247257
248258 if ( ! $ size ->has_been_compressed () ) {
249259 $ size ->add_tiny_meta_start ();
250260 $ this ->update_tiny_post_meta ();
251- $ compressor = $ this ->settings ->get_compressor ();
252261 $ preserve = $ this ->settings ->get_preserve_options ( $ size_name );
253- $ conversion = $ this ->settings ->get_conversion_options ();
254262
255263 try {
256- $ response = $ compressor ->compress_file ( $ path , false , $ preserve , $ conversion );
264+ $ response = $ compressor ->compress_file ( $ path , false , $ preserve , $ convert_to );
257265 $ size ->add_tiny_meta ( $ response );
258266 } catch ( Tiny_Exception $ e ) {
259267 $ size ->add_tiny_meta_error ( $ e );
@@ -472,6 +480,37 @@ public function can_be_converted() {
472480 return $ this ->settings ->get_conversion_enabled () && $ this ->file_type_allowed ();
473481 }
474482
483+ /**
484+ * Get the targeted conversion.
485+ * If original is already converted, then we use the originals' mimetype.
486+ * If nothing is converted yet, we use the settings conversion settings.
487+ *
488+ * @since 3.6.4
489+ *
490+ * @return array{string} mimetypes to which the image should be converted to
491+ */
492+ private function convert_to () {
493+ $ convert_settings = $ this ->settings ->get_conversion_options ();
494+ if ( ! $ convert_settings ['convert ' ] ) {
495+ // conversion is off so return no mimetypes to convert to
496+ return array ();
497+ }
498+
499+ if ( isset ( $ this ->sizes [ self ::ORIGINAL ] ) ) {
500+ // original is not in sizes so mimetypes are open
501+ return $ convert_settings ['convert_to ' ];
502+ }
503+
504+ $ original_img_size = $ this ->sizes [ self ::ORIGINAL ];
505+ if ( $ original_img_size ->converted () ) {
506+ // original has been convert so use that mimetype to convert to
507+ return array ($ original_img_size ->meta ['convert ' ]['type ' ]);
508+ }
509+
510+ return $ convert_settings ['convert_to ' ];
511+
512+ }
513+
475514 /**
476515 * Marks the image as compressed without actually compressing it.
477516 *
0 commit comments