|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FFCMS\Controllers\Media\Images; |
|
4
|
|
|
|
|
5
|
|
|
use FFCMS\Mappers; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Index Controller Class. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Vijay Mahrra <[email protected]> |
|
11
|
|
|
* @copyright (c) Copyright 2016 Vijay Mahrra |
|
12
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
|
13
|
|
|
*/ |
|
14
|
|
|
class Users |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* |
|
19
|
|
|
* @param \Base $f3 |
|
20
|
|
|
* @param array $params |
|
21
|
|
|
* @return void |
|
|
|
|
|
|
22
|
|
|
*/ |
|
23
|
|
|
public function profile(\Base $f3, array $params = []) |
|
24
|
|
|
{ |
|
25
|
|
|
// get asset table entry |
|
26
|
|
|
$asset = new Mappers\Assets; |
|
27
|
|
|
$asset->load(['users_uuid = ? AND ' . $asset->quotekey('key') . ' = ?', $params['uuid'], $params['key']]); |
|
28
|
|
|
if (null === $asset->uuid) { |
|
29
|
|
|
return $f3->status(404); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
// get image dimensions |
|
33
|
|
|
$max = $f3->get('assets.image.max'); |
|
34
|
|
|
$height = abs((int) $f3->get('REQUEST.height')); |
|
35
|
|
|
$width = abs((int) $f3->get('REQUEST.width')); |
|
36
|
|
|
|
|
37
|
|
|
if (empty($height) || empty($width)) { |
|
38
|
|
|
// resize on longest side |
|
39
|
|
|
if ($height > $width) { |
|
40
|
|
|
$width = $height; |
|
41
|
|
|
} elseif ($width > $height) { |
|
42
|
|
|
$height = $width; |
|
43
|
|
|
} else { |
|
44
|
|
|
// use default height/width if missing |
|
45
|
|
|
$height = $f3->get('assets.image.default.height'); |
|
46
|
|
|
$width = $f3->get('assets.image.default.width'); |
|
47
|
|
|
} |
|
48
|
|
|
} elseif ($width > $max['width'] || $height > $max['height']) { |
|
49
|
|
|
// make sure maximum width/height not exceeded |
|
50
|
|
|
$height = $height > $max['height'] ? $max['height'] : $height; |
|
51
|
|
|
$width = $width > $max['width'] ? $max['width'] : $width; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// load user mapper |
|
55
|
|
|
$usersMapper = new Mappers\Users; |
|
56
|
|
|
$usersMapper->load($params['uuid']); |
|
57
|
|
|
|
|
58
|
|
|
// work out filename |
|
59
|
|
|
$hash = $f3->hash($asset); // generate unique hash for asset |
|
|
|
|
|
|
60
|
|
|
$filename = 'profile_' . $width . 'x' . $height . '.jpg'; |
|
61
|
|
|
|
|
62
|
|
|
// return the url if exists |
|
63
|
|
|
$url = $usersMapper->profileImageUrl($filename); |
|
64
|
|
|
if (false !== $url) { |
|
65
|
|
|
return $f3->reroute($url); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// 404 if the original asset file does not exist |
|
69
|
|
|
if (!file_exists($asset->filename)) { |
|
70
|
|
|
return $f3->status(404); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// create new resized file |
|
74
|
|
|
$img = new \Image($asset->filename); |
|
75
|
|
|
$img->resize($width, $height); |
|
76
|
|
|
if (!$f3->write($usersMapper->profileImageFilePath($filename), $img->dump('jpeg', $f3->get('assets.image.default.quality.jpg')))) { |
|
|
|
|
|
|
77
|
|
|
return $f3->status(404); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// serve the generated image via the web |
|
81
|
|
|
return $f3->reroute($url); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.