| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_Resize |
| 5 |
*/ |
| 6 |
class Optml_Resize extends Optml_Property_Type { |
| 7 |
|
| 8 |
|
| 9 |
/** |
| 10 |
* Resize the image while keeping aspect ratio to fit given size. |
| 11 |
*/ |
| 12 |
const RESIZE_FILL = 'fill'; |
| 13 |
/** |
| 14 |
* Resize the image while keeping aspect ratio |
| 15 |
* to fill given size and cropping projecting parts. |
| 16 |
*/ |
| 17 |
const RESIZE_FIT = 'fit'; |
| 18 |
/** |
| 19 |
* Crops the image to a given size. |
| 20 |
*/ |
| 21 |
const RESIZE_CROP = 'crop'; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Top edge. |
| 26 |
*/ |
| 27 |
const GRAVITY_NORTH = 'no'; |
| 28 |
/** |
| 29 |
* Bottom Edge. |
| 30 |
*/ |
| 31 |
const GRAVITY_SOUTH = 'so'; |
| 32 |
/** |
| 33 |
* Right Edge. |
| 34 |
*/ |
| 35 |
const GRAVITY_EAST = 'ea'; |
| 36 |
/** |
| 37 |
* Left edge. |
| 38 |
*/ |
| 39 |
const GRAVITY_WEST = 'we'; |
| 40 |
|
| 41 |
/** |
| 42 |
* Top right corner. |
| 43 |
*/ |
| 44 |
const GRAVITY_NORTH_WEST = 'noea'; |
| 45 |
/** |
| 46 |
* Top left corner. |
| 47 |
*/ |
| 48 |
const GRAVITY_NORTH_EAST = 'nowe'; |
| 49 |
/** |
| 50 |
* Bottom right corner. |
| 51 |
*/ |
| 52 |
const GRAVITY_SOUTH_EAST = 'soea'; |
| 53 |
/** |
| 54 |
* Bottom left corner. |
| 55 |
*/ |
| 56 |
const GRAVITY_SOUTH_WEST = 'sowe'; |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Center |
| 61 |
*/ |
| 62 |
const GRAVITY_CENTER = 'ce'; |
| 63 |
/** |
| 64 |
* Detects the most "interesting" section of the image and |
| 65 |
* considers it as the center of the resulting image |
| 66 |
*/ |
| 67 |
const GRAVITY_SMART = 'sm'; |
| 68 |
/** |
| 69 |
* Detects the most "interesting" section of the image and |
| 70 |
* considers it as the center of the resulting image |
| 71 |
*/ |
| 72 |
const GRAVITY_FOCUS_POINT = 'fp'; |
| 73 |
|
| 74 |
/** |
| 75 |
* Floating point numbers between 0 and 1 that define the coordinates of the resulting image for X axis. |
| 76 |
* |
| 77 |
* @var int Focus point X. |
| 78 |
*/ |
| 79 |
private $focus_point_x = 0; |
| 80 |
/** |
| 81 |
* Floating point numbers between 0 and 1 that define the coordinates of the resulting image for X axis. |
| 82 |
* |
| 83 |
* @var int Focus point Y. |
| 84 |
*/ |
| 85 |
private $focus_point_y = 0; |
| 86 |
|
| 87 |
/** |
| 88 |
* Resize type. |
| 89 |
* |
| 90 |
* @var string Resize type string. |
| 91 |
*/ |
| 92 |
private $resize_type = ''; |
| 93 |
/** |
| 94 |
* Should enlarge. |
| 95 |
* |
| 96 |
* @var bool Enlarge flag |
| 97 |
*/ |
| 98 |
private $enlarge = false; |
| 99 |
/** |
| 100 |
* Global default enlarge. |
| 101 |
* |
| 102 |
* @var bool Enlarge flag |
| 103 |
*/ |
| 104 |
public static $default_enlarge = false; |
| 105 |
|
| 106 |
/** |
| 107 |
* Gravity type. |
| 108 |
* |
| 109 |
* @var string Gravity type string. |
| 110 |
*/ |
| 111 |
private $gravity = ''; |
| 112 |
|
| 113 |
/** |
| 114 |
* Optml_Resize constructor. |
| 115 |
*/ |
| 116 |
public function __construct( $value = [] ) { |
| 117 |
$this->set( $value ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Set property value. |
| 122 |
* |
| 123 |
* @param mixed $value Value to set. |
| 124 |
*/ |
| 125 |
public function set( $value ) { |
| 126 |
$this->resize_type = isset( $value['type'] ) ? $value['type'] : ''; |
| 127 |
$this->enlarge = isset( $value['enlarge'] ) ? $value['enlarge'] : self::$default_enlarge; |
| 128 |
$this->gravity = isset( $value['gravity'] ) ? is_array( $value['gravity'] ) ? self::GRAVITY_FOCUS_POINT : $value['gravity'] : ''; |
| 129 |
if ( $this->gravity === self::GRAVITY_FOCUS_POINT ) { |
| 130 |
$this->focus_point_x = $value['gravity'][0]; |
| 131 |
$this->focus_point_y = $value['gravity'][1]; |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Return property value. |
| 138 |
* |
| 139 |
* @return mixed |
| 140 |
*/ |
| 141 |
public function get() { |
| 142 |
if ( empty( $this->resize_type ) ) { |
| 143 |
return []; |
| 144 |
} |
| 145 |
if ( empty( $this->gravity ) ) { |
| 146 |
return [ 'type' => $this->resize_type ]; |
| 147 |
} |
| 148 |
if ( $this->gravity === self::GRAVITY_FOCUS_POINT ) { |
| 149 |
return [ 'type' => $this->gravity, 'gravity' => [ $this->focus_point_x, $this->focus_point_y ] ]; |
| 150 |
} |
| 151 |
|
| 152 |
return [ |
| 153 |
'type' => $this->resize_type, |
| 154 |
'gravity' => $this->gravity, |
| 155 |
'enlarge' => $this->enlarge, |
| 156 |
]; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Return ImageProxy URL formatted string property. |
| 161 |
* |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
public function toString() { |
| 165 |
|
| 166 |
$resize = sprintf( 'rt:%s', $this->resize_type ); |
| 167 |
|
| 168 |
$resize .= sprintf( '/g:%s', $this->gravity ); |
| 169 |
|
| 170 |
if ( $this->gravity === self::GRAVITY_FOCUS_POINT ) { |
| 171 |
$resize .= sprintf( ':%s:%s', $this->focus_point_x, $this->focus_point_y ); |
| 172 |
} |
| 173 |
|
| 174 |
if ( $this->enlarge ) { |
| 175 |
$resize .= '/el:1'; |
| 176 |
} |
| 177 |
|
| 178 |
return $resize; |
| 179 |
} |
| 180 |
} |
| 181 |
|