| 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 |
* Optml_Image constructor. |
| 45 |
* |
| 46 |
* @param string $url Source image url. |
| 47 |
* @param array $args Transformation arguments. |
| 48 |
* |
| 49 |
* @throws \InvalidArgumentException In case that the url is not provided. |
| 50 |
*/ |
| 51 |
public function __construct( $url = '', $args = [], $cache_buster = '' ) { |
| 52 |
parent::__construct( $url, $cache_buster ); |
| 53 |
|
| 54 |
$this->width->set( $args['width'] ); |
| 55 |
$this->height->set( $args['height'] ); |
| 56 |
|
| 57 |
if ( isset( $args['quality'] ) ) { |
| 58 |
$this->quality->set( $args['quality'] ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( isset( $args['resize'] ) ) { |
| 62 |
$this->resize->set( $args['resize'] ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Set defaults for image transformations. |
| 68 |
*/ |
| 69 |
protected function set_defaults() { |
| 70 |
$this->width = new Optml_Width( 'auto' ); |
| 71 |
$this->height = new Optml_Height( 'auto' ); |
| 72 |
$this->resize = new Optml_Resize(); |
| 73 |
$this->quality = new Optml_Quality(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Return transformed url. |
| 78 |
* |
| 79 |
* @param array $params Image transforms parameters. |
| 80 |
* |
| 81 |
* @return string Transformed image url. |
| 82 |
*/ |
| 83 |
public function get_url( $params = [] ) { |
| 84 |
$path_params = $this->get_path_params( $params ); |
| 85 |
|
| 86 |
if ( $this->is_dam_url() ) { |
| 87 |
return $this->get_dam_url( $path_params ); |
| 88 |
} |
| 89 |
|
| 90 |
$path = $path_params . '/' . $this->source_url; |
| 91 |
|
| 92 |
return sprintf( '%s%s', Optml_Config::$service_url, $path ); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Check if this contains the DAM flag. |
| 98 |
* |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
public function is_dam_url() { |
| 102 |
return strpos( $this->source_url, Optml_Dam::URL_DAM_FLAG ) !== false; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get the DAM image URL. |
| 107 |
* |
| 108 |
* @param string $path_params Path parameters. |
| 109 |
* |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
private function get_dam_url( $path_params ) { |
| 113 |
$url = $this->source_url; |
| 114 |
|
| 115 |
// Remove DAM flag. |
| 116 |
$url = str_replace( Optml_Dam::URL_DAM_FLAG, '', $url ); |
| 117 |
|
| 118 |
// Split the path params. |
| 119 |
$path_params_parts = explode( '/', $path_params ); |
| 120 |
|
| 121 |
foreach ( $path_params_parts as $param ) { |
| 122 |
if ( empty( $param ) ) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
// Split into parts. |
| 127 |
$param_parts = explode( ':', $param ); |
| 128 |
|
| 129 |
// Check if the param is width or height and if it's auto. |
| 130 |
// Rely on the initial URL as it generates the correct image size. |
| 131 |
if ( in_array( $param_parts[0], ['w', 'h'], true ) ) { |
| 132 |
if ( $param_parts[1] === 'auto' ) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// if the param exists we do a non-greedy replace for /$param:$value/ with the updated value. |
| 138 |
if ( strpos( $url, '/' . $param_parts[0] . ':' ) !== false ) { |
| 139 |
$url = preg_replace( '/\/' . $param_parts[0] . ':.*?\//', '/' . $param . '/', $url ); |
| 140 |
|
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
// Otherwise, we add it before the /q: param. |
| 145 |
$url = str_replace( '/q:', '/' . $param . '/q:', $url ); |
| 146 |
} |
| 147 |
|
| 148 |
return $url; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get transform params as string. |
| 153 |
* |
| 154 |
* @param array $params Image transforms parameters. |
| 155 |
* |
| 156 |
* @return string Transform parameters string to use in the URL. |
| 157 |
*/ |
| 158 |
private function get_path_params( $params = [] ) { |
| 159 |
$path_parts = []; |
| 160 |
|
| 161 |
$path_parts[] = $this->width->toString(); |
| 162 |
$path_parts[] = $this->height->toString(); |
| 163 |
$path_parts[] = $this->quality->toString(); |
| 164 |
|
| 165 |
if ( isset( $this->resize->get()['type'] ) ) { |
| 166 |
$path_parts[] = $this->resize->toString(); |
| 167 |
} |
| 168 |
if ( isset( $params['apply_watermark'] ) && $params['apply_watermark'] && is_array( self::$watermark->get() ) && isset( self::$watermark->get()['id'] ) && self::$watermark->get()['id'] > 0 ) { |
| 169 |
$path_parts[] = self::$watermark->toString(); |
| 170 |
} |
| 171 |
|
| 172 |
$path = ''; |
| 173 |
|
| 174 |
if ( isset( $params['format'] ) && ! empty( $params['format'] ) ) { |
| 175 |
$path = '/f:' . $params['format'] . $path; |
| 176 |
} |
| 177 |
|
| 178 |
if ( isset( $params['strip_metadata'] ) && '0' === $params['strip_metadata'] ) { |
| 179 |
$path = '/sm:' . $params['strip_metadata'] . $path; |
| 180 |
} |
| 181 |
|
| 182 |
if ( isset( $params['ignore_avif'] ) && $params['ignore_avif'] === true ) { |
| 183 |
$path = '/ig:avif' . $path; |
| 184 |
} |
| 185 |
|
| 186 |
if ( apply_filters( 'optml_keep_copyright', false ) === true ) { |
| 187 |
$path = '/keep_copyright:true' . $path; |
| 188 |
} |
| 189 |
|
| 190 |
$path = sprintf( '/%s%s', implode( '/', $path_parts ), $path ); |
| 191 |
|
| 192 |
$cache_buster = $this->get_cache_buster(); |
| 193 |
if ( $cache_buster !== false ) { |
| 194 |
$path = sprintf( '/%s%s', 'cb:' . $cache_buster, $path ); |
| 195 |
} |
| 196 |
|
| 197 |
return $path; |
| 198 |
} |
| 199 |
} |
| 200 |
|