/
var
/
www
/
html
/
gnet
/
gnet
/
App
/
Http
/
Controllers
/
Upload File
HOME
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\ProductImage; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Validator; use App\Models\Product; use App\Models\Category; use App\Models\Brand; use App\Models\ProductType; use App\Models\Tags; use Intervention\Image\Facades\Image; class MasterImageProductController extends Controller { public function __construct() { $this->middleware('auth'); } public function index() { $urltambah = '/tambah-master-image'; $productImage = ProductImage::all(); $product = Product::all(); $subproduct = ProductType::all(); $category = Category::all(); $brand = Brand::all(); $tags = Tags::all(); // $edit_view = view('edit.edit-kategori'); $create_view = view('create.create-image', compact('product', 'subproduct')); $url_edit = '/master-image/{id}'; $tableHeader = ['Product', 'Image', 'Alt Text', 'Action']; $url = '/data-image'; $columns = [ ["data" => "product"], ["data" => "image_hd"], ["data" => "alt_text"], ["data" => "action"] ]; return view('admin', compact('create_view', 'urltambah', 'url', 'columns', 'tableHeader', 'productImage', 'url_edit', 'product', 'brand', 'category', 'tags')); } public function getData(Request $request) { $draw = $request->get('draw'); $start = $request->get("start"); $rowperpage = $request->get("length"); $columnIndex_arr = $request->get('order'); $columnName_arr = $request->get('columns'); $order_arr = $request->get('order'); $search_arr = $request->get('search'); $columnIndex = $columnIndex_arr[0]['column']; $columnName = $columnName_arr[$columnIndex]['data']; $columnSortOrder = $order_arr[0]['dir']; $searchValue = $search_arr['value']; // Total records $totalRecords = DB::table('product_image') // ->leftJoin('users', 'users.id', '=', 'product_image.user_id') ->select('product_image.*') ->count(); $totalRecordswithFilter = DB::table('product_image') // ->leftJoin('users', 'users.id', '=', 'product_image.user_id') ->select('product_image.*') ->leftJoin('product', 'product_image.product_id', '=', 'product.id') ->leftJoin('product_types', 'product_image.product_type_id', '=', 'product_types.id') ->where(function ($query) use ($searchValue) { $query->where('product_image.product_id', 'like', '%' . $searchValue . '%') ->orWhere('product_image.image_hd', 'like', '%' . $searchValue . '%') ->orWhere('product_image.alt_text', 'like', '%' . $searchValue . '%') ->orWhere('product.name', 'like', '%' . $searchValue . '%') ->orWhere('product_types.name', 'like', '%' . $searchValue . '%'); }) ->count(); $records = DB::table('product_image') // ->leftJoin('users', 'users.id', '=', 'product_image.user_id') ->select( 'product_image.*', DB::raw("(SELECT (product.name) FROM product WHERE product_image.product_id = product.id) AS product"), DB::raw("(SELECT (product_types.name) FROM product_types WHERE product_image.product_type_id = product_types.id) AS subproduct"), DB::raw("CONCAT('<ul class=\"list-inline m-0\"><li class=\"list-inline-item\"><a id=\"openCategoryModalBtnEdit\" href=\"/master-image/', product_image.id, '/edit\" class=\"btn btn-success shadow btn-xs sharp me-1 btn-edit\" data-toggle=\"modal\" data-target=\"#modal-edit\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"Edit\"><i class=\"fa fa-edit\"></i></a></li><li class=\"list-inline-item\"><a href=\"/master-image/', product_image.id, '\" class=\"btn btn-danger shadow btn-xs sharp me-1 btn-delete\" data-toggle=\"tooltip\" data-placement=\"top\" title=\"Delete\"><i class=\"fa fa-trash\"></i></a></li></ul>') AS action") ) ->leftJoin('product', 'product_image.product_id', '=', 'product.id') ->leftJoin('product_types', 'product_image.product_type_id', '=', 'product_types.id') ->where(function ($query) use ($searchValue) { $query->where('product_image.product_id', 'like', '%' . $searchValue . '%') ->orWhere('product_image.image_hd', 'like', '%' . $searchValue . '%') ->orWhere('product_image.alt_text', 'like', '%' . $searchValue . '%') ->orWhere('product.name', 'like', '%' . $searchValue . '%') ->orWhere('product_types.name', 'like', '%' . $searchValue . '%'); }) ->orderBy($columnName, $columnSortOrder) ->skip($start) ->take($rowperpage) ->get(); $data_arr = array(); $sno = $start + 1; foreach ($records as $record) { $id = $record->id; $product = is_null($record->product) ? $record->subproduct : $record->product; $image_hd = $record->image_hd; $alt_text = $record->alt_text; $data_arr[] = array( "id" => $id, "product" => $product, "image_hd" => $image_hd, "alt_text" => $alt_text, "action" => $record->action ); } $response = array( "draw" => intval($draw), "iTotalRecords" => $totalRecords, "iTotalDisplayRecords" => $totalRecordswithFilter, "aaData" => $data_arr ); echo json_encode($response); exit; return response()->json($response); } private function compressImage($source, $destination, $quality) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source); imagejpeg($image, $destination, $quality); } public function store(Request $request) { $validatedData = Validator::make($request->all(), [ 'product_id' => 'nullable', 'product_type_id' => 'nullable', 'image_hd' => 'required', 'image_low' => 'nullable', 'image_hd_mobile' => 'nullable', 'image_low_mobile' => 'nullable', 'alt_text' => 'required', ]); if ($validatedData->fails()) { return response()->json($validatedData->errors(), 422); } $product_id = $request->input('product_id'); $product_type_id = $request->input('product_type_id'); $alt_text = $request->input('alt_text'); // menambahkan image untuk product type (subproduk) if ($request->input('image_group') === 'Subproduct') { $product_id = null; } $productImage = new ProductImage(); $productImage->product_id = $product_id; $productImage->product_type_id = $product_type_id; if ($request->hasFile('image_hd')) { $file = $request->file('image_hd'); $productImage->image_hd = $this->uploadImage($file, 'hd'); } // Process image_low if ($request->hasFile('image_low')) { $file = $request->file('image_low'); $productImage->image_low = $this->uploadImage($file, 'low'); }else{ // $this->compressImage($productImage->image_hd,strstr($productImage->image_hd, public_path('assets/img/products'),public_path('assets/img/products/low_')),1); // $productImage->image_low = strstr($productImage->image_hd, public_path('assets/img/products'),public_path('assets/img/products/low_')); $productImage->image_low = 'low_' . $productImage->image_hd; $this->compressImage(public_path('assets/img/products/' . $productImage->image_hd), public_path('assets/img/products/low_' . $productImage->image_hd), 1); } // Process image_hd_mobile if ($request->hasFile('image_hd_mobile')) { $file = $request->file('image_hd_mobile'); $productImage->image_hd_mobile = $this->uploadImage($file, 'hd_mobile'); } // Process image_low_mobile if ($request->hasFile('image_low_mobile')) { $file = $request->file('image_low_mobile'); $productImage->image_low_mobile = $this->uploadImage($file, 'low_mobile'); } // $productImage->wa_text = $wa_text; $productImage->alt_text = $alt_text; $productImage->save(); return response()->json([ 'success' => true, 'message' => 'Data berhasil ditambahkan.' ]); } public function edit($id) { $productImage = ProductImage::find($id); $product = Product::all(); $subproduct = ProductType::all(); return view('edit.edit-image', compact('productImage', 'product', 'subproduct')); } public function update(Request $request, $id) { $productImage = ProductImage::find($id); $validatedData = Validator::make($request->all(), [ 'product_id' => 'nullable', 'product_type_id' => 'nullable', 'image_hd' => 'nullable', 'image_low' => 'nullable', 'image_hd_mobile' => 'nullable', 'image_low_mobile' => 'nullable', 'alt_text' => 'required', // 'wa_text' => 'nullable', ]); if ($validatedData->fails()) { return response()->json($validatedData->errors(), 422); } $product_id = $request->input('product_id'); $product_type_id = $request->input('product_type_id'); $alt_text = $request->input('alt_text'); // $wa_text = $request->input('wa_text'); // Process image_hd_mobile if ($request->hasFile('image_hd_mobile')) { $file = $request->file('image_hd_mobile'); $image_hd_mobile = $this->processImage($file, $productImage->image_hd_mobile); } else { $image_hd_mobile = $productImage->image_hd_mobile; } // Process image_low_mobile if ($request->hasFile('image_low_mobile')) { $file = $request->file('image_low_mobile'); $image_low_mobile = $this->processImage($file, $productImage->image_low_mobile); } else { $image_low_mobile = $productImage->image_low_mobile; } // Process image_hd if ($request->hasFile('image_hd')) { $file = $request->file('image_hd'); $image_hd = $this->processImage($file, $productImage->image_hd); } else { $image_hd = $productImage->image_hd; } // Process image_low if ($request->hasFile('image_low')) { $file = $request->file('image_low'); $image_low = $this->processImage($file, $productImage->image_low); } else { $image_low = $productImage->image_low; } // Update data productImage $productImage->product_id = $product_id; $productImage->product_type_id = $product_type_id; $productImage->image_hd = $image_hd; $productImage->image_low = $image_low; $productImage->image_hd_mobile = $image_hd_mobile; $productImage->image_low_mobile = $image_low_mobile; // $productImage->wa_text = $wa_text; $productImage->alt_text = $alt_text; $productImage->save(); return response()->json([ 'success' => true, 'message' => 'Data berhasil di-update.' ]); } public function destroy($id) { $productImage = ProductImage::find($id); $imageProperties = [ $productImage->image_hd, $productImage->image_low, $productImage->image_hd_mobile, $productImage->image_low_mobile ]; foreach ($imageProperties as $imageProperty) { if ($imageProperty) { $oldFilePath = public_path('assets/img/products/' . $imageProperty); if (file_exists($oldFilePath)) { unlink($oldFilePath); } } } ProductImage::where('id', $id)->delete(); //return response return response()->json([ 'success' => true, 'message' => 'Data Berhasil Dihapus!.', ]); } private function uploadImage($file, $suffix) { try { if ($file && $file->isValid()) { // Generate a unique filename $fileName = time() . '_' . $suffix . '_' . $file->getClientOriginalName(); // Move the file to the public directory $file->move(public_path('assets/img/products'), $fileName); // Resize and save the image Image::make(public_path('assets/img/products/' . $fileName))->resize(800, 600)->save(); return $fileName; } return null; } catch (\Exception $e) { \Log::error('Error uploading file: ' . $e->getMessage()); return null; } } private function processImage($file, $oldFileName) { // Check if the file is valid if ($file->isValid()) { // Generate a unique file name $fileName = time() . '_' . $file->getClientOriginalName(); // Move the file to the destination directory $file->move(public_path('assets/img/products/'), $fileName); // Delete the old file if it exists if ($oldFileName) { $oldFilePath = public_path('assets/img/products/' . $oldFileName); if (file_exists($oldFilePath)) { unlink($oldFilePath); } } return $fileName; } return $oldFileName; } }