PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.3
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.3
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 / classes / image / image.php

image.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.3, at classes/image/image.php

189 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Classes\Image;
4
5 use ImageOptimization\Classes\Image\Exceptions\Invalid_Image_Exception;
6 use WP_Post;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class Image {
13 public const SIZE_FULL = 'full';
14 private const SUPPORTED_MIME_TYPES = [ 'image/jpeg', 'image/png', 'image/webp', 'image/gif', 'image/avif' ];
15
16 // Used for error messages
17 private const SUPPORTED_FORMATS = [ 'jpeg', 'png', 'webp', 'gif', 'avif' ];
18 private const MIME_TYPES_CANNOT_BE_OPTIMIZED = [ 'image/avif' ];
19 private const FORMATS_CANNOT_BE_OPTIMIZED = [ 'avif' ];
20
21 protected int $image_id;
22 protected $attachment_object;
23
24 /**
25 * Returns attachment post id.
26 *
27 * @return int
28 */
29 public function get_id(): int {
30 return $this->image_id;
31 }
32
33 /**
34 * Returns file URL for a specific image size.
35 *
36 * @param string $image_size Image size (e. g. 'full', 'thumbnail', etc)
37 * @return string|null
38 */
39 public function get_url( string $image_size ): ?string {
40 $image_data = wp_get_attachment_image_src( $this->image_id, $image_size );
41
42 if ( empty( $image_data ) ) {
43 return null;
44 }
45
46 return $image_data[0];
47 }
48
49 /**
50 * Returns absolute file path for a specific image size.
51 *
52 * @param string $image_size Image size (e. g. 'full', 'thumbnail', etc)
53 * @return string|null
54 */
55 public function get_file_path( string $image_size ): ?string {
56 if ( 'full' === $image_size ) {
57 $path = get_attached_file( $this->image_id );
58 return $path ?? null;
59 }
60
61 $path_data = image_get_intermediate_size( $this->image_id, $image_size );
62
63 if ( empty( $path_data ) ) {
64 return null;
65 }
66
67 return sprintf(
68 '%s/%s',
69 wp_get_upload_dir()['basedir'],
70 $path_data['path']
71 );
72 }
73
74 /**
75 * Returns true if an image marked as optimized.
76 *
77 * @return bool
78 */
79 public function is_optimized(): bool {
80 $meta = new Image_Meta( $this->image_id );
81
82 return $meta->get_status() === Image_Status::OPTIMIZED;
83 }
84
85 /**
86 * Returns true if an image can be restored from the backups.
87 *
88 * @return bool
89 */
90 public function can_be_restored(): bool {
91 $meta = new Image_Meta( $this->image_id );
92
93 return (bool) count( $meta->get_image_backup_paths() );
94 }
95
96 /**
97 * Returns image's mime type.
98 *
99 * @return string
100 */
101 public function get_mime_type(): string {
102 return $this->attachment_object->post_mime_type;
103 }
104
105 /**
106 * Returns an original attachment WP_Post object.
107 *
108 * @return WP_Post
109 */
110 public function get_attachment_object(): WP_Post {
111 return $this->attachment_object;
112 }
113
114 /**
115 * Updates WP_Post fields of the attachment.
116 *
117 * @return bool True if the post was updated successfully, false otherwise.
118 */
119 public function update_attachment( array $update_query ): bool {
120 global $wpdb;
121
122 // Must use $wpdb here as `wp_update_post()` doesn't allow to rewrite guid.
123 $result = $wpdb->update(
124 $wpdb->posts,
125 $update_query,
126 [ 'ID' => $this->image_id ]
127 );
128
129 if ( 0 !== $result ) {
130 $this->attachment_object = get_post( $this->image_id );
131 return true;
132 }
133
134 return false;
135 }
136
137 /**
138 * Returns the list of mime types supported by the plugin.
139 *
140 * @return string[]
141 */
142 public static function get_supported_mime_types(): array {
143 return self::SUPPORTED_MIME_TYPES;
144 }
145
146 /**
147 * Returns the list of formats types supported by the plugin.
148 *
149 * @return string[]
150 */
151 public static function get_supported_formats(): array {
152 return self::SUPPORTED_FORMATS;
153 }
154
155 /**
156 * Returns the list of mime types that are supported by the plugin, but cannot be optimized
157 *
158 * @return string[]
159 */
160 public static function get_mime_types_cannot_be_optimized(): array {
161 return self::MIME_TYPES_CANNOT_BE_OPTIMIZED;
162 }
163
164 /**
165 * Returns the list of formats that are supported by the plugin, but cannot be optimized
166 *
167 * @return string[]
168 */
169 public static function get_formats_cannot_be_optimized(): array {
170 return self::FORMATS_CANNOT_BE_OPTIMIZED;
171 }
172
173 /**
174 * @throws Invalid_Image_Exception
175 */
176 public function __construct( int $image_id ) {
177 $this->image_id = $image_id;
178 $this->attachment_object = get_post( $image_id );
179
180 if ( ! $this->attachment_object ) {
181 throw new Invalid_Image_Exception( "There is no entity with id '$image_id'" );
182 }
183
184 if ( ! wp_attachment_is_image( $this->attachment_object ) ) {
185 throw new Invalid_Image_Exception( "Post '$image_id' is not an image" );
186 }
187 }
188 }
189