| 1 |
<?php |
| 2 |
namespace Depicter\Media\Image; |
| 3 |
|
| 4 |
|
| 5 |
use Depicter\Media\Uri; |
| 6 |
use Depicter\Media\Url; |
| 7 |
|
| 8 |
class ImageEditor |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Shorthand method for calling resize and crop methods for an image. |
| 12 |
* |
| 13 |
* @param string $file Path or url of file or attachment ID |
| 14 |
* @param int $resizeW Resize width |
| 15 |
* @param int $resizeH Resize height |
| 16 |
* @param int $cropW Crop with |
| 17 |
* @param int $cropH Crop height |
| 18 |
* @param array $args Resizing options |
| 19 |
* |
| 20 |
* @return mixed |
| 21 |
*/ |
| 22 |
public static function process( $file, $resizeW = null, $resizeH = null, $cropW = null, $cropH = null, $args = [] ){ |
| 23 |
try { |
| 24 |
$resizer = new Resizer( $file ); |
| 25 |
return $resizer->process( $resizeW, $resizeH, $cropW, $cropH, $args ); |
| 26 |
} catch( ImageEditorException $exception ) { |
| 27 |
return self::originalFile( $file ); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Resize an image. |
| 33 |
* |
| 34 |
* @param string $file Path or url of file or attachment ID |
| 35 |
* @param int $width Resize width |
| 36 |
* @param int $height Resize height |
| 37 |
* @param array $args Resizing options |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public static function resize( $file, $width = null, $height = null, $args = [] ){ |
| 42 |
try { |
| 43 |
$resizer = new Resizer( $file ); |
| 44 |
return $resizer->resize( $width, $height, $args ); |
| 45 |
} catch( ImageEditorException $exception ) { |
| 46 |
return self::originalFile( $file ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Crops an image |
| 52 |
* |
| 53 |
* @param string $file Path or url of file or attachment ID |
| 54 |
* @param int $width Width to crop |
| 55 |
* @param int $height Height to crop |
| 56 |
* @param array $args Cropping options |
| 57 |
* |
| 58 |
* @return mixed |
| 59 |
*/ |
| 60 |
public static function crop( $file, $width = null, $height = null, $args = [] ){ |
| 61 |
try { |
| 62 |
$resizer = new Resizer( $file ); |
| 63 |
return $resizer->crop( $width, $height, $args ); |
| 64 |
} catch( ImageEditorException $exception ) { |
| 65 |
return self::originalFile( $file ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* return original file |
| 71 |
* @param $file |
| 72 |
* |
| 73 |
* @return mixed|string |
| 74 |
*/ |
| 75 |
public static function originalFile( $file ) { |
| 76 |
if ( is_numeric( $file ) ) { |
| 77 |
return wp_get_attachment_image_src( $file, 'full')[0]; |
| 78 |
} else { |
| 79 |
$urlHandler = new Url(); |
| 80 |
if ( $urlHandler->isUrl( $file ) ) { |
| 81 |
return $file; |
| 82 |
} else { |
| 83 |
if ( file_exists( $file ) ) { |
| 84 |
return Uri::toUrl($file); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return ''; |
| 89 |
} |
| 90 |
} |
| 91 |
|