/** * @param $name * @param CropDimensions $dimensions * @return File */ public function cropTempImage($name, CropDimensions $dimensions) { $config = $dimensions->getConfig(); $path = $this->getTempFilePath($config, $name); $oldExtension = pathinfo($path, PATHINFO_EXTENSION); $img = ImageManagerStatic::make($path); $img->crop($dimensions->getWidth(), $dimensions->getHeight(), $dimensions->getX1(), $dimensions->getY1()); $unique = uniqid(); // Just if two people would edit the same image. Rather unlikely, but just in case $name = '_crop_' . $unique . '_' . $name; $tempPath = $this->getTempFilePath($config, $name); $img->save($tempPath); $img->destroy(); $file = new File($tempPath); $md5 = md5_file($tempPath); $newName = $md5 . '.' . $oldExtension; $file = $file->move($this->getTempDir($config), $newName); return $file; }
public function cropAndPreviewAction($config, $name, Request $request) { $manager = $this->get('wucdbm_gallery.manager.images'); $dimensions = new CropDimensions(); $imagePath = $manager->getTempFilePath($config, $name); $type = exif_imagetype($imagePath); $dimensions->setType($type); $form = $this->createForm(CropType::class, $dimensions); $form->handleRequest($request); if ($form->isValid()) { ini_set('memory_limit', '256M'); $file = $manager->cropTempImage($name, $dimensions); return $this->json(['url' => $this->generateUrl('wucdbm_gallery_image_serve_temp', ['config' => $config, 'name' => $file->getFilename()]), 'success' => true]); } $errors = []; foreach ($form->getErrors(true, true) as $error) { $errors[] = sprintf('<li>%s</li>', $error->getMessage()); } $errors = implode('', $errors); $response = sprintf("The form has the following errors: <ul>%s</ul>", $errors); return $this->json(['success' => false, 'error' => $response, 'witter' => ['title' => 'Error', 'text' => $response]]); }