| @@ -53,22 +53,30 @@ | ||
| 53 | 53 | */ |
| 54 | 54 | public function image_optimization( $file ) { |
| 55 | 55 | global $quickwebp_surecart_client; |
| 56 | 56 | |
| 57 | + quickwebp_debug_log( 'image_optimization:start', array( | |
| 58 | + 'file_name' => $file['name'] ?? '', | |
| 59 | + 'file_type' => $file['type'] ?? '', | |
| 60 | + ) ); | |
| 61 | + | |
| 57 | 62 | $settings = $this->get_settings(); |
| 58 | 63 | $image_file = $this->file_is_image( $file, $settings ); |
| 59 | 64 | |
| 60 | 65 | if ( ! $image_file ) { |
| 66 | + quickwebp_debug_log( 'image_optimization:not_image' ); | |
| 61 | 67 | return $file; |
| 62 | 68 | } |
| 63 | 69 | |
| 64 | 70 | $mode_enabled = $settings['quickwebp_settings_conversion']; |
| 65 | 71 | if ( $mode_enabled === '0' ) { |
| 72 | + quickwebp_debug_log( 'image_optimization:conversion_disabled' ); | |
| 66 | 73 | return $image_file; |
| 67 | 74 | } |
| 68 | 75 | |
| 69 | 76 | $save_original = is_array( $settings['quickwebp_settings_conversion_save_original'] ) ? $settings['quickwebp_settings_conversion_save_original'] : array(); |
| 70 | 77 | if ( in_array( 'checked', $save_original ) ) { |
| 78 | + quickwebp_debug_log( 'image_optimization:save_original_enabled' ); | |
| 71 | 79 | return $image_file; |
| 72 | 80 | } |
| 73 | 81 | |
| 74 | 82 | $ignore_same_format = is_array( $settings['quickwebp_settings_conversion_ignore_webp'] ) ? $settings['quickwebp_settings_conversion_ignore_webp'] : array(); |
| @@ -74,10 +82,12 @@ | ||
| 74 | 82 | $ignore_same_format = is_array( $settings['quickwebp_settings_conversion_ignore_webp'] ) ? $settings['quickwebp_settings_conversion_ignore_webp'] : array(); |
| 75 | 83 | $mime_type = wp_get_image_mime( $image_file['tmp_name'] ); |
| 76 | 84 | if ( in_array( 'checked', $ignore_same_format ) ) { |
| 77 | 85 | if ( '1' == $mode_enabled && 'image/webp' == $mime_type ) { |
| 86 | + quickwebp_debug_log( 'image_optimization:skip_same_format', array( 'mime_type' => $mime_type ) ); | |
| 78 | 87 | return $image_file; |
| 79 | 88 | } elseif( '2' == $mode_enabled && 'image/avif' == $mime_type ) { |
| 89 | + quickwebp_debug_log( 'image_optimization:skip_same_format', array( 'mime_type' => $mime_type ) ); | |
| 80 | 90 | return $image_file; |
| 81 | 91 | } |
| 82 | 92 | } |
| 83 | 93 | |
| @@ -90,22 +100,28 @@ | ||
| 90 | 100 | $image_file['new_size'] = $image_file['size']; |
| 91 | 101 | $image_file['new_type'] = $image_file['type']; |
| 92 | 102 | |
| 93 | 103 | if ( '2' === $mode_enabled && $is_pro ) { |
| 104 | + quickwebp_debug_log( 'image_optimization:try_avif', array( 'mime_type' => $mime_type, 'quality' => $quality ) ); | |
| 94 | 105 | $avif_image = $this->create_avif_image( $image_file['tmp_name'], $image_file['tmp_name'], $quality ); |
| 95 | 106 | if ( $avif_image ) { |
| 96 | 107 | $image_file['size'] = filesize( $image_file['tmp_name'] ); |
| 97 | 108 | $image_file['type'] = 'image/avif'; |
| 98 | 109 | $image_file['quickwebp'] = 'optimized'; |
| 110 | + $this->record_optimization_stats( $image_file['new_size'], $image_file['size'] ); | |
| 111 | + quickwebp_debug_log( 'image_optimization:avif_success', array( 'new_size' => $image_file['size'] ) ); | |
| 99 | 112 | } |
| 100 | 113 | } |
| 101 | 114 | |
| 102 | 115 | if ( '1' === $mode_enabled ) { |
| 116 | + quickwebp_debug_log( 'image_optimization:try_webp', array( 'mime_type' => $mime_type, 'quality' => $quality ) ); | |
| 103 | 117 | $webp_image = $this->create_webp_image( $image_file['tmp_name'], $image_file['tmp_name'], $quality ); |
| 104 | 118 | if ( $webp_image ) { |
| 105 | 119 | $image_file['size'] = filesize( $image_file['tmp_name'] ); |
| 106 | 120 | $image_file['type'] = 'image/webp'; |
| 107 | 121 | $image_file['quickwebp'] = 'optimized'; |
| 122 | + $this->record_optimization_stats( $image_file['new_size'], $image_file['size'] ); | |
| 123 | + quickwebp_debug_log( 'image_optimization:webp_success', array( 'new_size' => $image_file['size'] ) ); | |
| 108 | 124 | } |
| 109 | 125 | } |
| 110 | 126 | |
| 111 | 127 | return $image_file; |
| @@ -175,8 +191,10 @@ | ||
| 175 | 191 | } |
| 176 | 192 | } |
| 177 | 193 | |
| 178 | 194 | if ( ! empty( $new_sizes ) ) { |
| 195 | + $this->record_attachment_optimization_stats( $new_sizes ); | |
| 196 | + | |
| 179 | 197 | if ( '1' == $mode_enabled ) { |
| 180 | 198 | update_post_meta( $attachment_id, 'quickwebp_already_optimized', '1' ); |
| 181 | 199 | } elseif ( '2' == $mode_enabled ) { |
| 182 | 200 | update_post_meta( $attachment_id, 'quickwebp_already_optimized', '2' ); |
| @@ -278,8 +296,10 @@ | ||
| 278 | 296 | */ |
| 279 | 297 | public function image_optimization_ajax() { |
| 280 | 298 | global $quickwebp_surecart_client; |
| 281 | 299 | |
| 300 | + quickwebp_debug_log( 'image_optimization_ajax:start' ); | |
| 301 | + | |
| 282 | 302 | if ( ! current_user_can( 'upload_files' ) ) { |
| 283 | 303 | wp_send_json_error( __( 'You are not allowed to upload files.', 'quickwebp' ), 403 ); |
| 284 | 304 | } |
| 285 | 305 | |
| @@ -291,8 +311,9 @@ | ||
| 291 | 311 | |
| 292 | 312 | // Get the file |
| 293 | 313 | $file = count($_FILES) > 0 ? array_shift($_FILES) : array(); |
| 294 | 314 | if ( empty($file) ) { |
| 315 | + quickwebp_debug_log( 'image_optimization_ajax:missing_file' ); | |
| 295 | 316 | wp_send_json_error( __( 'No image uploaded, try again.', 'quickwebp' ) ); |
| 296 | 317 | } |
| 297 | 318 | |
| 298 | 319 | $settings = $this->get_ajax_settings(); |
| @@ -298,13 +319,15 @@ | ||
| 298 | 319 | $settings = $this->get_ajax_settings(); |
| 299 | 320 | $image_file = $this->file_is_image( $file, $settings ); |
| 300 | 321 | |
| 301 | 322 | if ( ! $image_file ) { |
| 323 | + quickwebp_debug_log( 'image_optimization_ajax:not_image' ); | |
| 302 | 324 | wp_send_json_error( __( 'No image uploaded, try again.', 'quickwebp' ) ); |
| 303 | 325 | } |
| 304 | 326 | |
| 305 | 327 | $mode_enabled = $settings['quickwebp_settings_conversion']; |
| 306 | 328 | if ( $mode_enabled === '0' ) { |
| 329 | + quickwebp_debug_log( 'image_optimization_ajax:conversion_disabled' ); | |
| 307 | 330 | $image_file['new_size'] = $image_file['size']; |
| 308 | 331 | $image_file['new_type'] = $image_file['type']; |
| 309 | 332 | $this->return_ajax_data( $image_file ); |
| 310 | 333 | } |
| @@ -318,8 +341,9 @@ | ||
| 318 | 341 | $image_file['new_size'] = $image_file['size']; |
| 319 | 342 | $image_file['new_type'] = $image_file['type']; |
| 320 | 343 | |
| 321 | 344 | if ( '2' === $mode_enabled && $is_pro ) { |
| 345 | + quickwebp_debug_log( 'image_optimization_ajax:try_avif', array( 'quality' => $quality ) ); | |
| 322 | 346 | $avif_image = $this->create_avif_image( $image_file['tmp_name'], $image_file['tmp_name'], $quality ); |
| 323 | 347 | if ( $avif_image ) { |
| 324 | 348 | $image_file['new_size'] = filesize( $image_file['tmp_name'] ); |
| 325 | 349 | $image_file['new_type'] = 'image/avif'; |
| @@ -326,8 +350,9 @@ | ||
| 326 | 350 | } |
| 327 | 351 | } |
| 328 | 352 | |
| 329 | 353 | if ( '1' === $mode_enabled ) { |
| 354 | + quickwebp_debug_log( 'image_optimization_ajax:try_webp', array( 'quality' => $quality ) ); | |
| 330 | 355 | $webp_image = $this->create_webp_image( $image_file['tmp_name'], $image_file['tmp_name'], $quality ); |
| 331 | 356 | if ( $webp_image ) { |
| 332 | 357 | $image_file['new_size'] = filesize( $image_file['tmp_name'] ); |
| 333 | 358 | $image_file['new_type'] = 'image/webp'; |
| @@ -340,8 +365,9 @@ | ||
| 340 | 365 | /** |
| 341 | 366 | * Optimize a single media |
| 342 | 367 | */ |
| 343 | 368 | public function single_optimization_ajax() { |
| 369 | + quickwebp_debug_log( 'single_optimization_ajax:start' ); | |
| 344 | 370 | |
| 345 | 371 | // verify the nonce. |
| 346 | 372 | $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 347 | 373 | if( !wp_verify_nonce( $nonce, 'quickwebp_admin_attachment' ) ) { |
| @@ -351,8 +377,9 @@ | ||
| 351 | 377 | // Sanitize data |
| 352 | 378 | $attachment_id = isset( $_POST['attachment_id'] ) ? absint( wp_unslash( $_POST['attachment_id'] ) ) : 0; |
| 353 | 379 | |
| 354 | 380 | if ( ! $attachment_id ) { |
| 381 | + quickwebp_debug_log( 'single_optimization_ajax:missing_attachment_id' ); | |
| 355 | 382 | wp_send_json_error( __( 'No attachment id.', 'quickwebp' ) ); |
| 356 | 383 | } |
| 357 | 384 | |
| 358 | 385 | $settings = $this->get_settings(); |
| @@ -379,8 +406,9 @@ | ||
| 379 | 406 | $sizes = $this->get_media_files( $attachment_id ); |
| 380 | 407 | $new_sizes = array(); |
| 381 | 408 | |
| 382 | 409 | foreach ( $sizes as $key => $size ) { |
| 410 | + quickwebp_debug_log( 'single_optimization_ajax:optimize_size', array( 'size_key' => $key, 'path' => $size['path'] ?? '' ) ); | |
| 383 | 411 | $result = $this->optimize_local_file( $size ); |
| 384 | 412 | |
| 385 | 413 | if ( $result ) { |
| 386 | 414 | $new_sizes[$key] = $result; |
| @@ -387,8 +415,9 @@ | ||
| 387 | 415 | } |
| 388 | 416 | } |
| 389 | 417 | |
| 390 | 418 | if ( ! empty( $new_sizes ) ) { |
| 419 | + $this->record_attachment_optimization_stats( $new_sizes ); | |
| 391 | 420 | |
| 392 | 421 | $data = get_post_meta( $attachment_id, 'quickwebp_data', true ); |
| 393 | 422 | if ( ! empty( $data ) ) { |
| 394 | 423 | $this->remove_related_files( $data ); |
| @@ -459,8 +488,9 @@ | ||
| 459 | 488 | /** |
| 460 | 489 | * Get the image data for the preview in the settings page |
| 461 | 490 | */ |
| 462 | 491 | public function get_image_data_for_preview_ajax() { |
| 492 | + quickwebp_debug_log( 'get_image_data_for_preview_ajax:start' ); | |
| 463 | 493 | |
| 464 | 494 | // verify the nonce. |
| 465 | 495 | $nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 466 | 496 | if( !wp_verify_nonce( $nonce, 'image_optimize_nonce' ) ) { |
| @@ -469,13 +499,15 @@ | ||
| 469 | 499 | |
| 470 | 500 | // Get the file |
| 471 | 501 | $file = count($_FILES) > 0 ? array_shift($_FILES) : array(); |
| 472 | 502 | if ( empty($file) ) { |
| 503 | + quickwebp_debug_log( 'get_image_data_for_preview_ajax:missing_file' ); | |
| 473 | 504 | wp_send_json_error( __( 'No image uploaded, try again.', 'quickwebp' ) ); |
| 474 | 505 | } |
| 475 | 506 | |
| 476 | 507 | $image_file = $this->file_is_image( $file ); |
| 477 | 508 | if ( ! $image_file ) { |
| 509 | + quickwebp_debug_log( 'get_image_data_for_preview_ajax:not_image' ); | |
| 478 | 510 | wp_send_json_error( __( 'No image uploaded, try again.', 'quickwebp' ) ); |
| 479 | 511 | } |
| 480 | 512 | |
| 481 | 513 | $preview_image_data = array( |
| @@ -488,8 +520,9 @@ | ||
| 488 | 520 | $qualities = array( 'low', 'medium', 'high', 'extra_high' ); |
| 489 | 521 | |
| 490 | 522 | foreach ( $conversions as $conversion ) { |
| 491 | 523 | foreach ( $qualities as $quality ) { |
| 524 | + quickwebp_debug_log( 'get_image_data_for_preview_ajax:preview_variant', array( 'conversion' => $conversion, 'quality' => $quality ) ); | |
| 492 | 525 | |
| 493 | 526 | $size_after = 0; |
| 494 | 527 | |
| 495 | 528 | if ( '2' == $conversion ) { |
| @@ -645,14 +678,18 @@ | ||
| 645 | 678 | */ |
| 646 | 679 | public function optimize_local_file( $size ) { |
| 647 | 680 | global $quickwebp_surecart_client; |
| 648 | 681 | |
| 682 | + quickwebp_debug_log( 'optimize_local_file:start', array( 'path' => $size['path'] ?? '' ) ); | |
| 683 | + | |
| 649 | 684 | if ( !is_file($size['path']) ) { |
| 685 | + quickwebp_debug_log( 'optimize_local_file:missing_file', array( 'path' => $size['path'] ?? '' ) ); | |
| 650 | 686 | return false; |
| 651 | 687 | } |
| 652 | 688 | |
| 653 | 689 | $real_type = mime_content_type( $size['path']); |
| 654 | 690 | if ( !in_array( $real_type, $this->allowed_mime_types ) ) { |
| 691 | + quickwebp_debug_log( 'optimize_local_file:invalid_mime_type', array( 'mime_type' => $real_type ) ); | |
| 655 | 692 | return false; |
| 656 | 693 | } |
| 657 | 694 | |
| 658 | 695 | $settings = $this->get_settings(); |
| @@ -666,13 +703,15 @@ | ||
| 666 | 703 | $new_path = $size['path'] . '.' . ( '2' === $mode_enabled && $is_pro ? 'avif' : 'webp' ); |
| 667 | 704 | $size_after = 0; |
| 668 | 705 | |
| 669 | 706 | if ( '2' === $mode_enabled && $is_pro ) { |
| 707 | + quickwebp_debug_log( 'optimize_local_file:try_avif', array( 'path' => $size['path'], 'quality' => $quality ) ); | |
| 670 | 708 | $avif_image = $this->create_avif_image( $size['path'], $new_path, $quality ); |
| 671 | 709 | if ( $avif_image ) { |
| 672 | 710 | $size_after = filesize( $new_path ); |
| 673 | 711 | } |
| 674 | 712 | } elseif ( '1' === $mode_enabled ) { |
| 713 | + quickwebp_debug_log( 'optimize_local_file:try_webp', array( 'path' => $size['path'], 'quality' => $quality ) ); | |
| 675 | 714 | $webp_image = $this->create_webp_image( $size['path'], $new_path, $quality ); |
| 676 | 715 | if ( $webp_image ) { |
| 677 | 716 | $size_after = filesize( $new_path ); |
| 678 | 717 | } |
| @@ -678,8 +717,9 @@ | ||
| 678 | 717 | } |
| 679 | 718 | } |
| 680 | 719 | |
| 681 | 720 | if ( ! $size_after ) { |
| 721 | + quickwebp_debug_log( 'optimize_local_file:conversion_failed', array( 'path' => $size['path'], 'target_path' => $new_path ) ); | |
| 682 | 722 | return false; |
| 683 | 723 | } |
| 684 | 724 | |
| 685 | 725 | $deference = $size_before - $size_after; |
| @@ -695,8 +735,58 @@ | ||
| 695 | 735 | ); |
| 696 | 736 | } |
| 697 | 737 | |
| 698 | 738 | /** |
| 739 | + * Record aggregated optimization statistics for one image attachment. | |
| 740 | + * | |
| 741 | + * @param array $optimized_sizes Optimization results for the attachment sizes. | |
| 742 | + */ | |
| 743 | + public function record_attachment_optimization_stats( $optimized_sizes ) { | |
| 744 | + $size_before = 0; | |
| 745 | + $size_after = 0; | |
| 746 | + | |
| 747 | + foreach ( $optimized_sizes as $optimized_size ) { | |
| 748 | + $size_before += absint( $optimized_size['original_size'] ?? 0 ); | |
| 749 | + $size_after += absint( $optimized_size['optimized_size'] ?? 0 ); | |
| 750 | + } | |
| 751 | + | |
| 752 | + $this->record_optimization_stats( $size_before, $size_after ); | |
| 753 | + } | |
| 754 | + | |
| 755 | + /** | |
| 756 | + * Record local cumulative optimization statistics. | |
| 757 | + * | |
| 758 | + * @param int $size_before File size before optimization in bytes. | |
| 759 | + * @param int $size_after File size after optimization in bytes. | |
| 760 | + */ | |
| 761 | + public function record_optimization_stats( $size_before, $size_after ) { | |
| 762 | + $size_before = absint( $size_before ); | |
| 763 | + $size_after = absint( $size_after ); | |
| 764 | + | |
| 765 | + if ( ! $size_before || ! $size_after ) { | |
| 766 | + return; | |
| 767 | + } | |
| 768 | + | |
| 769 | + $stats = get_option( 'quickwebp_optimization_stats', array() ); | |
| 770 | + $stats = wp_parse_args( | |
| 771 | + is_array( $stats ) ? $stats : array(), | |
| 772 | + array( | |
| 773 | + 'images_optimized' => 0, | |
| 774 | + 'bytes_before' => 0, | |
| 775 | + 'bytes_after' => 0, | |
| 776 | + 'bytes_saved' => 0, | |
| 777 | + ) | |
| 778 | + ); | |
| 779 | + | |
| 780 | + $stats['images_optimized'] = absint( $stats['images_optimized'] ) + 1; | |
| 781 | + $stats['bytes_before'] = absint( $stats['bytes_before'] ) + $size_before; | |
| 782 | + $stats['bytes_after'] = absint( $stats['bytes_after'] ) + $size_after; | |
| 783 | + $stats['bytes_saved'] = max( 0, $stats['bytes_before'] - $stats['bytes_after'] ); | |
| 784 | + | |
| 785 | + update_option( 'quickwebp_optimization_stats', $stats, false ); | |
| 786 | + } | |
| 787 | + | |
| 788 | + /** | |
| 699 | 789 | * Remove the related files of an optimized attachment |
| 700 | 790 | */ |
| 701 | 791 | public function remove_related_files( $data ) { |
| 702 | 792 | foreach ( $data as $value ) { |
| @@ -760,8 +850,9 @@ | ||
| 760 | 850 | $file_error = isset($file['error']) ? $file['error'] : ''; |
| 761 | 851 | $file_size = isset($file['size']) ? $file['size'] : ''; |
| 762 | 852 | |
| 763 | 853 | if ( empty($file_tmp_name) ) { |
| 854 | + quickwebp_debug_log( 'file_is_image:missing_tmp_name', array( 'file_name' => $file_name ) ); | |
| 764 | 855 | return false; |
| 765 | 856 | } |
| 766 | 857 | |
| 767 | 858 | $allowed_mime_types = apply_filters( 'quickwebp_mime_types_allowed', $this->allowed_mime_types ); |
| @@ -767,8 +858,9 @@ | ||
| 767 | 858 | $allowed_mime_types = apply_filters( 'quickwebp_mime_types_allowed', $this->allowed_mime_types ); |
| 768 | 859 | $is_image = wp_getimagesize($file_tmp_name); |
| 769 | 860 | $mime_type = wp_get_image_mime($file_tmp_name); |
| 770 | 861 | if ( ! $is_image || ! in_array( $mime_type, $allowed_mime_types ) ) { |
| 862 | + quickwebp_debug_log( 'file_is_image:rejected', array( 'file_name' => $file_name, 'mime_type' => $mime_type ) ); | |
| 771 | 863 | return false; |
| 772 | 864 | } |
| 773 | 865 | |
| 774 | 866 | $name = $file_name; |
| @@ -896,16 +988,25 @@ | ||
| 896 | 988 | * |
| 897 | 989 | * @return bool |
| 898 | 990 | */ |
| 899 | 991 | private function create_image_with_wp_editor( $file_path, $output_path, $quality, $target_mime_type ) { |
| 992 | + quickwebp_debug_log( 'create_image_with_wp_editor:start', array( | |
| 993 | + 'file_path' => $file_path, | |
| 994 | + 'output_path' => $output_path, | |
| 995 | + 'quality' => $quality, | |
| 996 | + 'target_mime_type' => $target_mime_type, | |
| 997 | + ) ); | |
| 998 | + | |
| 900 | 999 | $editor = wp_get_image_editor( $file_path ); |
| 901 | 1000 | |
| 902 | 1001 | if ( is_wp_error( $editor ) ) { |
| 1002 | + quickwebp_debug_log( 'create_image_with_wp_editor:editor_error', array( 'message' => $editor->get_error_message() ) ); | |
| 903 | 1003 | return false; |
| 904 | 1004 | } |
| 905 | 1005 | |
| 906 | 1006 | $extension = $this->get_extension_from_mime_type( $target_mime_type ); |
| 907 | 1007 | if ( empty( $extension ) ) { |
| 1008 | + quickwebp_debug_log( 'create_image_with_wp_editor:missing_extension', array( 'target_mime_type' => $target_mime_type ) ); | |
| 908 | 1009 | return false; |
| 909 | 1010 | } |
| 910 | 1011 | |
| 911 | 1012 | $save_path = $output_path; |
| @@ -916,15 +1017,33 @@ | ||
| 916 | 1017 | $editor->set_quality( $quality ); |
| 917 | 1018 | $result = $editor->save( $save_path, $target_mime_type ); |
| 918 | 1019 | |
| 919 | 1020 | if ( is_wp_error( $result ) || empty( $result['path'] ) || ! file_exists( $result['path'] ) ) { |
| 1021 | + quickwebp_debug_log( 'create_image_with_wp_editor:save_failed', array( | |
| 1022 | + 'output_path' => $save_path, | |
| 1023 | + 'error' => is_wp_error( $result ) ? $result->get_error_message() : 'missing_generated_file', | |
| 1024 | + ) ); | |
| 920 | 1025 | return false; |
| 921 | 1026 | } |
| 922 | 1027 | |
| 1028 | + $source_size = file_exists( $file_path ) ? filesize( $file_path ) : 0; | |
| 1029 | + $generated_size = filesize( $result['path'] ); | |
| 1030 | + | |
| 1031 | + if ( $source_size > 0 && $generated_size > $source_size ) { | |
| 1032 | + quickwebp_debug_log( 'create_image_with_wp_editor:larger_than_source', array( | |
| 1033 | + 'source_size' => $source_size, | |
| 1034 | + 'generated_size' => $generated_size, | |
| 1035 | + 'output_path' => $result['path'], | |
| 1036 | + ) ); | |
| 1037 | + wp_delete_file( $result['path'] ); | |
| 1038 | + return false; | |
| 1039 | + } | |
| 1040 | + | |
| 923 | 1041 | if ( $result['path'] !== $output_path ) { |
| 924 | 1042 | $file_contents = file_get_contents( $result['path'] ); |
| 925 | 1043 | |
| 926 | 1044 | if ( false === $file_contents || false === file_put_contents( $output_path, $file_contents ) ) { |
| 1045 | + quickwebp_debug_log( 'create_image_with_wp_editor:copy_failed', array( 'output_path' => $output_path ) ); | |
| 927 | 1046 | return false; |
| 928 | 1047 | } |
| 929 | 1048 | |
| 930 | 1049 | wp_delete_file( $result['path'] ); |
| @@ -929,8 +1048,14 @@ | ||
| 929 | 1048 | |
| 930 | 1049 | wp_delete_file( $result['path'] ); |
| 931 | 1050 | } |
| 932 | 1051 | |
| 1052 | + quickwebp_debug_log( 'create_image_with_wp_editor:success', array( | |
| 1053 | + 'output_path' => $output_path, | |
| 1054 | + 'generated_size' => file_exists( $output_path ) ? filesize( $output_path ) : $generated_size, | |
| 1055 | + 'target_mime_type'=> $target_mime_type, | |
| 1056 | + ) ); | |
| 1057 | + | |
| 933 | 1058 | return true; |
| 934 | 1059 | } |
| 935 | 1060 | |
| 936 | 1061 | /** |
| @@ -962,20 +1087,52 @@ | ||
| 962 | 1087 | return $loader( $file_path ); |
| 963 | 1088 | } |
| 964 | 1089 | |
| 965 | 1090 | /** |
| 1091 | + * Check whether the source mime type can be loaded through GD. | |
| 1092 | + * | |
| 1093 | + * @param string $mime_type Source mime type. | |
| 1094 | + * | |
| 1095 | + * @return bool | |
| 1096 | + */ | |
| 1097 | + private function can_load_image_with_gd( $mime_type ) { | |
| 1098 | + $loaders = array( | |
| 1099 | + 'image/avif' => 'imagecreatefromavif', | |
| 1100 | + 'image/webp' => 'imagecreatefromwebp', | |
| 1101 | + 'image/jpeg' => 'imagecreatefromjpeg', | |
| 1102 | + 'image/png' => 'imagecreatefrompng', | |
| 1103 | + ); | |
| 1104 | + | |
| 1105 | + if ( empty( $loaders[ $mime_type ] ) ) { | |
| 1106 | + return false; | |
| 1107 | + } | |
| 1108 | + | |
| 1109 | + return function_exists( $loaders[ $mime_type ] ); | |
| 1110 | + } | |
| 1111 | + | |
| 1112 | + /** | |
| 966 | 1113 | * Create the avif image |
| 967 | 1114 | */ |
| 968 | 1115 | private function create_avif_image( $file_path, $output_path, $quality ) { |
| 969 | - if ( $this->create_image_with_wp_editor( $file_path, $output_path, $quality, 'image/avif' ) ) { | |
| 1116 | + $image = false; | |
| 1117 | + $mime_type = wp_get_image_mime( $file_path ); | |
| 1118 | + | |
| 1119 | + quickwebp_debug_log( 'create_avif_image:start', array( | |
| 1120 | + 'file_path' => $file_path, | |
| 1121 | + 'output_path' => $output_path, | |
| 1122 | + 'mime_type' => $mime_type, | |
| 1123 | + 'quality' => $quality, | |
| 1124 | + ) ); | |
| 1125 | + | |
| 1126 | + if ( ! $this->can_load_image_with_gd( $mime_type ) && $this->create_image_with_wp_editor( $file_path, $output_path, $quality, 'image/avif' ) ) { | |
| 1127 | + quickwebp_debug_log( 'create_avif_image:used_wp_editor', array( 'mime_type' => $mime_type ) ); | |
| 970 | 1128 | return true; |
| 971 | 1129 | } |
| 972 | 1130 | |
| 973 | - $image = false; | |
| 974 | - $mime_type = wp_get_image_mime( $file_path ); | |
| 975 | 1131 | $image = $this->create_gd_image_resource( $file_path, $mime_type ); |
| 976 | 1132 | |
| 977 | 1133 | if ( ! $image ) { |
| 1134 | + quickwebp_debug_log( 'create_avif_image:gd_load_failed', array( 'mime_type' => $mime_type ) ); | |
| 978 | 1135 | return false; |
| 979 | 1136 | } |
| 980 | 1137 | |
| 981 | 1138 | // Handle EXIF orientation for proper image rotation |
| @@ -1036,11 +1193,17 @@ | ||
| 1036 | 1193 | |
| 1037 | 1194 | $avif_image = imageavif( $image, $output_path, $quality ); |
| 1038 | 1195 | imagedestroy( $image ); |
| 1039 | 1196 | if ( ! $avif_image ) { |
| 1197 | + quickwebp_debug_log( 'create_avif_image:gd_save_failed', array( 'output_path' => $output_path ) ); | |
| 1040 | 1198 | return false; |
| 1041 | 1199 | } |
| 1042 | 1200 | |
| 1201 | + quickwebp_debug_log( 'create_avif_image:gd_success', array( | |
| 1202 | + 'output_path' => $output_path, | |
| 1203 | + 'file_size' => file_exists( $output_path ) ? filesize( $output_path ) : 0, | |
| 1204 | + ) ); | |
| 1205 | + | |
| 1043 | 1206 | return $avif_image; |
| 1044 | 1207 | } |
| 1045 | 1208 | |
| 1046 | 1209 | /** |
| @@ -1046,17 +1209,27 @@ | ||
| 1046 | 1209 | /** |
| 1047 | 1210 | * Create the webp image |
| 1048 | 1211 | */ |
| 1049 | 1212 | private function create_webp_image( $file_path, $output_path, $quality ) { |
| 1050 | - if ( $this->create_image_with_wp_editor( $file_path, $output_path, $quality, 'image/webp' ) ) { | |
| 1213 | + $image = false; | |
| 1214 | + $mime_type = wp_get_image_mime( $file_path ); | |
| 1215 | + | |
| 1216 | + quickwebp_debug_log( 'create_webp_image:start', array( | |
| 1217 | + 'file_path' => $file_path, | |
| 1218 | + 'output_path' => $output_path, | |
| 1219 | + 'mime_type' => $mime_type, | |
| 1220 | + 'quality' => $quality, | |
| 1221 | + ) ); | |
| 1222 | + | |
| 1223 | + if ( ! $this->can_load_image_with_gd( $mime_type ) && $this->create_image_with_wp_editor( $file_path, $output_path, $quality, 'image/webp' ) ) { | |
| 1224 | + quickwebp_debug_log( 'create_webp_image:used_wp_editor', array( 'mime_type' => $mime_type ) ); | |
| 1051 | 1225 | return true; |
| 1052 | 1226 | } |
| 1053 | 1227 | |
| 1054 | - $image = false; | |
| 1055 | - $mime_type = wp_get_image_mime( $file_path ); | |
| 1056 | 1228 | $image = $this->create_gd_image_resource( $file_path, $mime_type ); |
| 1057 | 1229 | |
| 1058 | 1230 | if ( ! $image ) { |
| 1231 | + quickwebp_debug_log( 'create_webp_image:gd_load_failed', array( 'mime_type' => $mime_type ) ); | |
| 1059 | 1232 | return false; |
| 1060 | 1233 | } |
| 1061 | 1234 | |
| 1062 | 1235 | // Handle EXIF orientation for proper image rotation |
| @@ -1117,10 +1290,16 @@ | ||
| 1117 | 1290 | |
| 1118 | 1291 | $webp_image = imagewebp( $image, $output_path, $quality ); |
| 1119 | 1292 | imagedestroy( $image ); |
| 1120 | 1293 | if ( ! $webp_image ) { |
| 1294 | + quickwebp_debug_log( 'create_webp_image:gd_save_failed', array( 'output_path' => $output_path ) ); | |
| 1121 | 1295 | return false; |
| 1122 | 1296 | } |
| 1297 | + | |
| 1298 | + quickwebp_debug_log( 'create_webp_image:gd_success', array( | |
| 1299 | + 'output_path' => $output_path, | |
| 1300 | + 'file_size' => file_exists( $output_path ) ? filesize( $output_path ) : 0, | |
| 1301 | + ) ); | |
| 1123 | 1302 | |
| 1124 | 1303 | return $webp_image; |
| 1125 | 1304 | } |
| 1126 | 1305 | } |