Skip to content

Commit

Permalink
get_data_url
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdual committed Aug 22, 2020
1 parent 1f1e539 commit 2e41458
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ More examples in the "[examples](/examples)" directory.
|---|---|---|
| `upload()` | Upload the file and return output of the check() | boolean |
| `check()` | Check the file can be uploaded | boolean |
| `get_name()` | Get uploaded file name | string |
| `get_path()` | Get the path name of the file | string |
| `get_name()` | Get the uploaded file name | string |
| `get_tmp_name()` | Get the temporary file path | string |
| `get_data_url()` | Get the file as base64 encoded data URL | string |
| `get_path()` | Get the path of the file | string |
| `get_error()` | Get error message if an error occurred | string |

### Notes
[`exif_imagetype()`](https://php.net/manual/en/function.exif-imagetype.php) and [`getimagesize()`](https://php.net/manual/en/function.getimagesize.php) must be allowed on the server.
* `exif` and `fileinfo` extensions must be enabled.
* [`exif_imagetype()`](https://php.net/manual/en/function.exif-imagetype.php) and [`getimagesize()`](https://php.net/manual/en/function.getimagesize.php) must be allowed.

### Contributes
Please send pull request or open an issue if you have the feature you want.
Please send pull request or open an issue if you have the feature you want.
26 changes: 26 additions & 0 deletions examples/data_url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
require __DIR__ . '/../src/Uploader.php';

use \iamdual\Uploader;

if (isset($_FILES["file"])) {

$upload = new Uploader($_FILES["file"]);
$upload->allowed_extensions(array("png", "jpg", "jpeg", "gif"));
$upload->allowed_types(array("image/png", "image/jpeg"));
$upload->max_size(5); // in MB
$upload->min_size(0); // in MB
$upload->path("upload/files");

if (!$upload->check()) {
echo "Upload error: " . $upload->get_error();
} else {
echo 'Base64 encoded data URL: <textarea>'.$upload->get_data_url().'</textarea>';
}

}
?>

<form enctype="multipart/form-data" action="" method="post">
Select File: <input type="file" name="file"> <input type="submit" value="Upload">
</form>
17 changes: 17 additions & 0 deletions src/Uploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,23 @@ public function get_tmp_name()
return isset($this->file["tmp_name"]) ? $this->file["tmp_name"] : null;
}

/**
* Get the data URL of the temporary file
* @return string
*/
public function get_data_url()
{
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

if (isset($this->file["tmp_name"])) {
$mime = mime_content_type($this->file["tmp_name"]);
$source = file_get_contents($this->file["tmp_name"]);
$encoded = base64_encode($source);
return 'data:' . $mime . ';base64,' . $encoded;
}
return null;
}

/**
* Check the file can be uploaded
* @return boolean
Expand Down
7 changes: 7 additions & 0 deletions tests/MethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ public function testGetTmpName()

$this->assertNotNull($upload->get_tmp_name());
}

public function testGetDataUrl()
{
$upload = new \iamdual\Uploader(["name" => "foo.png", "type" => "image/png", "tmp_name" => __DIR__ . "/assets/foo.png", "error" => 0, "size" => 1]);

$this->assertEquals(trim(file_get_contents(__DIR__ . "/assets/foo.base64")), $upload->get_data_url());
}
}
1 change: 1 addition & 0 deletions tests/assets/foo.base64
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyBAMAAABYG2ONAAAAFVBMVEUAAAD///9/f39fX1+fn58fHx8/Pz8rYiDqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAo0lEQVRIie2Qyw7CIBBFb2DwO5q+1o0L1w0NrrHRPQnV//8EAUl0ga1ujIs5CZAz4YYZAIZhmN/QQOkjzq3LLuv6xUrQHmTJGphcEGE9rUQ3Y4bqqrDjAlgQoJK9Z8YBmFy8Gp8DeSeTfRSBCf2I6/JN5ORiRfrNiIfqh9S9SVPL27A1C0G4EX2eJR7J1iI7rbG0Vf4x0UwPW0Uh3i0bwzD/yR11mBj1DIKiVwAAAABJRU5ErkJggg==

0 comments on commit 2e41458

Please sign in to comment.