/
Illuminage.php
177 lines (157 loc) · 3.55 KB
/
Illuminage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Illuminage;
use Illuminate\Container\Container;
/**
* Handles image creation and caching
*
* @property request $request
*/
class Illuminage
{
/**
* The IoC Container
*
* @var Container
*/
protected $app;
/**
* Set up Illuminage
*
* @param Container $container A base Container to bind onto
*/
public function __construct(Container $app)
{
$this->app = $app;
}
/**
* Get an option from the config
*
* @param string $option
* @param string $fallback
*
* @return array|string|null
*/
public function getOption($option, $fallback = null)
{
return $this->app['config']->get('illuminage::'.$option, $fallback);
}
////////////////////////////////////////////////////////////////////
///////////////////////////// CONSTRUCTORS /////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Create a new Image
*
* @param string $image Path to the image
*
* @return Image
*/
public function image($image)
{
return new Image($this->app, $image);
}
/**
* Resize an image
*
* @param string $image
* @param integer $width
* @param integer $height
*
* @return Image
*/
public function resize($image, $width, $height = null)
{
// Fallback size for height
if (!$height) {
$height = $width;
}
$image = $this->image($image);
$image->resize($width, $height);
return $image;
}
/**
* Create a resized image
*
* @param string $image
* @param integer $width
* @param integer $height
*
* @return Image
*/
public function thumb($image, $width, $height = null, $outbound = true)
{
$image = $this->image($image);
$image->thumbnail($width, $height, $outbound);
return $image;
}
/**
* Create a thumb image
*
* @param string $image
* @param integer $size
* @param string $outbound
*
* @return Image
*/
public function square($image, $size, $outbound = true)
{
return $this->thumb($image, $size, $size, $outbound);
}
////////////////////////////////////////////////////////////////////
/////////////////////////// IMAGE PROCESSING ///////////////////////
////////////////////////////////////////////////////////////////////
/**
* Process an Image
*
* @param Image $image
*
* @return string
*/
public function process(Image $image)
{
$endpoint = $this->app['illuminage.cache']->getCachePathOf($image);
// If the image hasn't yet been processed, do it
// and then cache it
if (!$this->app['illuminage.cache']->isCached($image)) {
$quality = $image->getQuality() ?: $this->getOption('quality');
$processedImage = $this->app['illuminage.processor']->process($image);
$processedImage->save($endpoint, array('quality' => (int) $quality));
}
return $endpoint;
}
////////////////////////////////////////////////////////////////////
////////////////////////////// HELPERS /////////////////////////////
////////////////////////////////////////////////////////////////////
/**
* Get the URL to an image
*
* @param Image $image
*
* @return string
*/
public function getUrlTo(Image $image)
{
return $this->app['url']->asset(
$this->getOption('cache_folder').
$this->app['illuminage.cache']->getHashOf($image));
}
/**
* Get the path to the public folder
*
* @return string
*/
public function getPublicFolder()
{
return $this->app->bound('path.public')
? $this->app['path.public'].'/'
: './';
}
/**
* Get the cache folder
*
* @return string
*/
public function getCacheFolder()
{
return $this->getPublicFolder().$this->getOption('cache_folder');
}
}