From 87318402bed9eb83d0f605cd2f225662c729c049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Miko=C5=82ajczuk?= Date: Mon, 12 Jan 2015 14:21:06 +0100 Subject: [PATCH] CS-5603: Media Archive thumbnails are square, should be landscape or portrait fix regression. --- .../library/Newscoop/Image/ImageService.php | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/newscoop/library/Newscoop/Image/ImageService.php b/newscoop/library/Newscoop/Image/ImageService.php index 4d36917bd4..245c2bc947 100644 --- a/newscoop/library/Newscoop/Image/ImageService.php +++ b/newscoop/library/Newscoop/Image/ImageService.php @@ -65,7 +65,7 @@ public function __construct(array $config, \Doctrine\ORM\EntityManager $orm, $ca * * @return LocalImage */ - public function upload(UploadedFile $file, array $attributes, ImageInterface $image = null) + public function upload(UploadedFile $file, array $attributes, ImageInterface $image = null, $keepRatio = true) { $filesystem = new Filesystem(); $imagine = new Imagine(); @@ -119,8 +119,23 @@ public function upload(UploadedFile $file, array $attributes, ImageInterface $im $file->move($this->config['image_path'], $this->generateImagePath($image->getId(), $file->getClientOriginalExtension(), true)); $filesystem->chmod($imagePath, 0644); + if ($keepRatio) { + $ratioOrig = $width / $height; + $ratioNew = $this->config['thumbnail_max_size'] / $this->config['thumbnail_max_size']; + if ($ratioNew > $ratioOrig) { + $newImageWidth = $this->config['thumbnail_max_size'] * $ratioOrig; + $newImageHeight = $this->config['thumbnail_max_size']; + } else { + $newImageWidth = $this->config['thumbnail_max_size']; + $newImageHeight = $this->config['thumbnail_max_size'] / $ratioOrig; + } + } else { + $newImageWidth = $this->config['thumbnail_max_size']; + $newImageHeight = $this->config['thumbnail_max_size']; + } + $imagine->open($imagePath) - ->resize(new Box($this->config['thumbnail_max_size'], $this->config['thumbnail_max_size'])) + ->resize(new Box($newImageWidth, $newImageHeight)) ->save($thumbnailPath, array()); $filesystem->chmod($thumbnailPath, 0644); } catch (\Exceptiom $e) { @@ -293,29 +308,24 @@ private function generateImagePath($imageId, $extension, $olnyFileName = false) public function fillImage($image, $attributes) { $attributes = array_merge(array( - 'description' => '', - 'photographer' => '', - 'photographer_url' => '', - 'place' => '', 'date' => date('Y-m-d'), - 'content_type' => 'image/jeg', + 'content_type' => 'image/jpeg', 'user' => null, 'updated' => new \DateTime(), 'status' => 'unapproved', 'source' => 'local', - 'url' => '' ), $attributes); - $image->setDescription($attributes['description']); - $image->setPhotographer($attributes['photographer']); - $image->setPhotographerUrl($attributes['photographer_url']); - $image->setPlace($attributes['place']); + if (isset($attributes['description'])) { $image->setDescription($attributes['description']); } + if (isset($attributes['photographer'])) { $image->setPhotographer($attributes['photographer']); } + if (isset($attributes['photographer_url'])) { $image->setPhotographerUrl($attributes['photographer_url']); } + if (isset($attributes['place'])) { $image->setPlace($attributes['place']); } $image->setDate($attributes['date']); $image->setContentType($attributes['content_type']); $image->setUser($attributes['user']); $image->setUpdated($attributes['updated']); $image->setSource($attributes['source']); - $image->setUrl($attributes['url']); + if (isset($attributes['url'])) { $image->setUrl($attributes['url']); } if ($image->getUser() && $image->getUser()->isAdmin() == true) { $image->setStatus('approved'); @@ -466,12 +476,9 @@ public function getDefaultArticleImage($articleNumber) )); if ($image === null) { - $image = array_pop( - $this->orm->getRepository('Newscoop\Image\ArticleImage')->findBy( - array('articleNumber' => (int) $articleNumber), - array('number' => 'asc'), - 1 - ) + $image = $this->orm->getRepository('Newscoop\Image\ArticleImage')->findOneBy( + array('articleNumber' => (int) $articleNumber), + array('number' => 'asc') ); if ($image !== null) {