laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子

2025-05-27 0 23

首先先看下效果图

这是添加的时候 可以上传照片

laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子

这是编辑的时候 可以修改照片

laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子

代码部分:

先看控制器:

  1. /***
  2. * 添加商户
  3. * @return \\Illuminate\\Contracts\\View\\Factory|\\Illuminate\\View\\View
  4. */
  5. public function add()
  6. {
  7. $data = null;
  8. return _view('admin.merchant.merchant.edit', compact('data'));
  9. }
  10. /***
  11. * 添加商户
  12. * @return \\Illuminate\\Contracts\\View\\Factory|\\Illuminate\\View\\View
  13. */
  14. public function store(StoreMenchantRequest $request)
  15. {
  16. //判断手机号是否重复 重复不能添加
  17. //后面开发可能会去掉这个判断
  18. $merchant = Merchant::where('mobile', $request->mobile)->first();
  19. if (!empty($merchant)) {
  20. return back()->withErrors('该用户已存在');
  21. }
  22. $token = str_random(60);
  23. $api_token = $this->getToken($token);
  24. $newMerchantData = [
  25. 'mobile' => $request->mobile,
  26. 'api_token' => $api_token,
  27. ];
  28. DB::beginTransaction();
  29. $newMerchant = Merchant::create($newMerchantData);
  30. $newData = [
  31. 'merchant_id' => $newMerchant->id,//Merchantid
  32. 'merchant_principal' => $request->merchant_principal,//负责人
  33. 'merchant_name' => $request->merchant_name,//商家名称
  34. 'merchant_short_name' => $request->merchant_short_name,//商家简称
  35. 'merchant_address' => $request->merchant_address,//商家地址
  36. 'business_num' => $request->business_num,//注册号
  37. 'business_address' => $request->business_address,//营业地址
  38. 'business_name' => $request->business_name,//营业执照名称
  39. 'business_person' => $request->person,//营业执照法人
  40. 'identity_name' => $request->person,//身份证姓名
  41. 'identity_num' => $request->identity_num,//身份证号
  42. ];
  43. //上传缩略图
  44. $input = $request->all();
  45. if (isset($input['file']) && is_object($input['file'])) {
  46. $file_name = save_image_file($input['file'], 'merchant_infos');
  47. if (!$file_name) {
  48. return back()->with('msg', '图片上传失败,请重试!');
  49. }
  50. // dd($file_name);
  51. $input['thumbnail'] = $file_name;
  52. unset($input['_token']);
  53. unset($input['file']);
  54. } else {
  55. return back()->with('msg', '请上传图片');
  56. }
  57. //上传内景图1
  58. if (isset($input['image1']) && is_object($input['image1'])) {
  59. $file_name_1 = save_image_file($input['image1'], 'merchant_infos');
  60. if (!$file_name_1) {
  61. return back()->with('msg', '图片上传失败,请重试!');
  62. }
  63. $input['interior_figure_one'] = $file_name_1;
  64. unset($input['_token']);
  65. unset($input['image1']);
  66. } else {
  67. return back()->with('msg', '请上传图片');
  68. }
  69. //上传内景图2
  70. if (isset($input['image2']) && is_object($input['image2'])) {
  71. $file_name_2 = save_image_file($input['image2'], 'merchant_infos');
  72. if (!$file_name_2) {
  73. return back()->with('msg', '图片上传失败,请重试!');
  74. }
  75. $input['interior_figure_two'] = $file_name_2;
  76. unset($input['_token']);
  77. unset($input['image2']);
  78. } else {
  79. return back()->with('msg', '请上传图片');
  80. }
  81. //上传内景图3
  82. if (isset($input['image3']) && is_object($input['image3'])) {
  83. $file_name_3 = save_image_file($input['image3'], 'merchant_infos');
  84. if (!$file_name_3) {
  85. return back()->with('msg', '图片上传失败,请重试!');
  86. }
  87. $input['interior_figure_three'] = $file_name_3;
  88. unset($input['_token']);
  89. unset($input['image3']);
  90. } else {
  91. return back()->with('msg', '请上传图片');
  92. }
  93. $merchantInfo = MerchantInfo::where('merchant_id', $newMerchant->id)->first();
  94. if (!empty($merchantInfo)) {
  95. return back()->withErrors('该用户已录入信息');
  96. }
  97. $homestayInfo = HomestayInfo::where('merchant_id', $newMerchant->id)->first();
  98. if (!empty($homestayInfo)) {
  99. return back()->withErrors('该用户已录入信息');
  100. }
  101. //录入商户信息
  102. $newData['thumbnail'] = $input['thumbnail'];
  103. $newData['interior_figure_one'] = $input['interior_figure_one'];
  104. $newData['interior_figure_two'] = $input['interior_figure_two'];
  105. $newData['interior_figure_three'] = $input['interior_figure_three'];
  106. $newData['content'] = $input['content'];
  107. $newMerchantInfo = MerchantInfo::create($newData);
  108. $newHomestayInfo = HomestayInfo::create($newData);
  109. if ($newMerchantInfo && $newHomestayInfo && $newMerchant) {
  110. DB::commit();
  111. admin_action_logs($newMerchant, "添加商户成功");
  112. return redirect()->route('admin.merchant.index')->with('msg', '添加成功');
  113. } else {
  114. DB::rollback();
  115. return back()->withErrors('添加失败,请联系管理员');
  116. }
  117. }

