protected function processQuery(\GearmanJob $job, Query $query) { $start = microtime(true); $job->sendStatus(0, 100); $tempfile = tempnam(sys_get_temp_dir(), 'transmute_image'); $tempdest = tempnam(sys_get_temp_dir(), 'transmute_image'); if (false === ($filecontent = @file_get_contents($query->getFile()))) { $this->logger->addInfo(sprintf('Unable to download file `%s`', $query->getFile())); return new Result($job->handle(), $query->getUuid(), $job->workload(), null, $this->workerName, $start, microtime(true), array(), array(sprintf('Unable to download file `%s`', $query->getFile()))); } $this->logger->addInfo(sprintf('file %s retrieved', $query->getFile())); $job->sendStatus(30, 100); file_put_contents($tempfile, $filecontent); unset($filecontent); $job->sendStatus(50, 100); $specification = new Image(); $width = $height = null; foreach ($query->getParameters() as $name => $value) { switch ($name) { case 'width': $width = $value; break; case 'height': $height = $value; break; case 'quality': $specification->setQuality($value); break; } } if (null !== $width && null !== $height) { $specification->setDimensions($width, $height); } try { $this->alchemyst->open($tempfile)->turnInto($tempdest, $specification)->close(); } catch (Exception $e) { $this->logger->addInfo(sprintf('A media-alchemyst exception occured %s', $e->getMessage())); return new Result($job->handle(), $query->getUuid(), $job->workload(), null, $this->workerName, $start, microtime(true), array(), array(sprintf('A media-alchemyst exception occured %s', $e->getMessage()))); } catch (\Exception $e) { $this->logger->addInfo(sprintf('An unexpected exception occured %s', $e->getMessage())); return new Result($job->handle(), $query->getUuid(), $job->workload(), null, $this->workerName, $start, microtime(true), array(), array(sprintf('An unexpected exception occured %s', $e->getMessage()))); } $result = new Result($job->handle(), $query->getUuid(), $job->workload(), file_get_contents($tempdest), $this->workerName, $start, microtime(true)); unlink($tempfile); unlink($tempdest); $this->logger->addInfo('Conversion successfull'); $job->sendStatus(100, 100); return $result; }
private function specificationsFromJob(ImageJob $job) { $specifications = new Image(); $parameters = $this->populateParameters($job->getParameters(), array('width' => null, 'height' => null, 'quality' => null, 'resize-mode' => null, 'resolution' => null, 'resolution-units' => null, 'resolution-x' => null, 'resolution-y' => null, 'rotation' => null, 'strip' => null)); if ($parameters['width'] && $parameters['height']) { $specifications->setDimensions($parameters['width'], $parameters['height']); } if ($parameters['quality']) { $specifications->setQuality($parameters['quality']); } if ($parameters['resize-mode']) { switch ($parameters['resize-mode']) { case ImageJob::RESIZE_OUTBOUND: $mode = Image::RESIZE_MODE_OUTBOUND; break; case ImageJob::RESIZE_INBOUND: $mode = Image::RESIZE_MODE_INBOUND; break; default: case ImageJob::RESIZE_INBOUND_FIXEDRATIO: $mode = Image::RESIZE_MODE_INBOUND_FIXEDRATIO; break; } $specifications->setResizeMode($mode); } if ($parameters['resolution'] || $parameters['resolution-x'] && $parameters['resolution-y']) { $units = Image::RESOLUTION_PIXELPERINCH; if ($parameters['resolution-units'] === ImageJob::RESOLUTION_PER_CENTIMETERS) { $units = Image::RESOLUTION_PIXELPERCENTIMETER; } if ($parameters['resolution']) { $res_x = $res_y = $parameters['resolution']; } else { $res_x = $parameters['resolution-x']; $res_y = $parameters['resolution-y']; } $specifications->setResolution($res_x, $res_y, $units); } if ($parameters['rotation']) { $specifications->setRotationAngle($parameters['rotation']); } if ($parameters['strip']) { $specifications->setStrip($parameters['strip']); } return $specifications; }
public function uploadIconAction(Request $request, $id) { $datas = ['success' => false, 'message' => '']; $feed = $this->getFeedRepository()->find($id); if (null === $feed) { $this->app->abort(404, "Feed not found"); } if (!$feed->isOwner($this->getAuthenticatedUser())) { $this->app->abort(403, "Access Forbidden"); } try { if (!$request->files->get('files')) { throw new BadRequestHttpException('Missing file parameter'); } if (count($request->files->get('files')) > 1) { throw new BadRequestHttpException('Upload is limited to 1 file per request'); } $file = current($request->files->get('files')); if (!$file->isValid()) { throw new BadRequestHttpException('Uploaded file is invalid'); } $media = $this->app->getMediaFromUri($file->getPathname()); if ($media->getType() !== MediaInterface::TYPE_IMAGE) { throw new BadRequestHttpException('Bad filetype'); } $spec = new Image(); $spec->setResizeMode(Image::RESIZE_MODE_OUTBOUND); $spec->setDimensions(32, 32); $spec->setStrip(true); $spec->setQuality(72); $tmpname = tempnam(sys_get_temp_dir(), 'feed_icon') . '.png'; try { /** @var Alchemyst $alchemyst */ $alchemyst = $this->app['media-alchemyst']; $alchemyst->turnInto($media->getFile()->getPathname(), $tmpname, $spec); } catch (\MediaAlchemyst\Exception\ExceptionInterface $e) { throw new \Exception_InternalServerError('Error while resizing'); } unset($media); $feed->setIconUrl(true); $manager = $this->getObjectManager(); $manager->persist($feed); $manager->flush(); /** @var Filesystem $filesystem */ $filesystem = $this->app['filesystem']; $rootPath = $this->app['root.path']; $filesystem->copy($tmpname, $rootPath . '/config/feed_' . $feed->getId() . '.jpg'); $filesystem->copy($tmpname, sprintf('%s/www/custom/feed_%d.jpg', $rootPath, $feed->getId())); $filesystem->remove($tmpname); $datas['success'] = true; } catch (\Exception $e) { $datas['message'] = $this->app->trans('Unable to add file to Phraseanet'); } return $this->app->json($datas); }
/** * @covers MediaAlchemyst\Specification\Image::setQuality * @expectedException MediaAlchemyst\Exception\InvalidArgumentException */ public function testSetWrongQuality() { $this->object->setQuality(160); }