Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidImageFieldException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace QCod\ImageUp\Exceptions;

class InvalidImageFieldException extends \Exception
{

}
46 changes: 29 additions & 17 deletions src/HasImageUploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,17 @@ public function getImageFieldOptions($field = null)
{
// get first option if no field provided
if (is_null($field)) {
$options = array_first($this->getDefinedImageFields());
$imagesFields = $this->getDefinedImageFields();

if (!$options) {
if (!$imagesFields) {
throw new InvalidImageFieldException(
'No image fields are defined in $imageFields array on model.'
);
}

$fieldKey = array_first(array_keys($imagesFields));
$options = is_int($fieldKey) ? [] : array_first($imagesFields);

return $options;
}

Expand All @@ -235,7 +238,7 @@ public function getImageFieldOptions($field = null)
);
}

return array_get($this->getDefinedImageFields(), $field);
return array_get($this->getDefinedImageFields(), $field, []);
}

/**
Expand All @@ -262,9 +265,13 @@ public function getImageFieldName($field = null)
return $field;
}

$imagesFields = $this->getDefinedImageFields();
$fieldKey = array_first(array_keys($imagesFields));

// return first field name
$fieldKey = array_keys($this->getDefinedImageFields());
return array_first($fieldKey);
return is_int($fieldKey)
? $imagesFields[$fieldKey]
: $fieldKey;
}

/**
Expand Down Expand Up @@ -466,21 +473,26 @@ protected function validationFactory()
*/
protected function autoUpload()
{
foreach ($this->getDefinedImageFields() as $field => $options) {
foreach ($this->getDefinedImageFields() as $key => $val) {
$field = is_numeric($key) ? $val : $key;
$options = array_wrap($val);

// check if global upload is allowed, then in override in option
$autoUploadAllowed = array_get($options, 'auto_upload', $this->canAutoUploadImages());

if (is_array($options) && count($options) && $autoUploadAllowed) {
// get the input file name
$requestFileName = array_get($options, 'file_input', $field);

// if request has the file upload it
if (request()->hasFile($requestFileName)) {
$this->uploadImage(
request()->file($requestFileName),
$field
);
}
if (! $autoUploadAllowed) {
continue;
}

// get the input file name
$requestFileName = array_get($options, 'file_input', $field);

// if request has the file upload it
if (request()->hasFile($requestFileName)) {
$this->uploadImage(
request()->file($requestFileName),
$field
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ImageUpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function boot()
]);

$this->mergeConfigFrom(
__DIR__.'/../config/imageup.php', 'imageup'
__DIR__.'/../config/imageup.php',
'imageup'
);
}

Expand All @@ -29,6 +30,5 @@ public function boot()
*/
public function register()
{

}
}
35 changes: 35 additions & 0 deletions tests/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,49 @@ class UserController extends Controller
public function store(Request $request)
{
$user = new User();

$fieldOption = [
'avatar' => ['width' => 200],
'cover' => ['width' => 400, 'height' => 400]
];

$user->setImagesField($fieldOption);
$user->forceFill($request->all())->save();

return $user;
}

public function storeImagesWithoutOptions(Request $request)
{
$user = new User();

$fieldOption = [
'avatar',
'cover'
];

$user->setImagesField($fieldOption);
$user->forceFill($request->all())->save();

return $user;
}

public function storeImagesWithMixedOptions(Request $request)
{
$user = new User();

$fieldOption = [
'avatar',
'cover' => [
'width' => 400,
'height' => 400,
'auto_upload' => false
],
];

$user->setImagesField($fieldOption);
$user->forceFill($request->except('cover'))->save();

return $user;
}
}
2 changes: 1 addition & 1 deletion tests/Hooks/CopyImageHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public function handle($image)
'public'
);
}
}
}
2 changes: 1 addition & 1 deletion tests/Hooks/ResizeToFiftyHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ public function handle($image)
{
$image->resize(50, 50);
}
}
}
36 changes: 21 additions & 15 deletions tests/ImageResizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

class ImageResizeTest extends TestCase
{
protected $testImage = __DIR__ . '/images/test1200x1200.png';
protected $newImage;
protected $testImage = __DIR__ . '/images/test1200x1200.png';
protected $newImage;
protected $user;

protected function setUp()
Expand All @@ -26,7 +26,7 @@ protected function tearDown()
{
parent::tearDown();

if( $this->newImage ) {
if ($this->newImage) {
$this->newImage->destroy();
}
}
Expand All @@ -37,7 +37,7 @@ protected function tearDown()
*
* @test
*/
function it_resize_image_based_on_width_given()
public function it_resize_image_based_on_width_given()
{
$this->newImage = $this->user->resizeImage($this->testImage, ['width' => 300]);

Expand All @@ -49,7 +49,7 @@ function it_resize_image_based_on_width_given()
*
* @test
*/
function it_resize_image_by_height()
public function it_resize_image_by_height()
{
$this->newImage = $this->user->resizeImage($this->testImage, ['height' => 200]);

Expand All @@ -61,14 +61,16 @@ function it_resize_image_by_height()
*
* @test
*/
function it_crops_image_in_given_width_and_height()
public function it_crops_image_in_given_width_and_height()
{
$this->newImage = $this->user->resizeImage($this->testImage,
$this->newImage = $this->user->resizeImage(
$this->testImage,
[
'width' => 150,
'height' => 150,
'crop' => true
]);
]
);

$this->assertEquals(150, $this->newImage->width());
$this->assertEquals(150, $this->newImage->height());
Expand All @@ -79,14 +81,16 @@ function it_crops_image_in_given_width_and_height()
*
* @test
*/
function it_crops_in_x_and_y_if_crop_is_set_to_array_of_coordinates()
public function it_crops_in_x_and_y_if_crop_is_set_to_array_of_coordinates()
{
$this->newImage = $this->user->resizeImage($this->testImage,
$this->newImage = $this->user->resizeImage(
$this->testImage,
[
'width' => 100,
'height' => 100,
'crop' => [25, 10]
]);
]
);

$this->assertEquals(100, $this->newImage->width());
$this->assertEquals(100, $this->newImage->height());
Expand All @@ -97,14 +101,16 @@ function it_crops_in_x_and_y_if_crop_is_set_to_array_of_coordinates()
*
* @test
*/
function it_can_override_the_crop_x_and_y_coordinates()
public function it_can_override_the_crop_x_and_y_coordinates()
{
$this->newImage = $this->user->cropTo(10, 0)->resizeImage($this->testImage,
$this->newImage = $this->user->cropTo(10, 0)->resizeImage(
$this->testImage,
[
'width' => 100,
'height' => 100,
'crop' => [25, 10]
]);
]
);

$this->assertEquals(100, $this->newImage->width());
$this->assertEquals(100, $this->newImage->height());
Expand All @@ -115,7 +121,7 @@ function it_can_override_the_crop_x_and_y_coordinates()
*
* @test
*/
function it_do_not_resize_if_width_and_height_are_not_provided()
public function it_do_not_resize_if_width_and_height_are_not_provided()
{
$this->newImage = $this->user->resizeImage($this->testImage, []);

Expand Down
Loading