| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
if ( ! class_exists( 'Kadence_Image_Processing' ) ) { |
| 6 |
class Kadence_Image_Processing_Exception extends Exception {} |
| 7 |
class Kadence_Image_Processing { |
| 8 |
protected static $instance = null; |
| 9 |
private $is_updating_backup_sizes = false; |
| 10 |
public $throwOnError = false; |
| 11 |
/** |
| 12 |
* No cloning allowed |
| 13 |
*/ |
| 14 |
private function __clone() {} |
| 15 |
/** |
| 16 |
* Singleton |
| 17 |
*/ |
| 18 |
public static function getInstance() { |
| 19 |
if ( is_null( self::$instance ) ) { |
| 20 |
self::$instance = new self(); |
| 21 |
} |
| 22 |
return self::$instance; |
| 23 |
} |
| 24 |
public function __construct() { |
| 25 |
add_filter( 'update_post_metadata', array( $this, 'filter_update_post_metadata' ), 10, 5 ); |
| 26 |
} |
| 27 |
/** |
| 28 |
* Filter the post meta data for backup sizes |
| 29 |
* |
| 30 |
* Unfortunately WordPress core is lacking hooks in its image resizing functions so we are reduced |
| 31 |
* to this hackery to detect when images are resized and previous versions are relegated to backup sizes. |
| 32 |
* |
| 33 |
*/ |
| 34 |
public function filter_update_post_metadata( $check, $object_id, $meta_key, $meta_value, $prev_value ) { |
| 35 |
if ( '_wp_attachment_backup_sizes' !== $meta_key ) { |
| 36 |
return $check; |
| 37 |
} |
| 38 |
|
| 39 |
$current_value = get_post_meta( $object_id, $meta_key, true ); |
| 40 |
|
| 41 |
if ( ! $current_value ) { |
| 42 |
$current_value = array(); |
| 43 |
} |
| 44 |
|
| 45 |
$diff = array_diff_key( $meta_value, $current_value ); |
| 46 |
|
| 47 |
if ( ! $diff ) { |
| 48 |
return $check; |
| 49 |
} |
| 50 |
|
| 51 |
$key = key( $diff ); |
| 52 |
$suffix = substr( $key, strrpos( $key, '-' ) + 1 ); |
| 53 |
|
| 54 |
$image_meta = self::kt_get_image_meta( $object_id ); |
| 55 |
|
| 56 |
foreach ( $image_meta['sizes'] as $size_name => $size ) { |
| 57 |
if ( 0 !== strpos( $size_name, 'kip-' ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
$meta_value[ $size_name . '-' . $suffix ] = $size; |
| 62 |
unset( $image_meta['sizes'][ $size_name ] ); |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! $this->is_updating_backup_sizes ) { |
| 66 |
$this->is_updating_backup_sizes = true; |
| 67 |
update_post_meta( $object_id, '_wp_attachment_backup_sizes', $meta_value ); |
| 68 |
wp_update_attachment_metadata( $object_id, $image_meta ); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
$this->is_updating_backup_sizes = false; |
| 73 |
|
| 74 |
return $check; |
| 75 |
} |
| 76 |
public function process( $id = null, $width = '', $height = '' ) { |
| 77 |
$width = absint( $width ); |
| 78 |
$height = absint( $height ); |
| 79 |
$sizes = array( array( $width, $height ) ); |
| 80 |
$retina = array(); |
| 81 |
if ( apply_filters( 'kadence_retina_support', true ) ) { |
| 82 |
$retina_w = $width*2; |
| 83 |
$retina_h = $height*2; |
| 84 |
$retina = array(array($retina_w, $retina_h)); |
| 85 |
} |
| 86 |
$sizes = array_reverse(array_merge($sizes,$retina)); |
| 87 |
// If we haven't created the image yet, lets do that now. |
| 88 |
$created = false; |
| 89 |
foreach ( $sizes as $size ) { |
| 90 |
if ( self::kt_does_size_already_exist_for_image( $id, $size[0], $size[1] ) ) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
|
| 94 |
if ( self::kt_is_size_larger_than_original( $id, $size[0], $size[1] ) ) { |
| 95 |
continue; |
| 96 |
} |
| 97 |
$item = array( |
| 98 |
'id' => $id, |
| 99 |
'width' => $size[0], |
| 100 |
'height' => $size[1], |
| 101 |
'crop' => true, |
| 102 |
); |
| 103 |
$created = $this->create_image_size( $item ); |
| 104 |
} |
| 105 |
return $created; |
| 106 |
} |
| 107 |
public function create_image_size( $item ) { |
| 108 |
try { |
| 109 |
$defaults = array( |
| 110 |
'id' => 0, |
| 111 |
'width' => 0, |
| 112 |
'height' => 0, |
| 113 |
'crop' => false, |
| 114 |
); |
| 115 |
$item = wp_parse_args( $item, $defaults ); |
| 116 |
|
| 117 |
$id = $item['id']; |
| 118 |
$width = $item['width']; |
| 119 |
$height = $item['height']; |
| 120 |
$crop = $item['crop']; |
| 121 |
|
| 122 |
if ( ! $width && ! $height ) { |
| 123 |
throw new Kadence_Image_Processing_Exception( "Invalid image dimensions" ); |
| 124 |
} |
| 125 |
$suffix = "{$width}x{$height}"; |
| 126 |
$image_meta = self::kt_get_image_meta($id ); |
| 127 |
if ( ! $image_meta ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
$img_path = get_attached_file( $id ); |
| 131 |
|
| 132 |
if ( ! $img_path ) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
$ext = substr($image_meta['file'], strrpos($image_meta['file'], ".")); |
| 137 |
|
| 138 |
$dst_rel_path = str_replace( '.' . $ext, '', $img_path ); |
| 139 |
$destfilename = "{$dst_rel_path}-{$suffix}.{$ext}"; |
| 140 |
|
| 141 |
|
| 142 |
if(file_exists( $destfilename )) { |
| 143 |
$filename = basename($image_meta['file'], $ext) . '-'.$width.'x'.$height.'' . $ext; |
| 144 |
$image_meta['sizes'][ $size_name ] = array( |
| 145 |
'file' => $filename, |
| 146 |
'width' => $width, |
| 147 |
'height' => $height, |
| 148 |
'mime-type' => isset($image_meta['sizes']['thumbnail']) ? $image_meta['sizes']['thumbnail']['mime-type'] : '', |
| 149 |
); |
| 150 |
wp_update_attachment_metadata( $id, $image_meta ); |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
$editor = wp_get_image_editor( $img_path ); |
| 156 |
|
| 157 |
if ( is_wp_error( $editor ) ) { |
| 158 |
throw new Kadence_Image_Processing_Exception( 'Unable to get WP_Image_Editor for file (is GD or ImageMagick installed?)' ); |
| 159 |
} |
| 160 |
|
| 161 |
if ( is_wp_error( $editor->resize( $width, $height, $crop ) ) ) { |
| 162 |
throw new Kadence_Image_Processing_Exception( 'Error resizing image'); |
| 163 |
} |
| 164 |
|
| 165 |
$resized_file = $editor->save(); |
| 166 |
if ( is_wp_error( $resized_file ) ) { |
| 167 |
throw new Kadence_Image_Processing_Exception( 'Unable to save resized image file' ); |
| 168 |
} |
| 169 |
|
| 170 |
$size_name = self::get_size_name( array( $width, $height) ); |
| 171 |
$image_meta['sizes'][ $size_name ] = array( |
| 172 |
'file' => $resized_file['file'], |
| 173 |
'width' => $resized_file['width'], |
| 174 |
'height' => $resized_file['height'], |
| 175 |
'mime-type' => $resized_file['mime-type'], |
| 176 |
); |
| 177 |
wp_update_attachment_metadata( $id, $image_meta ); |
| 178 |
|
| 179 |
return true; |
| 180 |
} |
| 181 |
catch (Kadence_Image_Processing_Exception $ex) { |
| 182 |
// Only for debugging |
| 183 |
if ($this->throwOnError) { |
| 184 |
// Bubble up exception. |
| 185 |
throw $ex; |
| 186 |
} else { |
| 187 |
// Return false, so that this patch is backwards-compatible. |
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
public static function kt_get_image_meta( $id ) { |
| 193 |
return wp_get_attachment_metadata( $id ); |
| 194 |
} |
| 195 |
public static function kt_is_size_larger_than_original($id, $width, $height) { |
| 196 |
$image_meta = self::kt_get_image_meta( $id ); |
| 197 |
// no size? then lets try |
| 198 |
if ( ! isset( $image_meta['width'] ) || ! isset( $image_meta['height'] ) ) { |
| 199 |
return true; |
| 200 |
} |
| 201 |
if ( $width > $image_meta['width'] || $height > $image_meta['height'] ) { |
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
return false; |
| 206 |
} |
| 207 |
public static function kt_does_size_already_exist_for_image( $id, $width, $height ) { |
| 208 |
$image_meta = self::kt_get_image_meta( $id ); |
| 209 |
$size_name = self::get_size_name( array($width, $height)); |
| 210 |
return isset( $image_meta['sizes'][ $size_name ] ); |
| 211 |
} |
| 212 |
public static function get_size_name( $size ) { |
| 213 |
return 'kip-' . $size[0] . 'x' . $size[1]; |
| 214 |
} |
| 215 |
} |
| 216 |
Kadence_Image_Processing::getInstance(); |
| 217 |
} |