Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Logo;
  6. use App\Customer;
  7. use App\User;
  8. use App\ConvertStep;
  9. use App\Transfer;
  10. use App\Color;
  11. use App\Size;
  12. use App\Quantity;
  13. use App\Product;
  14.  
  15. use \CloudConvert\Api;
  16. use \CloudConvert\Process;
  17. use Exception;
  18. use Intervention\Image\ImageManager as Image;
  19.  
  20. use GuzzleHttp\Client;
  21. use Illuminate\Database\Eloquent\ModelNotFoundException;
  22. use Illuminate\Http\Request;
  23. use Illuminate\Http\UploadedFile;
  24. use Illuminate\Support\Arr;
  25. use Illuminate\Support\Facades\Log;
  26. use Illuminate\Support\Facades\Response;
  27. use Illuminate\Support\Facades\Storage;
  28. use Illuminate\Support\Facades\Input;
  29. use DB;
  30.  
  31. class LogoController extends Controller
  32. {
  33. protected $configs = [
  34.  
  35. 'apiKey' => 'CJzz1TCJbuTivgz9HX0xwC10xCMznnrV9hcCpDMe395pa4k8aOr8CGVvqcL6Lqfx',
  36. 'outputFormat' => 'svg',
  37. 'outputPath' => 'logos',
  38. 'inputPath' => 'logos',
  39. 'bitmap' => ['tiff', 'jpg', 'jpeg', 'gif', 'png'],
  40. 'minwidth' => 100 // 1080
  41.  
  42. ];
  43.  
  44. public function __construct(){
  45. $this->middleware('auth')->except(['callback']);
  46.  
  47. }
  48.  
  49. public function index(Request $request){
  50.  
  51. $q = $request->get('q', false);
  52. $customer = $request->get('customer', false);
  53. $customers = Customer::where('id', '!=', User::where('role', '=', 'superadmin')->first()->customer_id)->get();
  54. $user = auth()->user();
  55.  
  56. if($user->isSuper()){
  57. if(!$customer){
  58. $logos = null;
  59.  
  60. } else{
  61.  
  62. if($q){
  63. $logos= Logo::where('customer_id', $customer)
  64. ->where('description', 'like', '%' . $q . '%')
  65. ->orderBy('created_at')
  66. ->simplePaginate(8)
  67. ->appends('q', $q)
  68. ->appends('customer', $customer);
  69. } else{
  70. $logos= Logo::query()
  71. ->where('customer_id', $customer)
  72. ->orderBy('created_at')
  73. ->simplePaginate(8)
  74. ->appends('customer', $customer);
  75. }
  76.  
  77. }
  78.  
  79. } else{
  80. $logos = Logo::query()
  81. ->where('customer_id', $user->customer_id)
  82. ->when($q, function ($query) use ($q) {
  83. return $query->where('name', 'like', '%' . $q . '%')
  84. ->orWhere('description', 'like', '%' . $q . '%')
  85. ->orWhere('size', 'like', '%' . $q . '%');
  86.  
  87. })
  88. ->orderBy('created_at')
  89. ->simplePaginate(8)
  90. ->appends('q', $q);
  91. }
  92.  
  93. return view('pages.logobank.product.list', [
  94. 'title' => __('Logobank'),
  95. 'logos' => $logos,
  96. 'query' => $q,
  97. 'customers' => $customers,
  98. 'user' => $user
  99. ]);
  100. }
  101.  
  102. public function store(Request $r){
  103.  
  104. if(Input::file('logo')){
  105. $count = count(Input::file('logo'));
  106. for($i = 0; $i < $count; $i++){
  107.  
  108. $file = Input::file('logo')[$i];
  109. $input = $this->configs['inputPath'];
  110. $logo = new Logo();
  111. $user = auth()->user();
  112. $logo->fill(array(
  113. 'name' => $r['name'][$i],
  114. 'description' => $r['description'][$i],
  115. 'size' => $r['size'][$i],
  116. 'logo' => $file,
  117. 'user_id' => null
  118. ));
  119.  
  120. if($logo->validate()){
  121.  
  122. // Save original file
  123. $logo->file_path = $file->store($input, ['disk' => 'public']);
  124. $logo->customer_id = ($r->customer_id) ? $r->customer_id : $user->customer_id;
  125. $logo->user_id = auth()->id();
  126. unset($logo->logo);
  127. $type = explode('/', $file->getMimeType());
  128.  
  129. if(!in_array(explode('-', $type[1])[1], $this->configs['bitmap']) && $logo->save()){
  130.  
  131. $logo->convert_step = ConvertStep::Convert()->getValue();
  132.  
  133. $api = new Api($this->configs['apiKey']);
  134.  
  135. $process = $api->createProcess([
  136. "inputformat" => $file->extension(),
  137. "outputformat" => "png",
  138. ]);
  139.  
  140. $process->start([
  141. 'outputformat' => $this->configs['outputFormat'],
  142. 'converteroptions' => [
  143. 'quality' => 75,
  144. "resize" => "400x400",
  145. ],
  146. 'input' => 'download',
  147. 'file' => Storage::disk('public')->url($logo->file_path),
  148. 'callback' => route('logo.callback', ['id' => $logo->id])
  149.  
  150. ]);
  151.  
  152. $logo->convert_step = ConvertStep::Started()->getValue();
  153.  
  154. $logo->bitmap = false;
  155.  
  156. $logo->update(['status']);
  157.  
  158. $message = __('Logo(s) uploaded.');
  159.  
  160. if($i == $count){
  161.  
  162. return back()->with('success', $message);
  163.  
  164. }
  165.  
  166. } else if(getimagesize($file)[0] >= $this->configs['minwidth']) {
  167.  
  168. $size = getimagesize($file);
  169.  
  170. $logo->dimension = ($size) ? $size[0] . 'x' . $size[1] : null;
  171.  
  172. $logo->ratio = ($size) ? $size[0] / $size[1] : null;
  173.  
  174. $logo->name = $r->name[0] . ' (' . $logo->dimension . ')';
  175.  
  176. if($logo->save()){
  177.  
  178. $filename = basename($logo->file_path, $file->extension()) . 'png';
  179.  
  180. $path = Storage::disk('public')->url($logo->file_path);
  181.  
  182. $logo->image_path = 'logos/' . $filename;
  183.  
  184. $resized = Image::make($file->getRealPath());
  185.  
  186. $$resized->resize(200, 200, function ($constraint) {
  187. $constraint->aspectRatio();
  188. })->save($path);
  189.  
  190. $logo->convert_step = ConvertStep::Finished()->getValue();
  191.  
  192. $logo->update();
  193.  
  194. } else{
  195.  
  196. return back()->with('error', 'Picture must be at least ' . $this->configs['minwidth'] . 'px wide');
  197.  
  198. }
  199. }
  200. }
  201. };
  202.  
  203. return back()
  204. ->withErrors($logo->errors);
  205. }
  206.  
  207. return back()->with('error', 'No logo was uploaded');
  208.  
  209. }
  210.  
  211. public function callback(Request $request, string $id){
  212.  
  213. $log = Log::channel('callback');
  214.  
  215. $log->info(['id' => $id, 'request' => $request->all()]);
  216.  
  217. $api = new Api($this->configs['apiKey']);
  218.  
  219. try {
  220.  
  221. $logo = Logo::query()->findOrFail($id);
  222.  
  223. $step = $request->get('step');
  224.  
  225. if ($step === ConvertStep::Finished()->getValue()) {
  226.  
  227. $process = new Process($api, $_REQUEST['url']);
  228.  
  229. $format = pathinfo($logo->file_path);
  230.  
  231. $path = str_replace($format['extension'], $this->configs['outputFormat'], $logo->file_path);
  232.  
  233. $log->info(['path' => public_path('/uploads/' . $path)]);
  234.  
  235. $process->refresh()->download(public_path('/uploads/' . $path));
  236.  
  237. $logo->image_path = $path;
  238.  
  239. $logo->convert_step = $step;
  240.  
  241. $logo->update();
  242.  
  243. }
  244.  
  245. } catch (ModelNotFoundException $ex) {
  246. $log->info($ex);
  247. return Response::make($ex->getMessage(), 404);
  248. } catch (Exception $ex) {
  249. $log->info($ex);
  250. return Response::make($ex->getMessage(), 500);
  251. }
  252.  
  253. return Response::make(null, 200);
  254.  
  255. }
  256.  
  257. public function destroy(int $id){
  258.  
  259. $logo = Logo::findOrFail($id);
  260.  
  261. if($logo->delete()) {
  262.  
  263. if(Storage::disk('public')->exists($logo->file_path)){
  264.  
  265. Storage::disk('public')->delete($logo->file_path);
  266.  
  267. }
  268.  
  269. $message = __('Logo ":name" has been deleted.', ['name' => $logo->name]);
  270.  
  271. return back()->with('success', $message);
  272.  
  273. }
  274.  
  275. return back();
  276. }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement