Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Added EncodedImage rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Alphametric committed Apr 2, 2019
1 parent a3cab43 commit 83f6b31
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The following validation rules are currently available:
| DisposableEmail | validation.disposable_email | Requires the presence of an email address which is not disposable |
| DoesNotExist | validation.does_not_exist | Requires that the given value is not present in a given database table / column - see class for details |
| Decimal | validation.decimal | Requires that the given value is a decimal with an appropriate format - see class for details |
| EncodedImage | validation.encoded_image | Requires that the given value is a base64-encoded image of a given mime type - see class for details |

The package will receive new rules over time, however since these updates will not be breaking changes, they will not receive major version numbers unless Laravel changes in such a way that the package requires a re-write.

Expand Down
84 changes: 84 additions & 0 deletions src/EncodedImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

// Namespace
namespace Alphametric\Validation\Rules;

// Using directives
use Illuminate\Http\UploadedFile;

// Encoded image rule
class EncodedImage extends Rule
{

/**
* Pointer to the temporary file.
*
**/
protected $file;



/**
* Write the given data to a temporary file.
*
* @param string $data.
* @return UploadedFile.
*
**/
protected function createTemporaryFile($data)
{
$this->file = tmpfile();

fwrite($this->file, base64_decode(str_after($data, 'base64,')));

return new UploadedFile(
stream_get_meta_data($this->file)['uri'], 'image',
'text/plain', null, null, true
);
}



/**
* Determine if the validation rule passes.
*
* The rule requires a single parameter, which is
* the expected mime type of the file e.g. png, jpeg etc.
*
* @param string $attribute.
* @param mixed $value.
* @return bool.
*
**/
public function passes($attribute, $value)
{
if (! starts_with($value, "data:image/{$this->parameters[0]};base64,")) {
return false;
}

$result = validator(['file' => $this -> createTemporaryFile($value)], ['file' => 'image'])
-> passes();

fclose($this->file);

return $result;
}



/**
* Get the validation error message.
*
* @param none.
* @return string.
*
**/
public function message()
{
return Helper::getLocalizedErrorMessage(
'encoded_image',
"The :attribute must be a valid {$this->parameters[0]} image"
);
}

}
Binary file added support/assets/image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added support/assets/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions tests/EncodedImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

// Namespace
namespace Alphametric\Validation\Rules\Tests;

// Using directives
use Alphametric\Validation\Rules\EncodedImage;
use Orchestra\Testbench\TestCase as Orchestra;

// Encoded image test
class EncodedImageTest extends Orchestra
{

/**
* Retrieve the contents of a given file.
*
* @param string $file_name.
* @return string.
*
**/
protected function getFile($file_name)
{
$path = realpath(__DIR__ . '/..') . "/support/assets/$file_name";

$extension = pathinfo($path, PATHINFO_EXTENSION);

return "data:image/{$extension};base64," . base64_encode(file_get_contents($path));
}



/**
* Generate an invalid image file with a given mime type.
*
* @param string $mime.
* @return string.
*
**/
protected function invalidImage($mime = "")
{
return "data:image/{$mime};base64," . base64_encode("not an image file");
}



/** @test */
public function a_encoded_jpeg_image_can_be_validated()
{
// Define the validation rule
$png_rule = ['image' => [new EncodedImage('png')]];
$jpeg_rule = ['image' => [new EncodedImage('jpeg')]];

// Execute the tests
$this->assertFalse(validator(['image' => $this -> getFile('image.jpeg')], $png_rule)->passes());
$this->assertTrue(validator(['image' => $this -> getFile('image.png')], $png_rule)->passes());
$this->assertTrue(validator(['image' => $this -> getFile('image.jpeg')], $jpeg_rule)->passes());
$this->assertFalse(validator(['image' => $this -> getFile('image.png')], $jpeg_rule)->passes());
$this->assertFalse(validator(['image' => $this -> invalidImage('image.jpeg')], $jpeg_rule)->passes());
$this->assertFalse(validator(['image' => $this -> invalidImage('image.png')], $jpeg_rule)->passes());
$this->assertFalse(validator(['image' => $this -> invalidImage()], $jpeg_rule)->passes());
}

}

0 comments on commit 83f6b31

Please sign in to comment.