Advertisement
Guest User

Untitled

a guest
Jul 9th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Models\Article;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Storage;
  9. use Illuminate\Support\Facades\Log;
  10. use Intervention\Image\Facades\Image;
  11.  
  12. class ArticleController extends Controller
  13. {
  14.  public function update(Request $request, $slug)
  15.     {
  16.         $request->validate([
  17.             'title_en' => 'required_without:title_cs',
  18.             'content_en' => 'required_without:content_cs',
  19.             'title_cs' => 'required_without:title_en',
  20.             'content_cs' => 'required_without:content_en',
  21.             'background_image' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
  22.         ]);
  23.  
  24.         $article = Article::where('slug', $slug)->firstOrFail();
  25.         $imagePath = $article->background_image;
  26.         $thumbnailPath = $article->thumbnail_image;
  27.  
  28.         if ($request->hasFile('background_image')) {
  29.             try {
  30.                 // Delete the old image and thumbnail if exists
  31.                 if ($article->background_image) {
  32.                     Storage::disk('public')->delete($article->background_image);
  33.                     Storage::disk('public')->delete($article->thumbnail_image);
  34.                 }
  35.  
  36.                 $image = $request->file('background_image');
  37.                 $imagePath = $image->store('uploads/images', 'public');
  38.  
  39.                 // Resize image to 1920px width
  40.                 $resizedImage = Image::make($image)->resize(1920, null, function ($constraint) {
  41.                     $constraint->aspectRatio();
  42.                     $constraint->upsize();
  43.                 });
  44.                 $resizedImage->save(storage_path('app/public/' . $imagePath));
  45.  
  46.                 // Create thumbnail
  47.                 $thumbnailPath = str_replace('.', '_thumbnail.', $imagePath);
  48.                 $thumbnail = Image::make($image)->resize(256, null, function ($constraint) {
  49.                     $constraint->aspectRatio();
  50.                     $constraint->upsize();
  51.                 });
  52.                 $thumbnail->save(storage_path('app/public/' . $thumbnailPath));
  53.  
  54.                 Log::info('Image uploaded and resized successfully: ' . $imagePath);
  55.             } catch (\Exception $e) {
  56.                 Log::error('Image upload error: ' . $e->getMessage());
  57.             }
  58.         }
  59.  
  60.         $article->update([
  61.             'title_en' => $request->title_en,
  62.             'content_en' => $request->content_en,
  63.             'title_cs' => $request->title_cs,
  64.             'content_cs' => $request->content_cs,
  65.             'slug' => \Str::slug($request->title_en ?? $request->title_cs),
  66.             'author_id' => Auth::id(),
  67.             'background_image' => $imagePath,
  68.             'thumbnail_image' => $thumbnailPath,
  69.             'edited_on' => now(),
  70.         ]);
  71.  
  72.         return redirect()->route('articles.index');
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement