rapyd-laravel
rapyd-laravel copied to clipboard
change image filename but not the extension
Hi, I am using this code to save an image
$edit->add('image','Image','image')->image(function($image){
$name = $image->filename.time();
$extension = $image->extension;
$image->widen('800',function($constraint){$constraint->upsize();});
$image->save("public/uploads/{$name}_width800.{$extension}");
}
The problem, is that when saving the image, the database field value is the orginal filename, I know I can use ->move() and pass the filename as parameter, but I don't know the extension of the original filename beforehand.
in summary, I want to change the filename value when saving it to database, but I want to keep the original uploaded file extension.
any ideas ?
+1
in summary, I want to change the filename value when saving it to database,
but I want to keep the original uploaded file extension.
That seems reasonable
untested... but this can work, please let me know
$edit->add('image','Image','image')->image(function($image) use ($edit){
...
$newname = "{$name}_width800.{$extension}";
$edit->field("image")->insertValue($newname)->updateValue($newname);
}
Thanks for the reply. i tested your code but it didn't work ...
i used a dirty way for a demo like this :
if (\Request::input('process'))
{
$filename = \Request::file('image')->getClientOriginalName();
$ext = \Request::file('image')->getClientOriginalExtension();
$without_ext = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename);
$filename = str_slug(strtolower($without_ext)).'-'.time().'.'.$ext;
}
$form->add('image', 'Category Image', 'image')
->move(public_path()."/files/images", $filename)
->rule('mimes:jpeg,jpg,png,gif')
->preview(100, 100);
@mehrdd the sample I made is about image() closure so it's more related to @HassanKanj code, However we can adopt a convention like this..: if given $filename is without extension, the original extension will be added. This would be enough?
Yeah absolutely , The image() closure is very cleaner and better ...
I have added the code, but unfortunately it didn't work, the code is still saving the original filename in the database
I am still trying to solve this issue but with no success, any specific reason why this didn't work? or any alternative solution?
I just found an alternative soulution:
if(Input::hasFile('image')){
$extension = Input::file('image')->getClientOriginalExtension();
}else{
$extension=NULL;
}
...
$entity->add('image','Image', 'image')->move($img_path, 'cool_name.'.$extension);
Have fun!
In File.php, add codes behind line 105:
if (strstr($filename, '.') == false) $filename = $filename .'.'. strtolower($this->file->getClientOriginalExtension());
then, use move('path/to/', 'name_without_ext');