63 $this->pivot = $point;
107 throw new \Intervention\Image\Exception\InvalidArgumentException(
108 "Width or height needs to be defined."
113 $dominant_w_size = clone $this;
114 $dominant_w_size->resizeHeight(
$height, $callback);
115 $dominant_w_size->resizeWidth(
$width, $callback);
118 $dominant_h_size = clone $this;
119 $dominant_h_size->resizeWidth(
$width, $callback);
120 $dominant_h_size->resizeHeight(
$height, $callback);
124 $this->
set($dominant_h_size->width, $dominant_h_size->height);
126 $this->
set($dominant_w_size->width, $dominant_w_size->height);
139 private function resizeWidth(
$width, Closure $callback =
null)
141 $constraint = $this->getConstraint($callback);
144 $max_width = $constraint->getSize()->getWidth();
145 $max_height = $constraint->getSize()->getHeight();
151 $this->width = (
$width > $max_width) ? $max_width :
$width;
157 $h = intval(round($this->width / $constraint->getSize()->getRatio()));
160 $this->height = ($h > $max_height) ? $max_height : $h;
175 private function resizeHeight(
$height, Closure $callback =
null)
177 $constraint = $this->getConstraint($callback);
180 $max_width = $constraint->getSize()->getWidth();
181 $max_height = $constraint->getSize()->getHeight();
193 $w = intval(round($this->height * $constraint->getSize()->getRatio()));
196 $this->width = ($w > $max_width) ? $max_width : $w;
213 $x = $this->pivot->x - $size->pivot->x;
214 $y = $this->pivot->y - $size->pivot->y;
216 return new Point($x, $y);
225 public function fit(
Size $size, $position =
'center')
228 $auto_height = clone $size;
230 $auto_height->
resize($this->width,
null,
function ($constraint) {
231 $constraint->aspectRatio();
235 if ($auto_height->fitsInto($this)) {
237 $size = $auto_height;
242 $auto_width = clone $size;
244 $auto_width->resize(
null, $this->height,
function ($constraint) {
245 $constraint->aspectRatio();
251 $this->
align($position);
252 $size->align($position);
266 return ($this->width <= $size->width) && ($this->height <= $size->height);
278 public function align($position, $offset_x = 0, $offset_y = 0)
280 switch (strtolower($position)) {
287 $x = intval($this->width / 2);
293 $x = $this->width - $offset_x;
303 $y = intval($this->height / 2);
311 $x = $this->width - $offset_x;
312 $y = intval($this->height / 2);
318 $y = $this->height - $offset_y;
322 case 'bottom-center':
323 case 'bottom-middle':
324 case 'center-bottom':
325 case 'middle-bottom':
326 $x = intval($this->width / 2);
327 $y = $this->height - $offset_y;
332 $x = $this->width - $offset_x;
333 $y = $this->height - $offset_y;
338 case 'center-center':
339 case 'middle-middle':
340 $x = intval($this->width / 2);
341 $y = intval($this->height / 2);
352 $this->pivot->setPosition($x, $y);
363 private function getConstraint(Closure $callback =
null)
367 if (is_callable($callback)) {
368 $callback($constraint);