Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option random_cb for images in edit fields #930

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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: 2 additions & 0 deletions docs/field-type-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ The optional `naming` option lets you define whether to `keep` the file's name o

The optional `length` option lets you define size of file name in case `random` is supplied as `naming` option method.

The optional `random_cb` option lets you provide a callback to generate random names for images. The callback receives three parameters : the original file name, an instance of `Symfony\Component\HttpFoundation\File\UploadedFile` and the `length` option provided. The callback must return the desired file name.

The optional `size_limit` option lets you set an integer size limit counted in megabytes. This only affects the JavaScript file uploading dialog, it doesn't limit your PHP upload sizes.

The optional `display_raw_value` option lets you put the raw value of the saved image source string into the image input. This is useful if you're using accessors, mutators, and [`setter fields`](/docs/fields#setter-option) to skip storing the image on your local server and instead upload it to a remote public image server.
Expand Down
3 changes: 3 additions & 0 deletions src/Frozennode/Administrator/Fields/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Image extends File {
* @var array
*/
protected $imageDefaults = array(
'random_cb' => null,
'sizes' => array(),
);

Expand All @@ -20,6 +21,7 @@ class Image extends File {
* @var array
*/
protected $imageRules = array(
'random_cb' => 'callable',
'sizes' => 'array',
);

Expand All @@ -35,6 +37,7 @@ public function doUpload()
$this->getOption('naming') === 'random')
->sizes($this->getOption('sizes'))
->set_length($this->getOption('length'))
->filename_callback($this->getOption('random_cb'))
->upload();

return $result[0];
Expand Down
4 changes: 3 additions & 1 deletion src/Frozennode/Administrator/Includes/Multup.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ private function upload_image()

if($this->random){
if(is_callable($this->random_cb)){
$filename = call_user_func( $this->random_cb, $original_name );
$filename = call_user_func($this->random_cb, $original_name,
$this->image[$this->input],
$this->random_length);
} else {
$ext = File::extension($original_name);
$filename = $this->generate_random_filename().'.'.$ext;
Expand Down