Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use App\Logo;
- use App\Customer;
- use App\User;
- use App\ConvertStep;
- use App\Transfer;
- use App\Color;
- use App\Size;
- use App\Quantity;
- use App\Product;
- use \CloudConvert\Api;
- use \CloudConvert\Process;
- use Exception;
- use Intervention\Image\ImageManager as Image;
- use GuzzleHttp\Client;
- use Illuminate\Database\Eloquent\ModelNotFoundException;
- use Illuminate\Http\Request;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Response;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\Input;
- use DB;
- class LogoController extends Controller
- {
- protected $configs = [
- 'apiKey' => 'CJzz1TCJbuTivgz9HX0xwC10xCMznnrV9hcCpDMe395pa4k8aOr8CGVvqcL6Lqfx',
- 'outputFormat' => 'svg',
- 'outputPath' => 'logos',
- 'inputPath' => 'logos',
- 'bitmap' => ['tiff', 'jpg', 'jpeg', 'gif', 'png'],
- 'minwidth' => 100 // 1080
- ];
- public function __construct(){
- $this->middleware('auth')->except(['callback']);
- }
- public function index(Request $request){
- $q = $request->get('q', false);
- $customer = $request->get('customer', false);
- $customers = Customer::where('id', '!=', User::where('role', '=', 'superadmin')->first()->customer_id)->get();
- $user = auth()->user();
- if($user->isSuper()){
- if(!$customer){
- $logos = null;
- } else{
- if($q){
- $logos= Logo::where('customer_id', $customer)
- ->where('description', 'like', '%' . $q . '%')
- ->orderBy('created_at')
- ->simplePaginate(8)
- ->appends('q', $q)
- ->appends('customer', $customer);
- } else{
- $logos= Logo::query()
- ->where('customer_id', $customer)
- ->orderBy('created_at')
- ->simplePaginate(8)
- ->appends('customer', $customer);
- }
- }
- } else{
- $logos = Logo::query()
- ->where('customer_id', $user->customer_id)
- ->when($q, function ($query) use ($q) {
- return $query->where('name', 'like', '%' . $q . '%')
- ->orWhere('description', 'like', '%' . $q . '%')
- ->orWhere('size', 'like', '%' . $q . '%');
- })
- ->orderBy('created_at')
- ->simplePaginate(8)
- ->appends('q', $q);
- }
- return view('pages.logobank.product.list', [
- 'title' => __('Logobank'),
- 'logos' => $logos,
- 'query' => $q,
- 'customers' => $customers,
- 'user' => $user
- ]);
- }
- public function store(Request $r){
- if(Input::file('logo')){
- $count = count(Input::file('logo'));
- for($i = 0; $i < $count; $i++){
- $file = Input::file('logo')[$i];
- $input = $this->configs['inputPath'];
- $logo = new Logo();
- $user = auth()->user();
- $logo->fill(array(
- 'name' => $r['name'][$i],
- 'description' => $r['description'][$i],
- 'size' => $r['size'][$i],
- 'logo' => $file,
- 'user_id' => null
- ));
- if($logo->validate()){
- // Save original file
- $logo->file_path = $file->store($input, ['disk' => 'public']);
- $logo->customer_id = ($r->customer_id) ? $r->customer_id : $user->customer_id;
- $logo->user_id = auth()->id();
- unset($logo->logo);
- $type = explode('/', $file->getMimeType());
- if(!in_array(explode('-', $type[1])[1], $this->configs['bitmap']) && $logo->save()){
- $logo->convert_step = ConvertStep::Convert()->getValue();
- $api = new Api($this->configs['apiKey']);
- $process = $api->createProcess([
- "inputformat" => $file->extension(),
- "outputformat" => "png",
- ]);
- $process->start([
- 'outputformat' => $this->configs['outputFormat'],
- 'converteroptions' => [
- 'quality' => 75,
- "resize" => "400x400",
- ],
- 'input' => 'download',
- 'file' => Storage::disk('public')->url($logo->file_path),
- 'callback' => route('logo.callback', ['id' => $logo->id])
- ]);
- $logo->convert_step = ConvertStep::Started()->getValue();
- $logo->bitmap = false;
- $logo->update(['status']);
- $message = __('Logo(s) uploaded.');
- if($i == $count){
- return back()->with('success', $message);
- }
- } else if(getimagesize($file)[0] >= $this->configs['minwidth']) {
- $size = getimagesize($file);
- $logo->dimension = ($size) ? $size[0] . 'x' . $size[1] : null;
- $logo->ratio = ($size) ? $size[0] / $size[1] : null;
- $logo->name = $r->name[0] . ' (' . $logo->dimension . ')';
- if($logo->save()){
- $filename = basename($logo->file_path, $file->extension()) . 'png';
- $path = Storage::disk('public')->url($logo->file_path);
- $logo->image_path = 'logos/' . $filename;
- $resized = Image::make($file->getRealPath());
- $$resized->resize(200, 200, function ($constraint) {
- $constraint->aspectRatio();
- })->save($path);
- $logo->convert_step = ConvertStep::Finished()->getValue();
- $logo->update();
- } else{
- return back()->with('error', 'Picture must be at least ' . $this->configs['minwidth'] . 'px wide');
- }
- }
- }
- };
- return back()
- ->withErrors($logo->errors);
- }
- return back()->with('error', 'No logo was uploaded');
- }
- public function callback(Request $request, string $id){
- $log = Log::channel('callback');
- $log->info(['id' => $id, 'request' => $request->all()]);
- $api = new Api($this->configs['apiKey']);
- try {
- $logo = Logo::query()->findOrFail($id);
- $step = $request->get('step');
- if ($step === ConvertStep::Finished()->getValue()) {
- $process = new Process($api, $_REQUEST['url']);
- $format = pathinfo($logo->file_path);
- $path = str_replace($format['extension'], $this->configs['outputFormat'], $logo->file_path);
- $log->info(['path' => public_path('/uploads/' . $path)]);
- $process->refresh()->download(public_path('/uploads/' . $path));
- $logo->image_path = $path;
- $logo->convert_step = $step;
- $logo->update();
- }
- } catch (ModelNotFoundException $ex) {
- $log->info($ex);
- return Response::make($ex->getMessage(), 404);
- } catch (Exception $ex) {
- $log->info($ex);
- return Response::make($ex->getMessage(), 500);
- }
- return Response::make(null, 200);
- }
- public function destroy(int $id){
- $logo = Logo::findOrFail($id);
- if($logo->delete()) {
- if(Storage::disk('public')->exists($logo->file_path)){
- Storage::disk('public')->delete($logo->file_path);
- }
- $message = __('Logo ":name" has been deleted.', ['name' => $logo->name]);
- return back()->with('success', $message);
- }
- return back();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement