| 1 |
<?php |
| 2 |
namespace Depicter\Media\Image; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use WP_Image_Editor; |
| 6 |
|
| 7 |
|
| 8 |
class FileEdit extends File implements FileEditInterface |
| 9 |
{ |
| 10 |
/** |
| 11 |
* The builtin editor |
| 12 |
* |
| 13 |
* @var \WP_Error|\WP_Image_Editor |
| 14 |
*/ |
| 15 |
protected $editor; |
| 16 |
|
| 17 |
/** |
| 18 |
* Crop starting point for x in pixels. |
| 19 |
* |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
protected $x; |
| 23 |
|
| 24 |
/** |
| 25 |
* Crop starting point for y in pixels. |
| 26 |
* |
| 27 |
* @var int |
| 28 |
*/ |
| 29 |
protected $y; |
| 30 |
|
| 31 |
/** |
| 32 |
* Determines if further process is not allowed |
| 33 |
* |
| 34 |
* @var bool |
| 35 |
*/ |
| 36 |
protected $isPaused = false; |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Constructor. |
| 41 |
* |
| 42 |
* @param string $path Path to the file or attachment id to load. |
| 43 |
* |
| 44 |
* @throws ImageEditorException |
| 45 |
*/ |
| 46 |
public function __construct( $path ){ |
| 47 |
parent::__construct( $path ); |
| 48 |
$this->reset(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Loads image from $this->file into new editor. |
| 53 |
* |
| 54 |
* @throws ImageEditorException |
| 55 |
*/ |
| 56 |
public function reset(){ |
| 57 |
if( $this->getExtension() === 'svg' ){ |
| 58 |
$this->isPaused = true; |
| 59 |
throw new ImageEditorException( 'SVG file provided' ); |
| 60 |
} |
| 61 |
|
| 62 |
$this->editor = wp_get_image_editor( $this->file ); |
| 63 |
if ( is_wp_error( $this->editor ) ) { |
| 64 |
throw new ImageEditorException( $this->editor->get_error_message() ); |
| 65 |
} |
| 66 |
|
| 67 |
$this->orig_w = $this->getSize('width'); |
| 68 |
$this->orig_h = $this->getSize('height'); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Resize the current image. |
| 74 |
* |
| 75 |
* @param int $width Resize width |
| 76 |
* @param int $height Resize height |
| 77 |
* @param array $args Resizing options |
| 78 |
* |
| 79 |
* @return $this |
| 80 |
* @throws ImageEditorException |
| 81 |
*/ |
| 82 |
public function resize( $width = null, $height = null, $args = [] ){ |
| 83 |
if( $this->isPaused ){ |
| 84 |
return $this; |
| 85 |
} |
| 86 |
|
| 87 |
$this->resized_w = $width; |
| 88 |
$this->resized_h = $height; |
| 89 |
|
| 90 |
if( !empty( $args['upscale'] ) ){ |
| 91 |
add_filter( 'image_resize_dimensions', [ $this, 'allowUpscale' ], 10, 6 ); |
| 92 |
} |
| 93 |
|
| 94 |
// check if upscale is false and resize with and height are greater than original sizes |
| 95 |
if ( empty( $args['upscale'] ) && $this->orig_w <= $width && $this->orig_h <= $height ) { |
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
$resized = $this->editor->resize( $width, $height ); |
| 100 |
if ( is_wp_error( $resized ) ) { |
| 101 |
throw new ImageEditorException( $resized->get_error_message() ); |
| 102 |
} |
| 103 |
|
| 104 |
if( !empty( $args['upscale'] ) ){ |
| 105 |
remove_filter( 'image_resize_dimensions', [ $this, 'allowUpscale' ] ); |
| 106 |
} |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Crops the images |
| 113 |
* |
| 114 |
* @param int $width Width to crop |
| 115 |
* @param int $height Height to crop |
| 116 |
* @param array $args Cropping options |
| 117 |
* |
| 118 |
* @return $this |
| 119 |
* @throws ImageEditorException |
| 120 |
*/ |
| 121 |
public function crop( $width = null, $height = null, $args = [] ){ |
| 122 |
if( $this->isPaused ){ |
| 123 |
return $this; |
| 124 |
} |
| 125 |
|
| 126 |
$x = isset( $args['x'] ) ? $args['x'] : null; |
| 127 |
$y = isset( $args['y'] ) ? $args['y'] : null; |
| 128 |
|
| 129 |
if( isset( $args['focalX'] ) ){ |
| 130 |
$this->focal[0] = $args['focalX']; |
| 131 |
} |
| 132 |
if( isset( $args['focalY'] ) ){ |
| 133 |
$this->focal[1] = $args['focalY']; |
| 134 |
} |
| 135 |
|
| 136 |
$mediaWidth = $this->getSize('width' ); |
| 137 |
$mediaHeight = $this->getSize('height' ); |
| 138 |
|
| 139 |
if( $width > $mediaWidth ){ |
| 140 |
$width = $mediaWidth; |
| 141 |
} |
| 142 |
if( $height > $mediaHeight ){ |
| 143 |
$height = $mediaHeight; |
| 144 |
} |
| 145 |
|
| 146 |
// check if crop size is equal to media size then stop the cropping process |
| 147 |
if ( $width == $mediaWidth && $height == $mediaHeight ) { |
| 148 |
return $this; |
| 149 |
} |
| 150 |
|
| 151 |
// if $x was not defined, calculate it based on focal point |
| 152 |
if( is_null( $x ) ){ |
| 153 |
$x = $mediaWidth * $this->focal[0] - $width / 2; |
| 154 |
} |
| 155 |
$x = min($mediaWidth - $width , max(0, $x) ); |
| 156 |
|
| 157 |
// if $y was not defined, calculate it based on focal point |
| 158 |
if( is_null( $y ) ){ |
| 159 |
$y = $mediaHeight * $this->focal[1] - $height / 2; |
| 160 |
} |
| 161 |
$y = min($mediaHeight - $height, max(0, $y) ); |
| 162 |
|
| 163 |
$this->x = $x; |
| 164 |
$this->y = $y; |
| 165 |
|
| 166 |
$this->crop_w = $width; |
| 167 |
$this->crop_h = $height; |
| 168 |
|
| 169 |
$cropped = $this->editor->crop( $x, $y, (int) $width, (int) $height ); |
| 170 |
if ( is_wp_error( $cropped ) ) { |
| 171 |
throw new ImageEditorException( $cropped->get_error_message() ); |
| 172 |
} |
| 173 |
|
| 174 |
return $this; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Shorthand method for calling resize and crop methods. |
| 179 |
* |
| 180 |
* @param int $resizeW Resize width |
| 181 |
* @param int $resizeH Resize height |
| 182 |
* @param int $cropW Crop with |
| 183 |
* @param int $cropH Crop height |
| 184 |
* @param array $args Resizing options |
| 185 |
* |
| 186 |
* @return $this |
| 187 |
* @throws ImageEditorException |
| 188 |
*/ |
| 189 |
public function process( $resizeW = null, $resizeH = null, $cropW = null, $cropH = null, $args = [] ){ |
| 190 |
if( $this->isPaused ){ |
| 191 |
return $this; |
| 192 |
} |
| 193 |
|
| 194 |
if( isset( $args['focalX'] ) ){ |
| 195 |
$this->focal[0] = $args['focalX']; |
| 196 |
} |
| 197 |
if( isset( $args['focalY'] ) ){ |
| 198 |
$this->focal[1] = $args['focalY']; |
| 199 |
} |
| 200 |
|
| 201 |
if( $resizeW || $resizeH ){ |
| 202 |
|
| 203 |
$resized = $this->resize( $resizeW, $resizeH, $args ); |
| 204 |
if ( is_wp_error( $resized ) ) { |
| 205 |
throw new ImageEditorException( $resized->get_error_message() ); |
| 206 |
} |
| 207 |
} |
| 208 |
if( $cropW || $cropH ){ |
| 209 |
$cropped = $this->crop( $cropW, $cropH, $args ); |
| 210 |
if ( is_wp_error( $cropped ) ) { |
| 211 |
throw new ImageEditorException( $cropped->get_error_message() ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return $this; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Sets Image Compression quality on a 1-100% scale. |
| 220 |
* |
| 221 |
* @param int $quality Compression Quality. Range: [1,100] |
| 222 |
* @return true|\WP_Error True if set successfully; WP_Error on failure. |
| 223 |
*/ |
| 224 |
public function setQuality( $quality = null ){ |
| 225 |
return $this->editor->set_quality( $quality ); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Saves current image to file. |
| 230 |
* |
| 231 |
* @param null $filename |
| 232 |
* @param string $mime_type |
| 233 |
* |
| 234 |
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string} |
| 235 |
*/ |
| 236 |
public function save( $filename = null, $mime_type = null ){ |
| 237 |
if( $this->isPaused ){ |
| 238 |
throw new ImageEditorException( __( 'File cannot be resized.' ) ); |
| 239 |
} |
| 240 |
if( ! $filename ){ |
| 241 |
$filename = $this->generateFilename(); |
| 242 |
} |
| 243 |
return $this->editor->save( $filename, $mime_type ); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Get current image size |
| 248 |
* |
| 249 |
* @param string $dimension |
| 250 |
* |
| 251 |
* @return array|null |
| 252 |
*/ |
| 253 |
public function getSize( $dimension = null ){ |
| 254 |
if( ! $dimension ){ |
| 255 |
return $this->editor->get_size(); |
| 256 |
} |
| 257 |
return !empty( $this->editor->get_size()[ $dimension ] ) ? $this->editor->get_size()[ $dimension ] : null; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Retrieves the WP_Image_Editor instance |
| 262 |
* |
| 263 |
* @return WP_Error|WP_Image_Editor |
| 264 |
*/ |
| 265 |
public function editor(){ |
| 266 |
return $this->editor; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Allows to scale up an image |
| 271 |
* |
| 272 |
* @param $default |
| 273 |
* @param $orig_w |
| 274 |
* @param $orig_h |
| 275 |
* @param $dest_w |
| 276 |
* @param $dest_h |
| 277 |
* @param $crop |
| 278 |
* |
| 279 |
* @return array |
| 280 |
*/ |
| 281 |
public function allowUpscale( $default, $orig_w, $orig_h, $dest_w, $dest_h, $crop ){ |
| 282 |
// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h |
| 283 |
return array( 0, 0, 0, 0, $dest_w, $dest_h, $orig_w, $orig_h ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* |
| 288 |
* @return string |
| 289 |
*/ |
| 290 |
protected function getSuffix(){ |
| 291 |
$suffix = $this->editor->get_suffix(); |
| 292 |
$focalSuffix = $this->focalPointSuffix(); |
| 293 |
|
| 294 |
return $focalSuffix . $suffix; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Builds an output filename based on current file, and adding proper suffix |
| 299 |
* |
| 300 |
* @since 3.5.0 |
| 301 |
* |
| 302 |
* @param string $suffix |
| 303 |
* @param string $dest_path |
| 304 |
* @param string $extension |
| 305 |
* @return string filename |
| 306 |
*/ |
| 307 |
public function generateFilename( $suffix = null, $dest_path = null, $extension = null ) { |
| 308 |
// $suffix will be appended to the destination filename, just before the extension. |
| 309 |
if ( ! $suffix ) { |
| 310 |
$suffix = $this->getSuffix(); |
| 311 |
} |
| 312 |
|
| 313 |
$dir = pathinfo( $this->file, PATHINFO_DIRNAME ); |
| 314 |
$ext = pathinfo( $this->file, PATHINFO_EXTENSION ); |
| 315 |
|
| 316 |
$name = wp_basename( $this->file, ".$ext" ); |
| 317 |
$new_ext = strtolower( $extension ? $extension : $ext ); |
| 318 |
|
| 319 |
if ( ! is_null( $dest_path ) ) { |
| 320 |
if ( ! wp_is_stream( $dest_path ) ) { |
| 321 |
$_dest_path = realpath( $dest_path ); |
| 322 |
if ( $_dest_path ) { |
| 323 |
$dir = $_dest_path; |
| 324 |
} |
| 325 |
} else { |
| 326 |
$dir = $dest_path; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}"; |
| 331 |
} |
| 332 |
|
| 333 |
} |
| 334 |
|