rapyd-laravel icon indicating copy to clipboard operation
rapyd-laravel copied to clipboard

change image filename but not the extension

Open H889761123 opened this issue 9 years ago • 11 comments

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 ?

H889761123 avatar Mar 06 '15 15:03 H889761123

+1

mehrdd avatar Mar 09 '15 06:03 mehrdd

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);
}

zofe avatar Mar 09 '15 06:03 zofe

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 avatar Mar 09 '15 07:03 mehrdd

@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?

zofe avatar Mar 09 '15 07:03 zofe

Yeah absolutely , The image() closure is very cleaner and better ...

mehrdd avatar Mar 09 '15 07:03 mehrdd

I have added the code, but unfortunately it didn't work, the code is still saving the original filename in the database

H889761123 avatar Mar 09 '15 21:03 H889761123

I am still trying to solve this issue but with no success, any specific reason why this didn't work? or any alternative solution?

H889761123 avatar Mar 13 '15 12:03 H889761123

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!

fantasmewfs avatar Mar 27 '15 16:03 fantasmewfs

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');

heimasi avatar Feb 06 '17 03:02 heimasi