PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.0
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.0
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / modules / optimization / classes / validate-image.php

validate-image.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.0, at modules/optimization/classes/validate-image.php

103 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Modules\Optimization\Classes;
4
5 use ImageOptimization\Classes\Image\{
6 Exceptions\Invalid_Image_Exception,
7 Image,
8 Image_Meta,
9 WP_Image_Meta
10 };
11 use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error;
12 use ImageOptimization\Classes\File_System\File_System;
13 use ImageOptimization\Classes\File_Utils;
14 use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_Validation_Error;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // Exit if accessed directly.
18 }
19
20 class Validate_Image {
21 public const MAX_FILE_SIZE = 10 * 1024 * 1024;
22
23 /**
24 * Returns true if $image_id provided associated with an image that can be optimized.
25 *
26 * @param int $image_id Attachment id.
27 *
28 * @return true
29 * @throws Image_Validation_Error
30 * @throws Invalid_Image_Exception
31 */
32 public static function is_valid( int $image_id ): bool {
33 $attachment_object = get_post( $image_id );
34
35 if ( ! $attachment_object ) {
36 throw new Image_Validation_Error(
37 __( 'Can\'t optimize this file. If the issue persists, Contact Support', 'image-optimization' )
38 );
39 }
40
41 if (
42 ! wp_attachment_is_image( $attachment_object ) ||
43 ! in_array( $attachment_object->post_mime_type, Image::get_supported_mime_types(), true ) ||
44 (
45 in_array( $attachment_object->post_mime_type, Image::get_mime_types_cannot_be_optimized(), true ) &&
46 ! get_post_meta( $image_id, Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, true )
47 )
48 ) {
49 throw new Image_Validation_Error( self::prepare_supported_formats_list_error() );
50 }
51
52 if ( ! file_exists( get_attached_file( $image_id ) ) ) {
53 throw new Image_Validation_Error(
54 esc_html__( 'File is missing. Verify the upload', 'image-optimization' )
55 );
56 }
57
58 $wp_meta = new WP_Image_Meta( $image_id );
59
60 try {
61 $image_size = $wp_meta->get_file_size( Image::SIZE_FULL )
62 ?? File_System::size( ( new Image( $image_id ) )->get_file_path( Image::SIZE_FULL ) );
63 } catch ( File_System_Operation_Error $e ) {
64 throw new Image_Validation_Error(
65 esc_html__( 'File is missing. Verify the upload', 'image-optimization' )
66 );
67 }
68
69 if ( $image_size > self::MAX_FILE_SIZE ) {
70 throw new Image_Validation_Error(
71 sprintf(
72 __( 'File is too large. Max size is %s', 'image-optimization' ),
73 File_Utils::format_file_size( self::MAX_FILE_SIZE, 0 ),
74 )
75 );
76 }
77
78 return true;
79 }
80
81 /**
82 * Prepares the error message for the unsupported file formats.
83 *
84 * @return string The error message.
85 */
86 private static function prepare_supported_formats_list_error(): string {
87 $formats = Image::get_supported_formats();
88 $formats = array_filter(
89 $formats,
90 fn ( $format) => ! in_array( $format, Image::get_formats_cannot_be_optimized(), true )
91 );
92 $last_item = strtoupper( array_pop( $formats ) );
93
94 $formats_list = join( ', ', array_map( 'strtoupper', $formats ) );
95
96 return sprintf(
97 __( 'Wrong file format. Only %1$s, or %2$s are accepted', 'image-optimization' ),
98 $formats_list,
99 $last_item
100 );
101 }
102 }
103