这边封装了一个上传图片的方法,调用即可

  1. **
  2. * 调用的文件中需要 use Illuminate\\Support\\Facades\\Input; Illuminate\\Support\\Facades\\Storage;
  3. * save_image_file 保存图片文件 ,存在Storage::disk('uploads') 目录下
  4. * @var $file object 上传图片文件,具体是在 request 中的 UploadedFile 类型的对象
  5. * @var $prefix_name string 可选保存的文件名前缀
  6. * @var $path string 文件路径
  7. * @return bool/string 如果通过验证 则返回在新的文件名
  8. */
  9. if (!function_exists('save_image_file')) {
  10. function save_image_file(&$file, $prefix_name = '', $path = 'serve')
  11. {
  12. $file = isset($file) ? $file : null;
  13. if ($file != null && $file->isValid()) {
  14. // 获取文件相关信息
  15. $originalName = $file->getClientOriginalName(); // 文件原名
  16. $ext = $file->getClientOriginalExtension(); // 扩展名
  17. //dd($ext);
  18. $file->getClientOriginalName();
  19. if ($ext == "" && $file->getClientOriginalName() == 'blob') {
  20. $ext = 'png';
  21. }
  22. if (!preg_match('/jpg|png|gif$/is', $ext)) {
  23. return false;
  24. }
  25. //dd($file->getRealPath());
  26. $realPath = $file->getRealPath(); //临时文件的绝对路径
  27. $type = $file->getClientMimeType(); // image/jpeg
  28. // 上传文件
  29. $filename = $prefix_name . '-' . date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
  30. //dd($filename);
  31. $bool = Storage::disk($path)->put($filename, file_get_contents($realPath));
  32. if (!$bool) return false;
  33. return $filename;
  34. }
  35. return false;
  36. }
  37. }

