| 1 |
<?php |
| 2 |
|
| 3 |
use Intervention\Image\ImageManager; |
| 4 |
|
| 5 |
/** |
| 6 |
* The core functionality for image optimizing. |
| 7 |
* |
| 8 |
* @link http://webdeclic.com |
| 9 |
* @since 1.0.0 |
| 10 |
* |
| 11 |
* @package Quickwebp |
| 12 |
* @subpackage Quickwebp/admin |
| 13 |
*/ |
| 14 |
class Quickwebp_Image_Optimizer { |
| 15 |
|
| 16 |
/** |
| 17 |
* The ID of this plugin. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
* @access private |
| 21 |
* @var string $plugin_name The ID of this plugin. |
| 22 |
*/ |
| 23 |
private $plugin_name; |
| 24 |
|
| 25 |
/** |
| 26 |
* The version of this plugin. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @access private |
| 30 |
* @var string $version The current version of this plugin. |
| 31 |
*/ |
| 32 |
private $version; |
| 33 |
|
| 34 |
/** |
| 35 |
* Initialize the class and set its properties. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @param string $plugin_name The name of this plugin. |
| 39 |
* @param string $version The version of this plugin. |
| 40 |
*/ |
| 41 |
public function __construct( $plugin_name, $version ) { |
| 42 |
|
| 43 |
$this->plugin_name = $plugin_name; |
| 44 |
$this->version = $version; |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Optimize an image added in wp_media |
| 50 |
*/ |
| 51 |
public function image_optimizition( $file ) { |
| 52 |
|
| 53 |
$settings = $this->get_settings(); |
| 54 |
|
| 55 |
$image_file = $this->file_is_image( $file, $settings ); |
| 56 |
if ( ! $image_file ) { |
| 57 |
return $file; |
| 58 |
} |
| 59 |
|
| 60 |
if ( $settings['quickwebp_settings_conversion'] != '1' ) { |
| 61 |
return $image_file; |
| 62 |
} |
| 63 |
|
| 64 |
$extension_to_use = $this->image_extension_loaded(); |
| 65 |
if ( ! $extension_to_use ) { |
| 66 |
return $image_file; |
| 67 |
} |
| 68 |
|
| 69 |
$manager = new ImageManager(array('driver'=>$extension_to_use)); |
| 70 |
$image = $manager->make($image_file['tmp_name']); |
| 71 |
$height = $image->height(); |
| 72 |
$width = $image->width(); |
| 73 |
|
| 74 |
if ( $width > $settings['quickwebp_settings_conversion_max_width'] ) { |
| 75 |
$image->widen( $settings['quickwebp_settings_conversion_max_width'] ); |
| 76 |
} |
| 77 |
|
| 78 |
if ( $height > $settings['quickwebp_settings_conversion_max_height'] ) { |
| 79 |
$image->heighten( $settings['quickwebp_settings_conversion_max_height'] ); |
| 80 |
} |
| 81 |
|
| 82 |
$image->sharpen($settings['quickwebp_settings_conversion_sharpen']); |
| 83 |
$image->save( $image_file['tmp_name'], $settings['quickwebp_settings_conversion_quality'], 'webp' ); |
| 84 |
|
| 85 |
$size_before = $image_file['size']; |
| 86 |
$size_after = $image->filesize(); |
| 87 |
$mime = $image->mime(); |
| 88 |
|
| 89 |
$image_file['size'] = $size_after; |
| 90 |
$image_file['type'] = $mime; |
| 91 |
|
| 92 |
return $image_file; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the package used by the server |
| 97 |
* |
| 98 |
*/ |
| 99 |
public function image_extension_loaded() { |
| 100 |
|
| 101 |
if ( extension_loaded('imagick') ) { |
| 102 |
return 'imagick'; |
| 103 |
} |
| 104 |
|
| 105 |
if ( extension_loaded('gd') ) { |
| 106 |
return 'gd'; |
| 107 |
} |
| 108 |
|
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Check if the file is an image |
| 114 |
* |
| 115 |
*/ |
| 116 |
public function file_is_image( $file, $settings ) { |
| 117 |
|
| 118 |
$file_name = isset($file['name']) ? $file['name'] : ''; |
| 119 |
$file_type = isset($file['type']) ? $file['type'] : ''; |
| 120 |
$file_tmp_name = isset($file['tmp_name']) ? $file['tmp_name'] : ''; |
| 121 |
$file_error = isset($file['error']) ? $file['error'] : ''; |
| 122 |
$file_size = isset($file['size']) ? $file['size'] : ''; |
| 123 |
|
| 124 |
if ( empty($file_tmp_name) ) { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
$is_image = wp_getimagesize($file_tmp_name); |
| 129 |
if ( ! $is_image ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
$name = $file_name; |
| 134 |
|
| 135 |
if ( $settings['quickwebp_settings_cleanup'] ) { |
| 136 |
$name = quickwebp_sanitize_name($file_name); |
| 137 |
} |
| 138 |
|
| 139 |
return array( |
| 140 |
'original_name' => $file_name, |
| 141 |
'name' => $name, |
| 142 |
'type' => $file_type, |
| 143 |
'tmp_name' => $file_tmp_name, |
| 144 |
'error' => $file_error, |
| 145 |
'size' => $file_size |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the optimization settings |
| 151 |
*/ |
| 152 |
public function get_settings() { |
| 153 |
|
| 154 |
return array( |
| 155 |
'quickwebp_settings_conversion' => get_option('quickwebp_settings_conversion', quickwebp_settings_default('quickwebp_settings_conversion') ), |
| 156 |
'quickwebp_settings_conversion_max_width' => get_option('quickwebp_settings_conversion_max_width', quickwebp_settings_default('quickwebp_settings_conversion_max_width') ), |
| 157 |
'quickwebp_settings_conversion_max_height' => get_option('quickwebp_settings_conversion_max_height', quickwebp_settings_default('quickwebp_settings_conversion_max_height') ), |
| 158 |
'quickwebp_settings_conversion_quality' => get_option('quickwebp_settings_conversion_quality', quickwebp_settings_default('quickwebp_settings_conversion_quality') ), |
| 159 |
'quickwebp_settings_conversion_sharpen' => get_option('quickwebp_settings_conversion_sharpen', quickwebp_settings_default('quickwebp_settings_conversion_sharpen') ), |
| 160 |
'quickwebp_settings_completion' => get_option('quickwebp_settings_completion', quickwebp_settings_default('quickwebp_settings_completion') ), |
| 161 |
'quickwebp_settings_completion_options' => get_option('quickwebp_settings_completion_options', quickwebp_settings_default('quickwebp_settings_completion_options') ), |
| 162 |
'quickwebp_settings_cleanup' => get_option('quickwebp_settings_cleanup', quickwebp_settings_default('quickwebp_settings_cleanup') ) |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Add data to the attachment |
| 168 |
*/ |
| 169 |
public function add_data_to_attachment( $metadata, $attachment_id, $context ) { |
| 170 |
|
| 171 |
$settings = $this->get_settings(); |
| 172 |
if ( $settings['quickwebp_settings_completion'] != '1' ) { |
| 173 |
return $metadata; |
| 174 |
} |
| 175 |
|
| 176 |
if ( $context != 'create' ) { |
| 177 |
return $metadata; |
| 178 |
} |
| 179 |
|
| 180 |
if ( isset($_FILES['async-upload']['original_name']) ) { |
| 181 |
|
| 182 |
$original_name = sanitize_text_field( $_FILES['async-upload']['original_name'] ); |
| 183 |
$original_name = pathinfo( $original_name, PATHINFO_FILENAME ); |
| 184 |
|
| 185 |
$post_arr = array( |
| 186 |
'ID' => $attachment_id, |
| 187 |
'meta_input' => array() |
| 188 |
); |
| 189 |
|
| 190 |
$completion_options = is_array($settings['quickwebp_settings_completion_options']) ? $settings['quickwebp_settings_completion_options'] : array(); |
| 191 |
|
| 192 |
if ( in_array( 'title', $completion_options ) ) { |
| 193 |
$post_arr['post_title'] = $original_name; |
| 194 |
} |
| 195 |
|
| 196 |
if ( in_array( 'caption', $completion_options ) ) { |
| 197 |
$post_arr['post_excerpt'] = $original_name; |
| 198 |
} |
| 199 |
|
| 200 |
if ( in_array( 'alt', $completion_options ) ) { |
| 201 |
$post_arr['meta_input']['_wp_attachment_image_alt'] = $original_name; |
| 202 |
} |
| 203 |
|
| 204 |
if ( in_array( 'description', $completion_options ) ) { |
| 205 |
$post_arr['post_content'] = $original_name; |
| 206 |
} |
| 207 |
|
| 208 |
wp_update_post( $post_arr ); |
| 209 |
} |
| 210 |
|
| 211 |
return $metadata; |
| 212 |
} |
| 213 |
|
| 214 |
} |