/** * @param string | array $data * @throws Exception\InvalidArgumentException * @return Box */ public function getImageBox($data) { if (!(is_array($data) && array_key_exists('width', $data) && array_key_exists('height', $data)) && !preg_match('/^(?P<width>[0-9]+)x(?P<height>[0-9]+)$/', $data, $data)) { throw new Exception\InvalidArgumentException("Could not retrieve width and height from data for ImageBox"); } $width = $data['width']; $height = $data['height']; $box = new Box($width, $height); if (array_key_exists('strategy', $data)) { if ($data['strategy'] == 'widen') { $box->widen($width); } else { if ($data['strategy'] == 'heighten') { $box->heighten($height); } } } return $box; }
/** * {@inheritDoc} */ public function normalizeImage($name, $content, $width, $height) { $extension = $this->getExtension($name); $image = $this->imagine->load($content); $size = $image->getSize(); $box = new \Imagine\Image\Box($size->getWidth(), $size->getHeight()); if ($size->getWidth() >= $size->getHeight() && $size->getWidth() > $width) { return $image->resize($box->widen($width))->get($extension); } elseif ($size->getWidth() < $size->getHeight() && $size->getHeight() > $height) { return $image->resize($box->heighten($height))->get($extension); } return $image->get($extension); }
/** * @dataProvider getDimensionsAndTargets * * @param integer $width * @param integer $height * @param integer $targetWidth * @param integer $targetHeight */ public function testShouldResizeToTargetWidthAndHeight($width, $height, $targetWidth, $targetHeight) { $box = new Box($width, $height); $expected = new Box($targetWidth, $targetHeight); $this->assertEquals($expected, $box->widen($targetWidth)); $this->assertEquals($expected, $box->heighten($targetHeight)); }
/** * Normalize image to given width or height * * @param string $imagePath * @param array $options */ private function normalizeImage($imagePath) { $options = $this->container->getParameter('neutron_form.plupload.configs'); $imagine = $this->container->get('imagine'); $image = $imagine->open($imagePath); $size = $image->getSize(); $box = new \Imagine\Image\Box($size->getWidth(), $size->getHeight()); if ($size->getWidth() >= $size->getHeight() && $size->getWidth() > $options['normalize_width']) { $image->resize($box->widen($options['normalize_width']))->save($imagePath); } elseif ($size->getWidth() < $size->getHeight() && $size->getHeight() > $options['normalize_height']) { $image->resize($box->heighten($options['normalize_height']))->save($imagePath); } }
/** * {@inheritdoc} */ public function widen($width) { $box = $this->box->widen($width); return new self($box->getWidth(), $box->getHeight()); }