接下来是编辑时候 显示已经上传图片 并且可以进行修改:

  1. <div class="row">
  2. <div class="col-lg-6 col-sm-8 col-xs-12">
  3. <div class="panel panel-default">
  4. {{ Form::open(['method'=>'post','route' => ['admin.merchant.add_img_store'],'enctype'=>'multipart/form-data']) }}
  5. <div class="panel-heading">商户图片</div>
  6. <div class="panel-body">
  7. <input type="hidden" name="id" value="{{$data->id}}">
  8. <div class=" form-group">
  9. <?php $hasUrl = old_or_new_field('thumbnail', $data); ?>
  10. <div class="form-group {{!$hasUrl or 'has-error'}} has-feedback">
  11. <label class="control-label" for="file">
  12. <span class="font-red">*</span>
  13. <span>缩略图:</span>
  14. <span class="font-gray">(宽高为120px:120px):</span>
  15. </label>
  16. <div class="input-group">
  17. @if( $hasUrl )
  18. <input type="file" class="file-preview form-control" name="file" id="file" accept="image/*"
  19. value="{{ old_or_new_field('thumbnail',$data) }}">
  20. @else
  21. <input type="file" class="file-preview form-control validate" name="file" required id="file"
  22. accept="image/*"
  23. value="{{ old_or_new_field('thumbnail',$data) }}">
  24. @endif
  25. </div>
  26. </div>
  27. <div class="file-preview-wrap">
  28. <img
  29. @if( old_or_new_field('thumbnail',$data) )
  30. src="{{asset('storage/serve').'/'.old_or_new_field('thumbnail',$data)}}" data-src="{{ asset('storage/serve'.old_or_new_img('thumbnail', $data, false))}}"
  31. @else
  32. src="{{asset('storage/serve').'/'. (isset($data->merchantInfo->thumbnail)?$data->merchantInfo->thumbnail:' ')}}" data-src="{{ asset('storage/serve').'/'. (isset($data->merchantInfo->thumbnail)?$data->merchantInfo->thumbnail:' ')}}"
  33. @endif
  34. id="file-preview" class="img-thumbnail" alt="图片预览" data-magnify="gallery">
  35. </div>
  36. </div>
  37. <div>
  38. <?php $hasUrl = old_or_new_field('interior_figure_one', $data); ?>
  39. <div class="form-group {{!$hasUrl or 'has-error'}} has-feedback">
  40. <label class="control-label" for="file">
  41. <span class="font-red">*</span>
  42. <span>内景图1:</span>
  43. <span class="font-gray">(宽高为375px:200px):</span>
  44. </label>
  45. <div class="input-group">
  46. @if( $hasUrl )
  47. <input type="file" class="file-preview-second form-control" name="image1" id="image1" accept="image/*"
  48. value="{{ old_or_new_field('interior_figure_one',$data) }}">
  49. @else
  50. <input type="file" class="file-preview-second form-control validate" name="image1" required id="image1"
  51. accept="image/*"
  52. value="{{ old_or_new_field('interior_figure_one',$data) }}">
  53. @endif
  54. </div>
  55. </div>
  56. <div class="file-preview-wrap">
  57. <img
  58. @if( old_or_new_field('interior_figure_one',$data) )
  59. src="{{asset('storage/serve').'/'.old_or_new_field('interior_figure_one',$data)}}" data-src="{{ asset('storage/serve'.old_or_new_img('interior_figure_one', $data, false))}}"
  60. @else
  61. src="{{asset('storage/serve').'/'.(isset($data->merchantInfo->interior_figure_one)?$data->merchantInfo->interior_figure_one:'')}}" data-src="{{ asset('storage/serve').'/'.(isset($data->merchantInfo->interior_figure_one)?$data->merchantInfo->interior_figure_one:'')}}"
  62. @endif
  63. width="375px" height="200px" id="file-preview-second" class="img-thumbnail" alt="图片预览" data-magnify="gallery">
  64. </div>
  65. </div>
  66. <div >
  67. <?php $hasUrl = old_or_new_field('interior_figure_two', $data); ?>
  68. <div class="form-group {{!$hasUrl or 'has-error'}} has-feedback">
  69. <label class="control-label" for="file">
  70. <span class="font-red">*</span>
  71. <span>内景图2:</span>
  72. <span class="font-gray">(宽高为375px:200px):</span>
  73. </label>
  74. <div class="input-group">
  75. @if( $hasUrl )
  76. <input type="file" class="file-preview-third form-control" name="image2" id="image2" accept="image/*"
  77. value="{{ old_or_new_field('interior_figure_two',$data) }}">
  78. @else
  79. <input type="file" class="file-preview-third form-control validate" name="image2" required id="image2"
  80. accept="image/*"
  81. value="{{ old_or_new_field('interior_figure_two',$data) }}">
  82. @endif
  83. </div>
  84. </div>
  85. <div class="file-preview-wrap">
  86. <img
  87. @if( old_or_new_field('interior_figure_two',$data) )
  88. {{–src="{{route('Img.uploads.file',[old_or_new_field('url',$data)])}}"–}}
  89. src="{{asset('storage/serve').'/'.old_or_new_field('interior_figure_two',$data)}}" data-src="{{ asset('storage/serve'.old_or_new_img('interior_figure_two', $data, false))}}"
  90. @else
  91. src="{{asset('storage/serve').'/'.(isset($data->merchantInfo->interior_figure_two)?$data->merchantInfo->interior_figure_two:'')}}" data-src="{{ asset('storage/serve').'/'.(isset($data->merchantInfo->interior_figure_two)?$data->merchantInfo->interior_figure_two:'')}}"
  92. @endif
  93. width="375px" height="200px" id="file-preview-third" class="img-thumbnail" alt="图片预览" data-magnify="gallery">
  94. </div>
  95. </div>
  96. <div>
  97. <?php $hasUrl = old_or_new_field('interior_figure_three', $data); ?>
  98. <div class="form-group {{!$hasUrl or 'has-error'}} has-feedback">
  99. <label class="control-label" for="file">
  100. <span class="font-red">*</span>
  101. <span>缩略图3:</span>
  102. <span class="font-gray">(宽高为375px:200px):</span>
  103. </label>
  104. <div class="input-group">
  105. @if( $hasUrl )
  106. <input type="file" class="file-preview-forth form-control" name="image3" id="image3" accept="image/*"
  107. value="{{ old_or_new_field('interior_figure_three',$data) }}">
  108. @else
  109. <input type="file" class="file-preview-forth form-control validate" name="image3" required id="image3"
  110. accept="image/*"
  111. value="{{ old_or_new_field('interior_figure_three',$data) }}" >
  112. @endif
  113. </div>
  114. </div>
  115. <div class="file-preview-wrap">
  116. <img
  117. @if( old_or_new_field('interior_figure_three',$data) )
  118. {{–src="{{route('Img.uploads.file',[old_or_new_field('url',$data)])}}"–}}
  119. src="{{asset('storage/serve').'/'.old_or_new_field('interior_figure_three',$data)}}" data-src="{{ asset('storage/serve'.old_or_new_img('interior_figure_three', $data, false))}}"
  120. @else
  121. src="{{asset('storage/serve').'/'.(isset($data->merchantInfo->interior_figure_three)?$data->merchantInfo->interior_figure_three:'')}}" data-src="{{ asset('storage/serve').'/'.(isset($data->merchantInfo->interior_figure_three)?$data->merchantInfo->interior_figure_three:'')}}"
  122. @endif
  123. width="375px" height="200px" id="file-preview-forth" class="img-thumbnail" alt="图片预览" data-magnify="gallery">
  124. </div>
  125. </div>
  126. </div>
  127. <div class="text-center margin-bottom-sm">
  128. <button class="pretty-btn"> 编辑商户</button>
  129. </div>
  130. {{ Form::close() }}
  131. </div>
  132. </div>
  133. </div>

