| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The class maps settings and options from imageproxy. |
| 5 |
* |
| 6 |
* @package \Optml\Inc |
| 7 |
* @author Optimole <friends@optimole.com> |
| 8 |
*/ |
| 9 |
class Optml_Image extends Optml_Resource { |
| 10 |
|
| 11 |
/** |
| 12 |
* Watermark for the image. |
| 13 |
* |
| 14 |
* @var Optml_Watermark Watermark. |
| 15 |
*/ |
| 16 |
public static $watermark = null; |
| 17 |
/** |
| 18 |
* Quality of the resulting image. |
| 19 |
* |
| 20 |
* @var Optml_Quality Quality; |
| 21 |
*/ |
| 22 |
public $quality = null; |
| 23 |
/** |
| 24 |
* Width of the resulting image. |
| 25 |
* |
| 26 |
* @var Optml_Width Width. |
| 27 |
*/ |
| 28 |
private $width = null; |
| 29 |
/** |
| 30 |
* Height of the resulting image. |
| 31 |
* |
| 32 |
* @var Optml_Height Height. |
| 33 |
*/ |
| 34 |
private $height = null; |
| 35 |
/** |
| 36 |
* Resize type for the image. |
| 37 |
* |
| 38 |
* @var Optml_Resize Resize details. |
| 39 |
*/ |
| 40 |
private $resize = null; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Optml_Image constructor. |
| 46 |
* |
| 47 |
* @param string $url Source image url. |
| 48 |
* @param array $args Transformation arguments. |
| 49 |
* |
| 50 |
* @throws \InvalidArgumentException In case that the url is not provided. |
| 51 |
*/ |
| 52 |
public function __construct( $url = '', $args = array(), $cache_buster = '' ) { |
| 53 |
parent::__construct( $url, $cache_buster ); |
| 54 |
|
| 55 |
$this->width->set( $args['width'] ); |
| 56 |
$this->height->set( $args['height'] ); |
| 57 |
|
| 58 |
if ( isset( $args['quality'] ) ) { |
| 59 |
$this->quality->set( $args['quality'] ); |
| 60 |
} |
| 61 |
|
| 62 |
if ( isset( $args['resize'] ) ) { |
| 63 |
$this->resize->set( $args['resize'] ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Set defaults for image transformations. |
| 69 |
*/ |
| 70 |
protected function set_defaults() { |
| 71 |
$this->width = new Optml_Width( 'auto' ); |
| 72 |
$this->height = new Optml_Height( 'auto' ); |
| 73 |
$this->resize = new Optml_Resize(); |
| 74 |
$this->quality = new Optml_Quality(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Return transformed url. |
| 79 |
* |
| 80 |
* @param array $params Either will be signed or not. |
| 81 |
* |
| 82 |
* @return string Transformed image url. |
| 83 |
*/ |
| 84 |
public function get_url( $params = [] ) { |
| 85 |
$path_parts = array(); |
| 86 |
|
| 87 |
$path_parts[] = $this->width->toString(); |
| 88 |
$path_parts[] = $this->height->toString(); |
| 89 |
$path_parts[] = $this->quality->toString(); |
| 90 |
|
| 91 |
if ( isset( $this->resize->get()['type'] ) ) { |
| 92 |
$path_parts[] = $this->resize->toString(); |
| 93 |
} |
| 94 |
if ( isset( $params['apply_watermark'] ) && $params['apply_watermark'] && is_array( self::$watermark->get() ) && isset( self::$watermark->get()['id'] ) && self::$watermark->get()['id'] > 0 ) { |
| 95 |
$path_parts[] = self::$watermark->toString(); |
| 96 |
} |
| 97 |
|
| 98 |
$path = '/' . $this->source_url; |
| 99 |
|
| 100 |
if ( isset( $params['format'] ) && ! empty( $params['format'] ) ) { |
| 101 |
$path = '/f:' . $params['format'] . '/' . $this->source_url; |
| 102 |
} |
| 103 |
|
| 104 |
$path = sprintf( '/%s%s', implode( '/', $path_parts ), $path ); |
| 105 |
|
| 106 |
$path = sprintf( '/%s%s', $this->get_domain_token() . $this->get_cache_buster(), $path ); |
| 107 |
|
| 108 |
return sprintf( '%s%s', Optml_Config::$service_url, $path ); |
| 109 |
|
| 110 |
} |
| 111 |
} |
| 112 |
|