使用干预进行laravel图像上传的问题?

时间:2017-01-14 06:32:39

标签: php image laravel

我编写了一个使用干预上传图片的代码。但是Image没有在相应的目录中找到。我正在使用邮递员表格数据上传图片。 甚至上传后也会收到一条消息,即我正在返回"已上传"。

这是我的路线

Route::post('contact/image/upload', ['as' => 'intervention.postresizeimage','uses' => 'contactController@upload_image']);

控制器 - contactController.php

<?php

    namespace App\Http\Controllers;

    use Illuminate\Support\Facades\File;
    use Illuminate\Http\Request;
    use App\Http\Traits\queryBuilderTrait;
    use App\Http\Traits\imageUploadTrait;
    use App\Http\Requests;
    use DB;
    use Image;
    use App\Contact;
    use \Carbon\Carbon;
    use App\User;
    use App\Communication_link;
    use App\Contact_communication;
    use App\addId;

    class contactController extends Controller
    {
        use queryBuilderTrait; 
        use imageUploadTrait;
        public $response;

        public function upload_image(Request $request) {
            $this->validate($request, [
                'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:1024',
            ]);

            $photo = $request->file('photo');
            $imagename = $photo->getClientOriginalExtension(); 

            $destinationPath = public_path('thumbnail_images');
            $thumb_img = Image::make($photo->getRealPath())->resize(100, 100);
            $thumb_img->save($destinationPath.'/'.$imagename,80);

            $destinationPath = public_path('normal_images');
            $photo->move($destinationPath, $imagename);
            return "uploaded";
      }
}

1 个答案:

答案 0 :(得分:1)

尝试这样做 -

public function upload_image(Request $request) {
    $this->validate($request, [
        'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:1024',
    ]);

    $image = $request->file('photo');
    $filename = time() . '.' . $image->getClientOriginalExtension();
    $location = public_path('images/' . $filename);
    Image::make($image)->resize(800, 600)->save($location);

    return "uploaded";
}