编辑这边 的控制器代码是:

  1. /***
  2. * 添加图片
  3. * @return \\Illuminate\\Contracts\\View\\Factory|\\Illuminate\\View\\View
  4. */
  5. public function add_img()
  6. {
  7. $data = null;
  8. return _view('admin.merchant.merchant.add', compact('data'));
  9. }
  10. /***
  11. * 保存图片
  12. * @return \\Illuminate\\Contracts\\View\\Factory|\\Illuminate\\View\\View
  13. */
  14. public function add_img_store(Request $request)
  15. {
  16. //上传缩略图
  17. $input = $request->all();
  18. if (isset($input['file']) && is_object($input['file'])) {
  19. $file_name = save_image_file($input['file'], 'merchant_infos');
  20. if (!$file_name) {
  21. return back()->with('msg', '图片上传失败,请重试!');
  22. }
  23. $input['thumbnail'] = $file_name;
  24. unset($input['_token']);
  25. unset($input['file']);
  26. } else {
  27. return back()->with('msg', '请上传图片');
  28. }
  29. //上传内景图1
  30. if (isset($input['image1']) && is_object($input['image1'])) {
  31. $file_name_1 = save_image_file($input['image1'], 'merchant_infos');
  32. if (!$file_name_1) {
  33. return back()->with('msg', '图片上传失败,请重试!');
  34. }
  35. $input['interior_figure_one'] = $file_name_1;
  36. unset($input['_token']);
  37. unset($input['image1']);
  38. } else {
  39. return back()->with('msg', '请上传图片');
  40. }
  41. //上传内景图2
  42. if (isset($input['image2']) && is_object($input['image2'])) {
  43. $file_name_2 = save_image_file($input['image2'], 'merchant_infos');
  44. if (!$file_name_2) {
  45. return back()->with('msg', '图片上传失败,请重试!');
  46. }
  47. $input['interior_figure_two'] = $file_name_2;
  48. unset($input['_token']);
  49. unset($input['image2']);
  50. } else {
  51. return back()->with('msg', '请上传图片');
  52. }
  53. //上传内景图3
  54. if (isset($input['image3']) && is_object($input['image3'])) {
  55. $file_name_3 = save_image_file($input['image3'], 'merchant_infos');
  56. if (!$file_name_3) {
  57. return back()->with('msg', '图片上传失败,请重试!');
  58. }
  59. $input['interior_figure_three'] = $file_name_3;
  60. unset($input['_token']);
  61. unset($input['image3']);
  62. } else {
  63. return back()->with('msg', '请上传图片');
  64. }
  65. //录入商户信息
  66. $merchang_info = MerchantInfo::where('merchant_id', '=', $input['id'])->first();
  67. if (empty($merchang_info)) {
  68. $newData['thumbnail'] = $input['thumbnail'];
  69. $newData['merchant_id'] = $input['id'];
  70. $newData['interior_figure_one'] = $input['interior_figure_one'];
  71. $newData['interior_figure_two'] = $input['interior_figure_two'];
  72. $newData['interior_figure_three'] = $input['interior_figure_three'];
  73. $newData['content']='';
  74. $result = MerchantInfo::create($newData);
  75. } /* $newData['thumbnail']=$input['thumbnail'];
  76. $newData['interior_figure_one']=$input['interior_figure_one'];
  77. $newData['interior_figure_two']=$input['interior_figure_two'];
  78. $newData['interior_figure_three']=$input['interior_figure_three'];
  79. // $newData['content']=$input['content'];
  80. $newMerchantInfo = MerchantInfo::create($newData);*/
  81. else {
  82. $merchang_info->thumbnail = $input['thumbnail']??'';
  83. $merchang_info->interior_figure_one = $input['interior_figure_one']??'';
  84. $merchang_info->interior_figure_two = $input['interior_figure_two']??'';
  85. $merchang_info->interior_figure_three = $input['interior_figure_three']??'';
  86. $result = $merchang_info->save();
  87. }
  88. if ($result) {
  89. DB::commit();
  90. admin_action_logs($result, "编辑商户成功");
  91. return redirect()->route('admin.merchant.index')->with('msg', '编辑成功');
  92. } else {
  93. DB::rollback();
  94. return back()->withErrors('编辑失败,请联系管理员');
  95. }
  96. }

以上这篇laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/zhangzeshan/article/details/81484441

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子 https://www.kuaiidc.com/71118.html

相关文章

发表评论
暂无评论