Я хочу получить данные изображения для обрезки и затем сохранить
<?php
namespace App\Controller;
use App\Entity\Services;
use App\Form\ServicesType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class CreateServiceController extends AbstractController
{
/**
* @Route("/create/service", name="create_service")
*/
public function index(Request $request)
{
$service = new Services();
$form = $this->createForm(ServicesType::class, $service);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$service = $form->getData();
$service->setCreated(new \DateTime('now'));
/** @var UploadedFile $imagereFile */
$imageFile = $form->get('image')->getData();
if ($imageFile) {
$originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);
$safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
$newFilename = $safeFilename.'-'.uniqid().'.'.$imageFile->guessExtension();
try {
$imageFile->move(
$this->getParameter('image_directory'),
$newFilename
);
//problem here
$croped = \imagecrop( $this->getParameter('image_directory').$newFilename, ['x' => 0, 'y' => 0, 'width' => 100, 'height' => 100]);
imagejpeg($croped, $this->getParameter('image_directory').$newFilename, 75);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$service->setImage($newFilename);
}
// ... perform some action, such as saving the task to the database
// for example, if Task is a Doctrine entity, save it!
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($service);
$entityManager->flush();
return $this->redirectToRoute('services');
}
return $this->render('update_service/index.html.twig', [
'form' => $form->createView(),
]);
}
}
У меня есть этот код, но он не работает
Warning: imagecrop() expects parameter 1 to be resource, string given
// проблема здесь
$ croped = \ imagecrop ($ this-> getParameter ('image_directory'). $ NewFilename, ['x' => 0, 'y' => 0, 'width' => 100, 'height' = > 100]);
imagejpeg ($ croped, $ this-> getParameter ('image_directory'). $ NewFilename, 75);
} catch (FileException $ e) {
// ... обрабатывать исключение, если что-то происходит во время загрузки файла
}
$ service-> setImage ($ newFilename);
}