【问题标题】:Laravel Mutiple Image Saving Always lacks 1 imageLaravel 多张图片保存总是缺少 1 张图片
【发布时间】:2015-04-14 14:03:52
【问题描述】:

我已经查看了我的代码一个小时了,但我真的找不到我的代码有什么问题。当我上传 3 张图片时,它只保存 2 张,而 4 张则保存 3 张,依此类推,所以这是我的代码:

控制器

if(Input::hasFile('images'))
    {

    $file = Input::file('images');

    foreach($file as $files) {
        $img = Image::make($files)->resize(300, 240);
        $lgimg = Image::make($files);
    $name = time().'-'.'chicken.jpg';


    $img->save('images/chickens/thumbs/'.$name,30);

    $lgimg->resize(800, null, function ($constraint) {
     $constraint->aspectRatio();
    });
    $lgimg->save('images/chickens/'.$name,60);

    $newimage = new Photo;
    $newimage->chicken_id=$idinsert;
    $newimage->photo_loc=$name;
    $newimage->save();

    }

    }

观看次数

    {{ Form::label('tuimg','Upload Image')}}
    {{ Form::file('images[]', array('multiple'=>true)) }}

请帮忙。谢谢

【问题讨论】:

    标签: php laravel eloquent image-uploading blade


    【解决方案1】:

    发现问题,是文件名没有改变,这就是照片总是缺少的原因,它使用相同的文件名,因为我正在使用时间。为了进一步解释这里是代码

    if(输入::hasFile('images')) {

        $file = Input::file('images');
    
        //we need to a changing value on every save
        $myarray=0;
    
        foreach($file as $files) {
            $img = Image::make($files)->resize(300, 240);
            $lgimg = Image::make($files);
        $name = time().'-'.$myarray.'chicken.jpg';
    
        $img->save('images/chickens/thumbs/'.$name,30);
    
        $lgimg->resize(800, null, function ($constraint) {
         $constraint->aspectRatio();
        });
        $lgimg->save('images/chickens/'.$name,60);
    
        $newimage = new Photo;
        $newimage->chicken_id=$idinsert;
        $newimage->photo_loc=$name;
        $newimage->save();
    
        $myarray++;
        }
    
        }
    

    【讨论】:

    • 您可以像这样使用 datetime 函数 date("Y-m-d H:i:s") 来代替时间和递增变量。
    猜你喜欢
    • 2014-04-02
    • 2022-10-17
    • 2020-12-17
    • 2022-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    相关资源
    最近更新 更多