Created
June 16, 2020 11:31
-
-
Save FragsterAt/0075a77962abdfb4855129b663207636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\ImageSet; | |
use App\PuzzleImage; | |
use Exception; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Facades\Log; | |
use Intervention\Image\Facades\Image; | |
class PuzzleImageController extends Controller | |
{ | |
public function __construct() | |
{ | |
$this->authorizeResource(PuzzleImage::class, 'puzzle_image'); | |
} | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index(ImageSet $imageSet) | |
{ | |
} | |
/** | |
* Show the form for creating a new resource. | |
* @param \App\ImageSet $imageSet | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function create(ImageSet $imageSet) | |
{ | |
return view('puzzle_images/create_edit', ['imageSet' => $imageSet, 'puzzleImage' => new PuzzleImage]); | |
} | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request, ImageSet $imageSet) | |
{ | |
$puzzleImage = new PuzzleImage(); | |
$puzzleImage->user()->associate($imageSet->user); | |
$puzzleImage->imageSet()->associate($imageSet); | |
$puzzleImage->fill( | |
$request->only('name', 'active', 'order', 'width', 'height', 'slice_h', 'slice_v', 'pieces') | |
); | |
if (gettype($puzzleImage['pieces']) == 'string') | |
$puzzleImage['pieces'] = json_decode($puzzleImage['pieces']); | |
$puzzleImage->save(); // для дальнейшей работы нужен id | |
$this->storeUpdate($request, $imageSet, $puzzleImage); | |
return redirect(action('PuzzleImageController@edit', ['image_set' => $imageSet, 'puzzle_image' => $puzzleImage])); | |
} | |
protected function storeUpdate(Request $request, ImageSet $imageSet, PuzzleImage $puzzleImage) | |
{ | |
$savePath = "puzzle_images/{$imageSet['id']}/{$puzzleImage['id']}/"; | |
if ($request->hasFile('image')) { | |
$request->image->storeAs($savePath, 'original.jpg'); | |
} | |
if ($request->hasFile('background')) { | |
$request->background->storeAs($savePath, 'background_original.jpg'); | |
} | |
if (Storage::exists($savePath . '/original.jpg')) { | |
$img = Image::make(Storage::get($savePath . 'original.jpg')); | |
$puzzleImage->original_width = $img->width(); | |
$puzzleImage->original_height = $img->height(); | |
if ($puzzleImage->width) { | |
$puzzleImage->height = round($puzzleImage->width / $puzzleImage->original_width * $puzzleImage->original_height); | |
} else { | |
$puzzleImage->width = round($puzzleImage->height / $puzzleImage->original_height * $puzzleImage->original_width); | |
} | |
$h_width = round($puzzleImage->width / $puzzleImage->slice_h); | |
$v_height = round($puzzleImage->height / $puzzleImage->slice_v); | |
$puzzleImage->width = $h_width * $puzzleImage->slice_h; | |
$puzzleImage->height = $v_height * $puzzleImage->slice_v; | |
$img->resize($puzzleImage->width, $puzzleImage->height); | |
$img->backup(); | |
Storage::deleteDirectory($savePath . "/pieces"); | |
for ($h = 0; $h < $puzzleImage->slice_h; $h++) { | |
for ($v = 0; $v < $puzzleImage->slice_v; $v++) { | |
$img->crop($h_width, $v_height, $h * $h_width, $v * $v_height); | |
Storage::put($savePath . '/pieces/' . ($h + $v * $puzzleImage->slice_h) . ".jpg", (string) $img->encode('jpg', 92)); | |
$img->reset(); | |
} | |
} | |
} | |
if (Storage::exists($savePath . 'background_original.jpg')) { | |
Log::debug('resizing bg'); | |
$img = Image::make(Storage::get($savePath . 'background_original.jpg')); | |
$img->fit($puzzleImage->width, $puzzleImage->height); | |
Storage::put($savePath . "/background_resized.jpg", (string) $img->encode('jpg', 92)); | |
// для обхода глюка с прозрачностью прямоугольников | |
$img = Image::make(Storage::get($savePath . 'background_resized.jpg')); | |
for ($h = 0; $h < $puzzleImage->slice_h; $h++) { | |
for ($v = 0; $v < $puzzleImage->slice_v; $v++) { | |
$x = $h * $h_width + $h_width / 2; | |
$y = $v * $v_height + $v_height / 2; | |
$s = $v_height / 4; | |
Log::debug([$x, $y, $s]); | |
$img->rectangle($x - $s, $y - $s, $x + $s, $y + $s, function ($draw) { | |
$draw->background('rgba(0, 0, 0, 0.5)'); | |
}); | |
$img->text(1 + $h + $v * $puzzleImage->slice_h, $x, $y, function ($font) use ($s) { | |
$font->file(resource_path('fonts/DejaVuSans.ttf')); | |
$font->size(round($s)); | |
$font->color('#FFFFFF'); | |
$font->align('center'); | |
$font->valign('middle'); | |
}); | |
} | |
} | |
Storage::put($savePath . "/background.jpg", (string) $img->encode('jpg', 92)); | |
} | |
if (Storage::exists($savePath . '/original.jpg') && Storage::exists($savePath . 'background.jpg')) { | |
$img = Image::make(Storage::get($savePath . 'background_resized.jpg')); | |
$img_mixed = Image::make(Storage::get($savePath . 'background.jpg')); | |
$img_mixed_opened = Image::make(Storage::get($savePath . 'background.jpg')); | |
$h_width = round($puzzleImage->width / $puzzleImage->slice_h); | |
$v_height = round($puzzleImage->height / $puzzleImage->slice_v); | |
$slice_h = (int) $puzzleImage->slice_h; | |
foreach ($puzzleImage->pieces as $key => $value) { | |
$piece = Image::make(Storage::get($savePath . '/pieces/' . $value['num'] . '.jpg')); | |
$x = $h_width * ($key % $slice_h); | |
$y = $v_height * floor($key / $slice_h); | |
$img_mixed_opened->insert($piece, null, $x, $y); | |
if (!$value['show']) continue; | |
$img_mixed->insert($piece, null, $x, $y); | |
$x = $h_width * ($value['num'] % $slice_h); | |
$y = $v_height * floor($value['num'] / $slice_h); | |
$img->insert($piece, 'top-left', $x, $y); | |
} | |
Storage::put($savePath . "/current.jpg", (string) $img->encode('jpg', 92)); | |
Storage::put($savePath . "/current_mixed.jpg", (string) $img_mixed->encode('jpg', 92)); | |
Storage::put($savePath . "/mixed.jpg", (string) $img_mixed_opened->encode('jpg', 92)); | |
} | |
$puzzleImage->save(); | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param \App\PuzzleImage $puzzleImage | |
* @return \Illuminate\Http\Response | |
*/ | |
public function show(PuzzleImage $puzzleImage) | |
{ | |
// | |
} | |
/** | |
* Display the specified resource. | |
* | |
* @param \App\PuzzleImage $puzzleImage | |
* @return \Illuminate\Http\Response | |
*/ | |
public function current(ImageSet $imageSet, PuzzleImage $puzzleImage) | |
{ | |
$this->authorize('view', $puzzleImage); | |
$savePath = "puzzle_images/{$imageSet['id']}/{$puzzleImage['id']}/"; | |
return response(Storage::get($savePath . 'current.jpg'))->header('Content-Type', Storage::mimeType($savePath . 'current.jpg')); | |
} | |
public function currentMixed(ImageSet $imageSet, PuzzleImage $puzzleImage) | |
{ | |
$this->authorize('view', $puzzleImage); | |
$savePath = "puzzle_images/{$imageSet['id']}/{$puzzleImage['id']}/"; | |
return response(Storage::get($savePath . 'current_mixed.jpg'))->header('Content-Type', Storage::mimeType($savePath . 'current_mixed.jpg')); | |
} | |
public function mixed(ImageSet $imageSet, PuzzleImage $puzzleImage) | |
{ | |
$this->authorize('view', $puzzleImage); | |
$savePath = "puzzle_images/{$imageSet['id']}/{$puzzleImage['id']}/"; | |
return response(Storage::get($savePath . 'mixed.jpg'))->header('Content-Type', Storage::mimeType($savePath . 'mixed.jpg')); | |
} | |
public function original(ImageSet $imageSet, PuzzleImage $puzzleImage) | |
{ | |
$this->authorize('view', $puzzleImage); | |
$savePath = "puzzle_images/{$imageSet['id']}/{$puzzleImage['id']}/"; | |
return response(Storage::get($savePath . 'original.jpg'))->header('Content-Type', Storage::mimeType($savePath . 'original.jpg')); | |
} | |
/** | |
* Show the form for editing the specified resource. | |
* | |
* @param \App\ImageSet $imageSet | |
* @param \App\PuzzleImage $puzzleImage | |
* @return \Illuminate\Http\Response | |
*/ | |
public function edit(ImageSet $imageSet, PuzzleImage $puzzleImage) | |
{ | |
return view('puzzle_images/create_edit', ['imageSet' => $imageSet, 'puzzleImage' => $puzzleImage]); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \App\PuzzleImage $puzzleImage | |
* @return \Illuminate\Http\Response | |
*/ | |
public function update(Request $request, ImageSet $imageSet, PuzzleImage $puzzleImage) | |
{ | |
$puzzleImage->fill( | |
$request->only('name', 'active', 'order', 'width', 'height', 'slice_h', 'slice_v', 'pieces') | |
); | |
if (gettype($puzzleImage['pieces']) == 'string') | |
$puzzleImage['pieces'] = json_decode($puzzleImage['pieces']); | |
$this->storeUpdate($request, $imageSet, $puzzleImage); | |
return redirect(action('PuzzleImageController@edit', ['image_set' => $imageSet, 'puzzle_image' => $puzzleImage])); | |
} | |
/** | |
* Remove the specified resource from storage. | |
* | |
* @param \App\PuzzleImage $puzzleImage | |
* @return \Illuminate\Http\Response | |
*/ | |
public function destroy(ImageSet $imageSet, PuzzleImage $puzzleImage) | |
{ | |
$puzzleImage->delete(); | |
return redirect(action('ImageSetController@show', $imageSet)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment