| 3751 | |
| 3752 | |
| 3753 | /** |
| 3754 | * Test that generated files with the `image_editor_output_format` applied use the correct |
| 3755 | * quality level based on the ir mime type. |
| 3756 | * |
| 3757 | * @ticket 56442 |
| 3758 | */ |
| 3759 | public function test_quality_with_image_conversion_file_sizes() { |
| 3760 | if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) { |
| 3761 | $this->markTestSkipped( 'WebP is not supported on this system.' ); |
| 3762 | } |
| 3763 | add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) ); |
| 3764 | $temp_dir = get_temp_dir(); |
| 3765 | $file = $temp_dir . '/33772.jpg'; |
| 3766 | copy( DIR_TESTDATA . '/images/33772.jpg', $file ); |
| 3767 | |
| 3768 | // Set JPEG output quality very low and WebP quality very high, this should force all generated WebP images to |
| 3769 | // be larger than the the matching generated JPEGs. |
| 3770 | add_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low_jpeg' ), 10, 2 ); |
| 3771 | |
| 3772 | $editor = wp_get_image_editor( $file ); |
| 3773 | |
| 3774 | $attachment_id = self::factory()->attachment->create_object( |
| 3775 | $file, |
| 3776 | 0, |
| 3777 | array( |
| 3778 | 'post_mime_type' => 'image/jpeg', |
| 3779 | ) |
| 3780 | ); |
| 3781 | |
| 3782 | add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) ); |
| 3783 | |
| 3784 | // Generate all sizes as JPEGs. |
| 3785 | $jpeg_sizes = wp_generate_attachment_metadata( $attachment_id, $file ); |
| 3786 | remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_jpeg' ) ); |
| 3787 | |
| 3788 | // Generate all sizes as WebP. |
| 3789 | add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) ); |
| 3790 | $webp_sizes = wp_generate_attachment_metadata( $attachment_id, $file ); |
| 3791 | remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) ); |
| 3792 | |
| 3793 | // The main (scaled) image: the JPEG should be smaller than the WebP. |
| 3794 | $this->assertLessThan( $webp_sizes['filesize'], $jpeg_sizes['filesize'] ); |
| 3795 | |
| 3796 | // Sub-sizes: for each size, the JPEGs should be smaller than the WebP. |
| 3797 | $sizes_to_compare = array_intersect_key( $jpeg_sizes['sizes'], $webp_sizes['sizes'] ); |
| 3798 | foreach ( $sizes_to_compare as $size => $size_data ) { |
| 3799 | $this->assertLessThan( $webp_sizes['sizes'][ $size ]['filesize'], $jpeg_sizes['sizes'][ $size ]['filesize'] ); |
| 3800 | } |
| 3801 | |
| 3802 | // Cleanup. |
| 3803 | remove_filter( 'wp_editor_set_quality', array( $this, 'image_editor_change_quality_low_jpeg' ), 10, 2 ); |
| 3804 | remove_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) ); |
| 3805 | } |
| 3806 | |
| 3807 | /** |
| 3808 | * Add threshold to create a `-scaled` output image for testing. |
| 3809 | */ |
| 3810 | public function add_big_image_size_threshold() { |
| 3811 | return 1000; |
| 3812 | } |
| 3813 | |
| 3814 | /** |
| 3815 | * Output JPEG files. |
| 3816 | */ |
| 3817 | public function image_editor_output_jpeg() { |
| 3818 | return array( 'image/jpeg' => 'image/jpeg' ); |
| 3819 | } |
| 3820 | |
| 3821 | /** |
| 3822 | * Output WebP files. |
| 3823 | */ |
| 3824 | public function image_editor_output_webp() { |
| 3825 | return array( 'image/jpeg' => 'image/webp' ); |
| 3826 | } |
| 3827 | |
| 3828 | /** |
| 3829 | * Changes the quality using very low quality for JPEGs and very high quality |
| 3830 | * for WebPs, used to verify the filter is applying correctly. |
| 3831 | * |
| 3832 | * @param int $quality Default quality. |
| 3833 | * @param string $mime_type Image mime-type. |
| 3834 | * @return int The changed quality. |
| 3835 | */ |
| 3836 | public function image_editor_change_quality_low_jpeg( $quality, $mime_type ) { |
| 3837 | if ( 'image/jpeg' === $mime_type ) { |
| 3838 | return 1; |
| 3839 | } elseif ( 'image/webp' === $mime_type ) { |
| 3840 | return 100; |
| 3841 | } else { |
| 3842 | return 30; |
| 3843 | } |
| 3844 | } |
| 3845 | |