paper = new Paper(); } /** * post::/printImage * @param Request $request * @return \Illuminate\Http\RedirectResponse * @throws \Exception */ public function imagePrint(Request $request) { if ($request->hasFile('photo')) { $file = $request->photo; } else { $file = $request->input('url'); } $image = Image::make($file); if ($image->width() > $image->height()) { $image->rotate(90); } $image = $image->greyscale() ->resize(370, null, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); $fileName = storage_path('temp.png'); $image->save($fileName, 100); $img = imagecreatefrompng($fileName); // imagefilter($img, IMG_FILTER_GRAYSCALE); $width = imagesx($img); $height = imagesy($img); $img_arr = array(); // Parse image (can be combined with dither stage, but combining them is slower.) for ($y = 0; $y < $height; $y++) { for ($x = 0; $x < $width; $x++) { $img_arr[$x][$y] = imagecolorat($img, $x, $y); } } // make a b/w output image. $output = imagecreate($width, $height); $black = imagecolorallocate($output, 0, 0, 0); //background color. $white = imagecolorallocate($output, 0xff, 0xff, 0xff); for ($y = 1; $y < $height - 3; $y++) { for ($x = 1; $x < $width - 3; $x++) { $old = $img_arr[$x][$y]; if ($old > 0xffffff * .5) { // This is the b/w threshold. Currently @ halfway between white and black. $new = 0xffffff; imagesetpixel($output, $x, $y, $white); // Only setting white pixels, because the image is already black. } else { $new = 0x000000; } $quant_error = $old - $new; $error_diffusion = (1 / 8) * $quant_error; //I can do this because this dither uses 1 value for the applied error diffusion. //dithering here. $img_arr[$x + 1][$y] += $error_diffusion; $img_arr[$x + 2][$y] += $error_diffusion; $img_arr[$x - 1][$y + 1] += $error_diffusion; $img_arr[$x][$y + 1] += $error_diffusion; $img_arr[$x + 1][$y + 1] += $error_diffusion; $img_arr[$x][$y + 2] += $error_diffusion; } } // plop out a png of the dithered image. // Header("Content-type: image/png"); imagepng($output, $fileName, 9); //to print to screen $img = EscposImage::load($fileName, false); $this->paper->getPrinter()->bitImage($img); $this->paper->getPrinter()->feed(4); return back(); } /** * get:/ * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function listView(Request $request) { $notes = DB::select('SELECT * FROM note WHERE type = "note" ORDER BY updated_at DESC'); $templates = DB::select('SELECT * FROM note WHERE type = "template" ORDER BY updated_at DESC'); return view('list', [ 'notes' => $notes, 'templates' => $templates, 'title' => $request->old('title'), 'text' => $request->old('text'), 'icon_selected' => $request->old('icon'), 'icons' => $this->paper->getIcons() ]); } /** * get:/filte/{$filter?} * @param Request $request * @param null $filter * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function filterView(Request $request, $filter = 'empty') { $notes = DB::select('SELECT *, CASE WHEN icon IS NULL THEN \'empty\' ELSE icon END AS new_icon FROM note WHERE type = "note" AND new_icon = :icon ORDER BY updated_at DESC', ['icon' => $filter]); $templates = DB::select('SELECT *, CASE WHEN icon IS NULL THEN \'empty\' ELSE icon END AS new_icon FROM note WHERE type = "template" AND new_icon = :icon ORDER BY updated_at DESC', ['icon' => $filter]); return view('list', [ 'filter' => $filter, 'notes' => $notes, 'templates' => $templates, 'title' => $request->old('title'), 'text' => $request->old('text'), 'icon_selected' => $request->old('icon'), 'icons' => $this->paper->getIcons() ]); } /** * post::/print/{id} * @param Request $request * @param $id * @return \Illuminate\Http\RedirectResponse */ public function printText(Request $request, $id) { $note = $this->printNote($id); $request->session()->flash('print_status', 'Wydrukowano notatkę: ' . $note->topic . '!'); return back(); } public function printNote($noteId) { $note = DB::table('note')->where('id', $noteId)->first(); $this->paper->sendPrint($note->topic, $note->text, $note->icon); return $note; } public function noteLast() { $note = DB::table('note')->orderBy('id', 'desc')->first(); $this->printNote($note->id); } private function templateReplace($input) { return str_replace(['[d]', '[m]', '[y]', '[h]', '[i]', '[s]'], [date('d'), date('m'), date('Y'), date('H'), date('i'), date('s')], $input); } /** * get::/edit/{id}/{slug} * post::/edit/{id}/{slug} * @param Request $request * @param $id * @param $slug * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View */ public function edit(Request $request, $id, $slug) { $note = DB::table('note')->where('id', $id)->first(); if ($request->isMethod('post')) { if ($request->exists('save') || $request->exists('save_template')) { if ($note->type == $this::TEMPLATE) { $id = DB::table('note') ->insertGetId([ 'topic' => $this->templateReplace($request->input('title')), 'topic_slug' => str_slug($request->input('title'), '_'), 'text' => $this->templateReplace($request->input('text')), 'icon' => $request->input('icon'), 'type' => self::NOTE, 'created_at' => time(), 'updated_at' => time() ]); $note = DB::table('note')->where('id', $id)->first(); } else { DB::table('note') ->where('id', $note->id) ->update([ 'topic' => $request->input('title'), 'topic_slug' => str_slug($request->input('title'), '_'), 'text' => $request->input('text'), 'icon' => $request->input('icon'), 'updated_at' => time(), ]); } } elseif ($request->exists('delete')) { DB::table('note') ->where('id', $note->id) ->delete(); return redirect()->route('list'); } elseif ($request->exists('print')) { $this->paper->sendPrint($request->input('title'), $request->input('text'), $request->input('icon')); $request->session()->flash('print_status', 'Wydrukowano notatkę: ' . (strlen($request->input('title')) ? $request->input('title') : substr($request->input('text'), 0, 20)) . '!'); } return redirect()->route('edit', ['id' => $note->id, 'slug' => $note->topic_slug]); } else { return view('edit', [ 'title' => $note->topic, 'text' => $note->text, 'icon_selected' => $note->icon, 'type' => $note->type, 'id' => $note->id, 'icons' => $this->paper->getIcons(), 'topic_slug' => $note->topic_slug, ]); } } /** * post::/ * @param Request $request * @return $this|\Illuminate\Http\RedirectResponse */ public function main(Request $request) { if ($request->exists('save')) { $id = DB::table('note') ->insertGetId([ 'topic' => $request->input('title'), 'topic_slug' => str_slug($request->input('title'), '_'), 'text' => $request->input('text'), 'icon' => $request->input('icon'), 'type' => self::NOTE, 'created_at' => time(), 'updated_at' => time() ]); $note = DB::table('note')->where('id', $id)->first(); return redirect()->route('edit', ['id' => $note->id, 'slug' => $note->topic_slug]); } elseif ($request->exists('save_template')) { $id = DB::table('note') ->insertGetId([ 'topic' => $request->input('title'), 'topic_slug' => str_slug($request->input('title'), '_'), 'text' => $request->input('text'), 'icon' => $request->input('icon'), 'type' => self::TEMPLATE, 'created_at' => time(), 'updated_at' => time() ]); $note = DB::table('note')->where('id', $id)->first(); return redirect()->route('edit', ['id' => $note->id, 'slug' => $note->topic_slug]); } else { $this->paper->sendPrint($request->input('title'), $request->input('text'), $request->input('icon')); $request->session()->flash('print_status', 'Wydrukowano szybką notatkę: ' . (strlen($request->input('title')) ? $request->input('title') : substr($request->input('text'), 0, 20)) . '!'); return back()->withInput(); } } }