From d74eca8d2341ed293548b3339ef7e7b511f6b7e9 Mon Sep 17 00:00:00 2001 From: Taly Date: Sat, 14 Jul 2018 14:40:55 +0300 Subject: [PATCH] Return more image data on upload (#127) * fix typo * remove opened memcached port * return more image data on upload - mime - width - height - color - size * update readme --- README.md | 11 +++++++- capella/src/ImageProcessing.php | 2 +- capella/src/Uploader.php | 50 +++++++++++++++++++++++++-------- capella/src/controller/Form.php | 21 ++++++++------ docker-compose.yml | 2 -- 5 files changed, 62 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5a5523d..a6dbc18 100644 --- a/README.md +++ b/README.md @@ -78,13 +78,22 @@ Each response will have at least `success` and `message` fields. | `message` | String | `Image uploaded` | | `id` | String | Image id | | `url` | String | Full link to the uploaded image | +| `mime` | String | Mime type of the uploaded image | +| `width` | Integer | Image's width | +| `height` | Integer | Image's height | +| `color` | String | Average hex color of the image | +| `size` | Integer | Image's size in bytes | ```json { "success": true, "message": "Image uploaded", "id": "69256e83-66e1-449a-b0c2-5414d332e3a6", - "url": "https://capella.pics/69256e83-66e1-449a-b0c2-5414d332e3a6" + "url": "https://capella.pics/69256e83-66e1-449a-b0c2-5414d332e3a6", + "width": 1080, + "height": 700, + "color": "#9d908d", + "size": "176769" } ``` diff --git a/capella/src/ImageProcessing.php b/capella/src/ImageProcessing.php index 48c501d..eb95e1c 100644 --- a/capella/src/ImageProcessing.php +++ b/capella/src/ImageProcessing.php @@ -131,7 +131,7 @@ public function resizeImage($resizeWidth, $resizeHeight = 0) public function pixelizeImage($pixels) { if (!$pixels) { - throw new \Exception('Uncorrect ratio'); + throw new \Exception('Incorrect ratio'); } if ($this->width > $this->height) { diff --git a/capella/src/Uploader.php b/capella/src/Uploader.php index 4e7af60..022f1f1 100644 --- a/capella/src/Uploader.php +++ b/capella/src/Uploader.php @@ -94,7 +94,7 @@ protected function isValidSize($size) * * @throws \Exception * - * @return string - path to saved file + * @return array - saved file data * */ protected function saveFileToUploadDir($filepath) @@ -125,7 +125,33 @@ protected function saveFileToUploadDir($filepath) $image->setImageCompressionQuality(90); $image->writeImage($path); - return $path; + /** Save image resolution */ + $width = $image->getImageWidth(); + $height = $image->getImageHeight(); + + /** Get image size in bytes */ + $imageSize = strlen($image->getImageBlob()); + + /** + * Finding main color + * 1) resize to 1x1 image with gaussian blur + * 2) get color of top left pixel + * 3) convert color from rgb to hex + */ + $image->resizeImage(1, 1, Imagick::FILTER_GAUSSIAN, 1); + $color = $image->getImagePixelColor(1, 1)->getColor(); + $colorHex = sprintf("#%02x%02x%02x", $color['r'], $color['g'], $color['b']); + + $imageData = [ + 'filepath' => $path, + 'width' => $width, + 'height' => $height, + 'color' => $colorHex, + 'mime' => 'image/' . self::TARGET_EXT, + 'size' => $imageSize, + ]; + + return $imageData; } /** @@ -140,19 +166,19 @@ protected function saveFileToUploadDir($filepath) * * @throws \Exception * - * @return string - img url + * @return array - image data * */ protected function saveImage($file) { /** Copy temp file to upload dir */ - $filepath = $this->saveFileToUploadDir($file); - $label = explode('.', basename($filepath))[0]; + $imageData = $this->saveFileToUploadDir($file); + $label = explode('.', basename($imageData['filepath']))[0]; /** Get image's URL by id */ - $imgURI = \Methods::getImageUri($label); + $imageData['link'] = \Methods::getImageUri($label); - return $imgURI; + return $imageData; } /** @@ -164,7 +190,7 @@ protected function saveImage($file) * * @throws \Exception * - * @return string - uploaded image uri + * @return array - uploaded image data * */ protected function upload($file, $size, $mime) @@ -182,9 +208,9 @@ protected function upload($file, $size, $mime) } /** Upload file and get its ID */ - $imgURI = $this->saveImage($file); + $imageData = $this->saveImage($file); - return $imgURI; + return $imageData; } /** @@ -194,7 +220,7 @@ protected function upload($file, $size, $mime) * * @throws \Exception * - * @return string - uploaded image uri + * @return array - uploaded image data * */ public function uploadFile($data) @@ -215,7 +241,7 @@ public function uploadFile($data) * * @throws \Exception * - * @return string - uploaded image uri + * @return array - uploaded image data * */ public function uploadLink($url) diff --git a/capella/src/controller/Form.php b/capella/src/controller/Form.php index 04e4bc6..f224903 100644 --- a/capella/src/controller/Form.php +++ b/capella/src/controller/Form.php @@ -50,9 +50,9 @@ protected function uploadFile() $uploader = new \Uploader(); try { - $link = $uploader->uploadFile($_FILES['file']); + $imageData = $uploader->uploadFile($_FILES['file']); - $this->returnImageLink($link); + $this->returnImageData($imageData); } catch (\Exception $e) { HTTP\Response::BadRequest(); @@ -78,9 +78,9 @@ protected function uploadLink() $uploader = new \Uploader(); try { - $link = $uploader->uploadLink($_POST['link']); + $imageData = $uploader->uploadLink($_POST['link']); - $this->returnImageLink($link); + $this->returnImageData($imageData); } catch (\Exception $e) { HTTP\Response::BadRequest(); @@ -94,16 +94,21 @@ protected function uploadLink() /** * Return success result with image link * - * @param string $link + * @param string $imageData */ - protected function returnImageLink($link) + protected function returnImageData($imageData) { HTTP\Response::OK(); API\Response::success([ 'message' => 'Image uploaded', - 'id' => basename($link), - 'url' => $link + 'id' => basename($imageData['link']), + 'url' => $imageData['link'], + 'mime' => $imageData['mime'], + 'width' => $imageData['width'], + 'height' => $imageData['height'], + 'color' => $imageData['color'], + 'size' => $imageData['size'] ]); } } diff --git a/docker-compose.yml b/docker-compose.yml index 1e6574b..9ff6328 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,8 +3,6 @@ version: '2' services: memcached: image: memcached:alpine - ports: - - 11211:11211 php: build: docker/php5.6-fpm links: