nineskeletor/src/Controller/CropController.php

367 lines
13 KiB
PHP
Executable File

<?php
namespace App\Controller;
use App\Entity\Childheader;
use App\Service\MinioService;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
class CropController extends AbstractController
{
private $appKernel;
private $minio;
public function __construct(KernelInterface $appKernel, MinioService $minio)
{
$this->appKernel = $appKernel;
$this->minio = $minio;
}
// Etape 01 - Téléchargement de l'image
public function crop01($type, $reportinput): Response
{
return $this->render('Crop/crop01.html.twig', [
'useheader' => false,
'usesidebar' => false,
'type' => $type,
'reportinput' => $reportinput,
]);
}
// Etape 02 - Couper votre l'image
public function crop02($type, $reportinput, Request $request)
{
// Récupération de l'image à cropper
$file = $request->query->get('file');
$large_image_location = $this->minio->download($type.'/'.$file, $type.'/'.$file, true);
// Récupérer les tailles de l'image
$width = $this->getWidth($large_image_location);
$height = $this->getHeight($large_image_location);
$max_height = null;
$max_width = null;
$ratio = null;
// Définir le pourcentage de réduction de l'image
switch ($type) {
case 'illustration':
$max_height = 0;
$ratio = '1:1';
break;
case 'avatar':
$max_height = 900;
$max_width = 900;
$ratio = '1:1';
break;
case 'header':
$max_height = 1600;
$max_width = 1600;
$ratio = '16:2';
break;
case 'hero':
$max_height = 1600;
$max_width = 1600;
$ratio = '16:9';
break;
case 'image':
$max_height = 1600;
$max_width = 1600;
$ratio = '1:1';
break;
}
if ($max_height > 0) {
$scale = $max_height / $height;
if (($width * $scale) > $max_width) {
$scale = $max_width / $width;
}
$this->resizeImage($large_image_location, $width, $height, $scale);
$this->minio->upload($large_image_location, $type.'/'.$file, false);
} else {
$scale = 1;
}
// Construction du formulaire
$submited = false;
$form = $this->createFormBuilder()
->add('submit', SubmitType::class, ['label' => 'Valider', 'attr' => ['class' => 'btn btn-success']])
->add('x', HiddenType::class)
->add('y', HiddenType::class)
->add('w', HiddenType::class)
->add('h', HiddenType::class)
->add('xs', HiddenType::class)
->add('ys', HiddenType::class)
->add('ws', HiddenType::class)
->add('hs', HiddenType::class)
->getForm();
// Récupération des data du formulaire
$form->handleRequest($request);
// Sur validation on généère la miniature croppée
if ($form->get('submit')->isClicked() && $form->isValid()) {
// Récupération des valeurs du formulaire
$data = $form->getData();
$tmpdir = $this->appKernel->getProjectDir().'/var/tmp';
$thumb_image_location = "$tmpdir/$type/thumb_".$file;
$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location, $data['ws'], $data['hs'], $data['xs'], $data['ys'], $scale);
// Dépot des fichiers sur minio
$this->minio->upload($thumb_image_location, $type.'/thumb_'.$file, false);
$submited = true;
}
return $this->render('Crop/crop02.html.twig', [
'useheader' => false,
'usesidebar' => false,
'form' => $form->createView(),
'type' => $type,
'file' => $file,
'ratio' => $ratio,
'reportinput' => $reportinput,
'submited' => $submited,
]);
}
// Etape 01 - Téléchargement de l'image associé à un entity
public function cropentity01($type, $idparent)
{
return $this->render('Crop/crop01.html.twig', [
'useheader' => false,
'usesidebar' => false,
'type' => $type,
'idparent' => $idparent,
]);
}
// Etape 02 - Couper votre l'image
public function cropentity02($type, $idparent, Request $request, ManagerRegistry $em)
{
// Récupération de l'image à cropper
$file = $request->query->get('file');
$large_image_location = $this->minio->download($type.'/'.$file, $type.'/'.$file, true);
// Récupérer les tailles de l'image
$width = $this->getWidth($large_image_location);
$height = $this->getHeight($large_image_location);
// Définir le pourcentage de réduction de l'image
switch ($type) {
case 'childheader':
$max_height = 1600;
$max_width = 1600;
$parent = $em->getRepository("App\Entity\Child")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
switch ($parent->getChildtype()->getId()) {
case 1: $ratio = '16:4';
break;
case 16: $ratio = '4:6';
break;
case 17: $ratio = '4:6';
break;
default: $ratio = 'none';
break;
}
break;
}
if ($max_height > 0) {
$scale = $max_height / $height;
if (($width * $scale) > $max_width) {
$scale = $max_width / $width;
}
$this->resizeImage($large_image_location, $width, $height, $scale);
$this->minio->upload($large_image_location, $type.'/'.$file, false);
} else {
$scale = 1;
}
// Construction du formulaire
$submited = false;
$form = $this->createFormBuilder()
->add('submit', SubmitType::class, ['label' => 'Valider', 'attr' => ['class' => 'btn btn-success']])
->add('x', HiddenType::class)
->add('y', HiddenType::class)
->add('w', HiddenType::class)
->add('h', HiddenType::class)
->add('xs', HiddenType::class)
->add('ys', HiddenType::class)
->add('ws', HiddenType::class)
->add('hs', HiddenType::class)
->getForm();
// Récupération des data du formulaire
$form->handleRequest($request);
// Sur validation on généère la miniature croppée
if ($form->get('submit')->isClicked() && $form->isValid()) {
// Récupération des valeurs du formulaire
$data = $form->getData();
// Récupération des valeurs du formulaire
$data = $form->getData();
$tmpdir = $this->appKernel->getProjectDir().'/var/tmp';
$thumb_image_location = "$tmpdir/$type/".dirname($file).'/thumb_'.basename($file);
$cropped = $this->resizeThumbnailImage($thumb_image_location, $large_image_location, $data['ws'], $data['hs'], $data['xs'], $data['ys'], $scale);
// Dépot des fichiers sur minio
$this->minio->upload($thumb_image_location, $type.'/'.dirname($file).'/thumb_'.basename($file), false);
$submited = true;
switch ($type) {
case 'childheader':
$parent = $em->getRepository("App\Entity\Child")->find($idparent);
if (!$parent) {
throw $this->createNotFoundException('Not exist');
}
$last = $em->getRepository("App\Entity\Childheader")->findOneBy(['child' => $parent], ['roworder' => 'DESC']);
$lastorder = ($last ? $last->getRoworder() + 1 : 1);
$data = new Childheader();
$data->setChild($parent);
$data->setFilename($type.'/'.dirname($file).'/thumb_'.basename($file));
$data->setRoworder($lastorder);
$em->getManager()->persist($data);
$em->getManager()->flush();
break;
}
$submited = true;
}
return $this->render('Crop/crop02.html.twig', [
'useheader' => false,
'usesidebar' => false,
'form' => $form->createView(),
'type' => $type,
'file' => $file,
'ratio' => $ratio,
'idparent' => $idparent,
'submited' => $submited,
]);
}
// Calcul de la hauteur
protected function getHeight($image)
{
$size = getimagesize($image);
$height = $size[1];
return $height;
}
// Cacul de la largeur
protected function getWidth($image)
{
$size = getimagesize($image);
$width = $size[0];
return $width;
}
protected function resizeImage($image, $width, $height, $scale)
{
list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
$source = null;
switch ($imageType) {
case 'image/gif':
$source = imagecreatefromgif($image);
break;
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
$source = imagecreatefromjpeg($image);
break;
case 'image/png':
case 'image/x-png':
$source = imagecreatefrompng($image);
break;
}
imagecopyresampled($newImage, $source, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
switch ($imageType) {
case 'image/gif':
imagegif($newImage, $image);
break;
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
imagejpeg($newImage, $image, 90);
break;
case 'image/png':
case 'image/x-png':
imagepng($newImage, $image);
break;
}
chmod($image, 0640);
return $image;
}
protected function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale)
{
$fs = new Filesystem();
$fs->remove($thumb_image_name);
list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
$source = null;
switch ($imageType) {
case 'image/gif':
$source = imagecreatefromgif($image);
break;
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
$source = imagecreatefromjpeg($image);
break;
case 'image/png':
case 'image/x-png':
$source = imagecreatefrompng($image);
break;
}
$ok = imagecopyresampled($newImage, $source, 0, 0, $start_width, $start_height, $newImageWidth, $newImageHeight, $width, $height);
switch ($imageType) {
case 'image/gif':
imagegif($newImage, $thumb_image_name);
break;
case 'image/pjpeg':
case 'image/jpeg':
case 'image/jpg':
imagejpeg($newImage, $thumb_image_name, 100);
break;
case 'image/png':
case 'image/x-png':
imagepng($newImage, $thumb_image_name);
break;
}
chmod($thumb_image_name, 0640);
return $thumb_image_name;
}
}