public function postAction(Request $request, $width = 250, $height = 200)
 {
     $image = new Image();
     /** @var $file File */
     $file = $request->files->get('file');
     /** @var $imagine ImagineInterface */
     $imagine = $this->get('hoyes_image_manager.imagine');
     $em = $this->getDoctrine()->getManager();
     $repo = $em->getRepository('HoyesImageManagerBundle:Image');
     $hash = md5(file_get_contents($file->getPathname()));
     if (!($image = $repo->findOneByHash($hash))) {
         $data_path = $this->container->getParameter('hoyes_image_manager.data_dir');
         $ext = $file->guessExtension();
         $image_file = $imagine->open($file->getPathname());
         $size = $image_file->getSize();
         $max_width = $this->container->getParameter('hoyes_image_manager.max_width');
         $max_height = $this->container->getParameter('hoyes_image_manager.max_height');
         if ($size->getWidth() > $max_width || $size->getHeight() > $max_height) {
             $image_file = $image_file->thumbnail(new \Imagine\Image\Box($max_width, $max_height), ImageInterface::THUMBNAIL_INSET);
             $size = $image_file->getSize();
         }
         $image = new Image();
         $image->setExtension($ext)->setHash($hash)->setWidth($size->getWidth())->setHeight($size->getHeight())->setType($file->getMimeType());
         $em->persist($image);
         $em->flush();
         $image_file->save($data_path . DIRECTORY_SEPARATOR . $hash, array('format' => $ext));
     }
     /** @var $cache ImageCache */
     $cache = $this->get('hoyes_image_manager.image_cache');
     list($thumb_width, $thumb_height) = $cache->changeDimensions($image, $width, $height, false);
     $url = $this->generateUrl('hoyes_image_manager_image_both', array('hash' => $hash, 'width' => $width, 'height' => $height, 'ext' => $image->getExtension()));
     $full_url = $this->generateUrl('hoyes_image_manager_image_both', array('hash' => $hash, 'width' => 1024, 'height' => 768, 'ext' => $image->getExtension()));
     $data = array('url' => $url, 'full_url' => $full_url, 'width' => $thumb_width, 'height' => $thumb_height, 'id' => $image->getId());
     $response = new JsonResponse();
     $response->setData($data);
     return $response;
 }
 public function getImageUrl(Image $image, $width = null, $height = null, $crop = false, $absolute = false)
 {
     $hash = $image->getHash();
     if (!$width && !$height) {
         return $this->router->generate('hoyes_image_manager_image', array('ext' => $image->getExtension(), 'hash' => $hash), $absolute);
     } elseif ($width && !$height) {
         return $this->router->generate('hoyes_image_manager_image_width', array('ext' => $image->getExtension(), 'hash' => $hash, 'width' => $width), $absolute);
     } elseif (!$width && $height) {
         return $this->router->generate('hoyes_image_manager_image_height', array('ext' => $image->getExtension(), 'hash' => $hash, 'height' => $height), $absolute);
     } elseif ($width && $height && !$crop) {
         return $this->router->generate('hoyes_image_manager_image_both', array('ext' => $image->getExtension(), 'hash' => $hash, 'width' => $width, 'height' => $height), $absolute);
     } else {
         return $this->router->generate('hoyes_image_manager_image_crop', array('ext' => $image->getExtension(), 'hash' => $hash, 'width' => $width, 'height' => $height), $absolute);
     }
 }
 private function getResizeDimensions(Image $image, $width, $height, $fit)
 {
     if (is_null($width) && is_null($height)) {
         return array($image->getWidth(), $image->getHeight());
     } elseif (is_null($width)) {
         $ratio = $height / $image->getHeight();
     } elseif (is_null($height)) {
         $ratio = $width / $image->getWidth();
     } else {
         $ratios = array($width / $image->getWidth(), $height / $image->getHeight());
         if ($fit) {
             $ratio = max($ratios);
         } else {
             $ratio = min($ratios);
         }
     }
     if ($ratio > 1.0) {
         $ratio = 1.0;
     }
     $new_width = $image->getWidth() * $ratio;
     $new_height = $image->getHeight() * $ratio;
     return array(round($new_width), round($new_height));
 }