motorradig/motorradig/app/Http/Controllers/GalleryController.php

193 lines
5.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Gallery;
use Illuminate\Http\Request;
use Auth;
use App\Tour;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
class GalleryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Display the specified resource.
*
* @param \App\Gallery $gallery
* @return \Illuminate\Http\Response
*/
public function show($tour_id)
{
//dd("role:" . Auth::user()->role . " id:" . \Auth::id() . " has_tour:" . Auth::user()->has_tour($tour_id, \Auth::id()));
if(Auth::user()->role=="ADMIN" or Auth::user()->has_tour($tour_id, \Auth::id()) ) {
$imagelist_orig = Storage::disk('img_storage')->files("tour-$tour_id");
$imagelist=[];
foreach($imagelist_orig as $image) {
$arr = explode("/", $image);
$image = end($arr);
$before = $image;
if (str_ends_with($image, "_gross.jpg")) {
$image = substr($image, 0, -10); # Remove last 10 characters
array_push($imagelist, $image);
}
}
//dd($imagelist);
$tour_name = DB::select("select name from tours where id=".$tour_id)[0]->name;
return view('tour/gallery/show')->with("tour_id",$tour_id)
->with("imagelist",$imagelist)
->with("tour_name",$tour_name);
}
/*
tour-1
2023-04-24-ref1png.jpg
*/
return redirect('/tour/index_old');
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Gallery $gallery
* @return \Illuminate\Http\Response
*/
public function edit($tour_id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Gallery $gallery
* @return \Illuminate\Http\Response
*/
public function destroy($tour_id)
{
//
}
public function store(Request $request, $tour_id) {
$request->validate(
[
'files' => 'required'
]
);
//$filelist = join(', ', $request->files);
//dd($filelist);
$filelist="";
/*
foreach($request->files as $file) {
Log::info("File:");
Log::info($file);
foreach($file as $entry) {
Log::info($entry);
}
*/
//Log::info($request->files[0]) ;
//Log::info($file->getClientOriginalName());
//Log::info("####################################");
//Log::info($file->get("originalName"));
//$filelist = $filelist . "," . $file->originalName;
foreach($request->file('files') as $file)
{
//$name = time().'.'.$file->extension();
//$file->move(public_path().'/files/', $name);
//$data[] = $name;
//Log::info($name);
$orig_filename = $file->getClientOriginalName();
$this->storeImage($file,"tour_$tour_id", $orig_filename);
Log::info($orig_filename);
}
//$file= new File();
//$file->filenames=json_encode($data);
//$file->save();
return redirect("/tour/gallery/$tour_id");
}
public function delete_image($tour_id, $image_name) {
$image_name = explode(".", $image_name)[0];
$files = [$image_name . ".jpg" , $image_name . "_gross.jpg"];
$sourceDir = storage_path() . "/img_storage/tour-$tour_id";
$targetDir = storage_path() . "/img_storage/tour-$tour_id-DELETED";
if (!file_exists($targetDir)) {
mkdir($targetDir, 0777, true);
}
foreach($files as $file) {
rename("$sourceDir/$file", "$targetDir/$file");
}
return redirect("/tour/gallery/$tour_id");
}
///////////////////////////////////////////////////////////////////////////
private function storeImage($bild, $category, $name) {
$cat=Str::slug($category);
$name = explode(".",$name)[0];
$name=Str::slug($name);
$dirPath = storage_path() . "/img_storage/$cat";
if (!file_exists($dirPath)) {
mkdir($dirPath, 0777, true);
}
$bild = Image::make($bild);
$breite = $bild->width();
$hoehe = $bild->height();
if ($breite > $hoehe) {
Image::make($bild)
->widen(1200)
->save("$dirPath/$name"."_gross.jpg");
Image::make($bild)
->widen(128)
->save("$dirPath/$name".".jpg");
} else {
Image::make($bild)
->heighten(900)
->save("$dirPath/$name"."_gross.jpg");;
Image::make($bild)
->widen(192)
->save("$dirPath/$name".".jpg");
}
Log::info("Image saved as $dirPath/$name" . '_gross.jpg');
Log::info("Image saved as $dirPath/$name" . '.jpg');
}
}