PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / 3rd-party / nextgen-gallery / classes / DynamicThumbnails.php

DynamicThumbnails.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at inc/3rd-party/nextgen-gallery/classes/DynamicThumbnails.php

132 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\ThirdParty\NGG;
3
4 use Imagify\Traits\InstanceGetterTrait;
5
6 /**
7 * Class that handles the optimization of thumbnails dynamically generated.
8 *
9 * @since 1.9
10 * @author Grégory Viguier
11 */
12 class DynamicThumbnails {
13 use InstanceGetterTrait;
14
15 /**
16 * The queue containing the sizes, grouped by image ID.
17 *
18 * @var array
19 * @since 1.9
20 * @access protected
21 * @author Grégory Viguier
22 */
23 protected static $sizes = [];
24
25 /**
26 * A list of NGG image objects, grouped by image ID.
27 *
28 * @var array
29 * @since 1.9
30 * @access protected
31 * @author Grégory Viguier
32 */
33 protected static $images = [];
34
35 /**
36 * Add a dynamically generated thumbnail to the background process queue.
37 *
38 * @since 1.9
39 * @access public
40 * @author Grégory Viguier
41 *
42 * @param object $image A NGG image object.
43 * @param string $size The thumbnail size name.
44 */
45 public function push_to_queue( $image, $size ) {
46 static $done = false;
47
48 if ( empty( $image->pid ) ) {
49 // WUT?
50 return;
51 }
52
53 if ( empty( static::$sizes[ $image->pid ] ) ) {
54 static::$sizes[ $image->pid ] = [];
55 }
56
57 static::$sizes[ $image->pid ][] = $size;
58
59 if ( empty( static::$images[ $image->pid ] ) ) {
60 static::$images[ $image->pid ] = $image;
61 }
62
63 if ( $done ) {
64 return;
65 }
66
67 $done = true;
68
69 add_action( 'shutdown', [ $this, 'optimize' ], 555 ); // Must come before 666 (see Imagify_Abstract_Background_Process->init()).
70 }
71
72 /**
73 * Launch the optimizations.
74 *
75 * @since 1.9
76 * @access public
77 * @author Grégory Viguier
78 */
79 public function optimize() {
80 if ( empty( static::$sizes ) ) {
81 return;
82 }
83
84 foreach ( static::$sizes as $image_id => $sizes ) {
85 if ( empty( static::$images[ $image_id ] ) ) {
86 continue;
87 }
88
89 $sizes = array_filter( $sizes );
90
91 if ( empty( $sizes ) ) {
92 continue;
93 }
94
95 $process = imagify_get_optimization_process( static::$images[ $image_id ], 'ngg' );
96
97 if ( ! $process->is_valid() || ! $process->get_media()->is_supported() ) {
98 continue;
99 }
100
101 $data = $process->get_data();
102
103 if ( ! $data->is_optimized() ) {
104 // The main image is not optimized.
105 continue;
106 }
107
108 $sizes = array_unique( $sizes );
109
110 foreach ( $sizes as $i => $size ) {
111 $size_status = $data->get_size_data( $size, 'success' );
112
113 if ( $size_status ) {
114 // This thumbnail has already been processed.
115 unset( $sizes[ $i ] );
116 }
117 }
118
119 if ( empty( $sizes ) ) {
120 continue;
121 }
122
123 $optimization_level = $process->get_data()->get_optimization_level();
124 $args = [
125 'hook_suffix' => 'optimize_generated_image',
126 ];
127
128 $process->optimize_sizes( $sizes, $optimization_level, $args );
129 }
130 }
131 }
132