Skip to content

Commit

Permalink
Return more image data on upload (#127)
Browse files Browse the repository at this point in the history
* fix typo

* remove opened memcached port

* return more image data on upload

- mime
- width
- height
- color
- size

* update readme
  • Loading branch information
talyguryn authored Jul 14, 2018
1 parent 6056be3 commit d74eca8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down
2 changes: 1 addition & 1 deletion capella/src/ImageProcessing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 38 additions & 12 deletions capella/src/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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)
Expand All @@ -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;
}

/**
Expand All @@ -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)
Expand All @@ -215,7 +241,7 @@ public function uploadFile($data)
*
* @throws \Exception
*
* @return string - uploaded image uri
* @return array - uploaded image data
*
*/
public function uploadLink($url)
Expand Down
21 changes: 13 additions & 8 deletions capella/src/controller/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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']
]);
}
}
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ version: '2'
services:
memcached:
image: memcached:alpine
ports:
- 11211:11211
php:
build: docker/php5.6-fpm
links:
Expand Down

0 comments on commit d74eca8

Please sign in to comment.