Skip to content

Commit ff979e4

Browse files
committed
Added the ->fit() method
1 parent 67d7a5e commit ff979e4

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ Downsize an image if it's bigger than the specified width and/or height (the asp
9696
->downsize(integer $maxWidth, integer $maxHeight)
9797
```
9898

99+
Crop and resize an image to fit the the specified dimensions (the same as `->fit()` from Intervention Image)
100+
101+
```php
102+
->fit(integer $width, integer $height, boolean $dontUpsize = false)
103+
```
104+
99105
Call this method before calling any of the above methods, for example:
100106

101107
```php

src/FileUploader.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ public function setFile($file)
101101
return $this;
102102
}
103103

104+
/**
105+
* Create new or return an existing Intervention Image object based on
106+
* $this->file
107+
*
108+
* @return Image
109+
*/
110+
protected function getImage()
111+
{
112+
if (!$this->image) {
113+
if (self::isImage($this->file)) {
114+
return Image::make($this->file);
115+
}
116+
}
117+
118+
return $this->image;
119+
}
120+
104121
/**
105122
* Uploads a file under a hashname
106123
*
@@ -178,22 +195,40 @@ public function replaceAs($oldFilePath, $newFilename, $path = '', $storage = 'pu
178195
*/
179196
public function downsize($maxWidth, $maxHeight)
180197
{
181-
if (self::isImage($this->file)) {
182-
$image = Image::make($this->file);
183-
198+
if ($this->image = $this->getImage()) {
184199
// Downsize the image if it's bigger than desired
185-
if ($image->width() > $maxWidth || $image->height() > $maxHeight) {
186-
$image->resize($maxWidth, $maxHeight, function ($constraint) {
200+
if ($this->image->width() > $maxWidth || $this->image->height() > $maxHeight) {
201+
$this->image->resize($maxWidth, $maxHeight, function ($constraint) {
187202
$constraint->aspectRatio();
188203
});
189-
190-
$this->image = $image;
191204
}
192205
}
193206

194207
return $this;
195208
}
196209

210+
/**
211+
* Crop and resize an image to fit the the specified dimensions
212+
*
213+
* @param integer $width
214+
* @param integer $height
215+
* @param boolean $dontUpsize don't upsize the image if it's smaller than
216+
* the provided width and height
217+
* @return void
218+
*/
219+
public function fit($width, $height, $dontUpsize = false)
220+
{
221+
if ($this->image = $this->getImage()) {
222+
$this->image->fit($width, $height, function ($constraint) use ($dontUpsize) {
223+
if ($dontUpsize) {
224+
$constraint->upsize();
225+
}
226+
});
227+
}
228+
229+
return $this;
230+
}
231+
197232
/**
198233
* Delete a file from the specified storage
199234
*

tests/FileUploaderTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ public function testItDownsizesImageBeforeUploading()
5454
$this->assertLessThanOrEqual(200, $uploaded->height());
5555
}
5656

57+
public function testItDownsizesAndCropsImageBeforeUploading()
58+
{
59+
Storage::fake('public');
60+
61+
$image = UploadedFile::fake()->image('image.jpg', 640, 480);
62+
63+
$path = FileUploader::make($image)->fit(640, 640)->upload();
64+
65+
Storage::disk('public')->assertExists($path);
66+
67+
$uploaded = Image::make(Storage::disk('public')->get($path));
68+
$this->assertEquals(640, $uploaded->width());
69+
$this->assertEquals(640, $uploaded->height());
70+
}
71+
72+
public function testItDownsizesAndCropsImageBeforeUploadingWithoutUpscaling()
73+
{
74+
Storage::fake('public');
75+
76+
$image = UploadedFile::fake()->image('image.jpg', 640, 480);
77+
78+
$path = FileUploader::make($image)->fit(640, 640, true)->upload();
79+
80+
Storage::disk('public')->assertExists($path);
81+
82+
$uploaded = Image::make(Storage::disk('public')->get($path));
83+
$this->assertEquals(480, $uploaded->width());
84+
$this->assertEquals(480, $uploaded->height());
85+
}
86+
5787
public function testItReplacesTheOldFileWithTheNewFile()
5888
{
5989
Storage::fake('public');

0 commit comments

Comments
 (0)