PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.5
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.5
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 / functions / process.php

process.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.5, at inc/functions/process.php

171 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
3
4 /**
5 * Process an image with Imagify.
6 *
7 * @since 1.0
8 *
9 * @param string $file_path Absolute path to the image file.
10 * @param bool $backup Force a backup of the original file.
11 * @param int $optimization_level The optimization level (2=ultra, 1=aggressive, 0=normal).
12 * @param array $resize The resize parameters (with & height).
13 * @param bool $keep_exif To keep exif data or not
14 * @return obj|array Error message | Optimized image data
15 */
16 function do_imagify( $file_path, $args = array() ) {
17 $errors = new WP_Error();
18 $args = array_merge(
19 array(
20 'backup' => get_imagify_option( 'backup', false ),
21 'optimization_level' => get_imagify_option( 'optimization_level', 1 ),
22 'resize' => array(),
23 'keep_exif' => get_imagify_option( 'exif', false ),
24 'context' => 'wp',
25 'original_size' => 0
26 ),
27 $args
28 );
29
30 /**
31 * Filter the attachment path
32 *
33 * @since 1.2
34 *
35 * @param string $file_path The attachment path
36 */
37 $file_path = apply_filters( 'imagify_file_path', $file_path );
38
39 // Check if the Imagify servers & the API are accessible
40 if ( ! is_imagify_servers_up() ) {
41 $errors->add( 'api_server_down', __( 'Sorry, our servers are temporarily unaccessible. Please, try again in a couple of minutes.', 'imagify' ) );
42 return $errors;
43 }
44
45 // Check if external HTTP requests are blocked.
46 if ( is_imagify_blocked() ) {
47 $errors->add( 'http_block_external', __( 'External HTTP requests are blocked', 'imagify' ) );
48 return $errors;
49 }
50
51 // Check that file path isn't empty
52 if ( empty( $file_path ) ) {
53 $errors->add( 'empty_path', __( 'File path is empty', 'imagify' ) );
54 return $errors;
55 }
56
57 // Check that the file exists
58 if ( ! file_exists( $file_path ) || ! is_file( $file_path ) ) {
59 $errors->add( 'file_not_found', sprintf( __( 'Could not find %s', 'imagify' ), $file_path ) );
60 }
61
62 // Check that the file is writable
63 if ( ! is_writable( dirname( $file_path ) ) ) {
64 $errors->add( 'not_writable', sprintf( __( "%s is not writable", 'imagify' ), dirname( $file_path ) ) );
65 return $errors;
66 }
67
68 // Get file size
69 $file_size = file_exists( $file_path ) ? filesize( $file_path ) : 0;
70
71 // Check that file exists
72 if ( 0 == $file_size ) {
73 $errors->add( 'image_not_found', sprintf( __( 'Skipped (%s), image not found.', 'imagify' ), size_format( $file_size ) ) );
74 return $errors;
75 }
76
77 /**
78 * Fires before to optimize the Image with Imagify.
79 *
80 * @since 1.0
81 *
82 * @param string $file_path Absolute path to the image file.
83 * @param bool $backup Force a backup of the original file.
84 */
85 do_action( 'before_do_imagify', $file_path, $args['backup'] );
86
87 // Send image for optimization and fetch the response
88 $response = upload_imagify_image(
89 array(
90 'image' => curl_file_create( $file_path ),
91 'data' => json_encode(
92 array(
93 'aggressive' => ( 1 === (int) $args['optimization_level'] ) ? true : false,
94 'ultra' => ( 2 === (int) $args['optimization_level'] ) ? true : false,
95 'resize' => $args['resize'],
96 'keep_exif' => $args['keep_exif'],
97 'context' => $args['context'],
98 'original_size' => $args['original_size']
99 )
100 )
101 )
102 );
103
104 // Check status code
105 if( is_wp_error( $response ) ) {
106 $errors->add( 'api_error', $response->get_error_message() );
107 return $errors;
108 }
109
110 // Create a backup file
111 if ( 'wp' === $args['context'] && $args['backup'] ) {
112 $backup_path = get_imagify_attachment_backup_path( $file_path );
113 $backup_path_info = pathinfo( $backup_path );
114
115 wp_mkdir_p( $backup_path_info['dirname'] );
116
117 // TO DO - check and send a error message if the backup can't be created
118 @copy( $file_path, $backup_path );
119 imagify_chmod_file( $backup_path );
120 }
121
122 if ( ! function_exists( 'download_url' ) ) {
123 require( ABSPATH . 'wp-admin/includes/file.php' );
124 }
125
126 $temp_file = download_url( $response->image );
127
128 if ( is_wp_error( $temp_file ) ) {
129 $errors->add( 'temp_file_not_found', $temp_file->get_error_message() );
130 return $errors;
131 }
132
133 @rename( $temp_file, $file_path );
134 imagify_chmod_file( $file_path );
135
136 // If temp file still exists, delete it
137 if ( file_exists( $temp_file ) ) {
138 unlink( $temp_file );
139 }
140
141 /**
142 * Fires after to optimize the Image with Imagify.
143 *
144 * @since 1.0
145 *
146 * @param string $file_path Absolute path to the image file.
147 * @param bool $backup Force a backup of the original file.
148 */
149 do_action( 'after_do_imagify', $file_path, $args['backup'] );
150
151 return $response;
152 }
153
154 /**
155 * Run an async job to optimize images in background
156 *
157 * @param $body (array) Contains the usual $_POST
158 *
159 * @since 1.4
160 **/
161 function imagify_do_async_job( $body ) {
162 $args = array(
163 'timeout' => 0.01,
164 'blocking' => false,
165 'body' => $body,
166 'cookies' => $_COOKIE,
167 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
168 );
169
170 wp_remote_post( admin_url( 'admin-ajax.php' ), $args );
171 }