/** * @covers MediaAlchemyst\Transmuter\Video2Image::execute */ public function testExecuteWithOptions() { $this->specs->setDimensions(320, 240); $this->specs->setResizeMode(Image::RESIZE_MODE_INBOUND); $this->object->execute($this->specs, $this->source, $this->dest); $mediaDest = $this->getMediaVorus()->guess($this->dest); $this->assertEquals('image/png', $mediaDest->getFile()->getMimeType()); $this->assertEquals(320, $mediaDest->getWidth()); $this->assertEquals(240, $mediaDest->getHeight()); }
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; }
/** * @covers MediaAlchemyst\Specification\Image::setFlatten * @covers MediaAlchemyst\Specification\Image::isFlatten */ public function testSetFlatten() { $this->object->setFlatten(true); $this->assertEquals(true, $this->object->isFlatten()); $this->object->setFlatten(false); $this->assertEquals(false, $this->object->isFlatten()); }
/** * @covers MediaAlchemyst\Specification\Image::setStrip * @covers MediaAlchemyst\Specification\Image::getStrip */ public function testSetStrip() { $this->object->setStrip(true); $this->assertEquals(true, $this->object->getStrip()); $this->object->setStrip(false); $this->assertEquals(false, $this->object->getStrip()); }
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; }
/** * Return the box for a spec * * @param Specification\Image $spec * @param integer $width * @param integer $height * * @return \Image\Box */ protected function boxFromSize(Image $spec, $width, $height) { if (!$spec->getWidth() && !$spec->getHeight()) { throw new InvalidArgumentException('The specification you provide must have width nad height'); } if ($spec->getResizeMode() == Image::RESIZE_MODE_INBOUND_FIXEDRATIO) { $ratioOut = $spec->getWidth() / $spec->getHeight(); $ratioIn = $width / $height; if ($ratioOut > $ratioIn) { $outHeight = round($spec->getHeight()); $outWidth = round($ratioIn * $outHeight); } else { $outWidth = round($spec->getWidth()); $outHeight = round($outWidth / $ratioIn); } return new Box($outWidth, $outHeight); } return new Box($spec->getWidth(), $spec->getHeight()); }
/** * {@inheritdoc} */ public function apply(base $appbox, Application $app) { $conn = $appbox->get_connection(); try { //get all old lazaret file & transform them to LazaretFile object $sql = 'SELECT * FROM lazaret'; $stmt = $conn->prepare($sql); $stmt->execute(); $rs = $stmt->fetchAll(); $stmt->closeCursor(); } catch (DBALException $e) { // table not found if ($e->getCode() == '42S02') { } return; } //order matters for foreign keys constraints //truncate all altered tables $this->truncateTable($app['EM'], 'Alchemy\\Phrasea\\Model\\Entities\\LazaretAttribute'); $this->truncateTable($app['EM'], 'Alchemy\\Phrasea\\Model\\Entities\\LazaretCheck'); $this->truncateTable($app['EM'], 'Alchemy\\Phrasea\\Model\\Entities\\LazaretFile'); $this->truncateTable($app['EM'], 'Alchemy\\Phrasea\\Model\\Entities\\LazaretSession'); $i = 0; foreach ($rs as $row) { $filePath = $app['tmp.lazaret.path'] . '/' . $row['filepath']; if (null === ($user = $this->loadUser($app['EM'], $row['usr_id']))) { continue; } if (file_exists($filePath)) { $spec = new ImageSpec(); $spec->setResizeMode(ImageSpec::RESIZE_MODE_INBOUND_FIXEDRATIO); $spec->setDimensions(375, 275); $thumbPath = $app['tmp.lazaret.path'] . '/' . sprintf("thumb_%s", $row['filepath']); try { $app['media-alchemyst']->turnInto($filePath, $thumbPath, $spec); } catch (MediaAlchemystException $e) { } $media = $app['mediavorus']->guess($filePath); $collection = \collection::get_from_base_id($app, $row['base_id']); $borderFile = new \Alchemy\Phrasea\Border\File($app, $media, $collection); $lazaretSession = new LazaretSession(); $lazaretSession->setUser($user); $lazaretFile = new LazaretFile(); $lazaretFile->setBaseId($row['base_id']); if (null === $row['uuid']) { $uuid = $borderFile->getUUID(true); $lazaretFile->setUuid($uuid); } else { $lazaretFile->setUuid($row['uuid']); } if (null === $row['sha256']) { $sha256 = $media->getHash('sha256'); $lazaretFile->setSha256($sha256); } else { $lazaretFile->setSha256($row['sha256']); } $lazaretFile->setOriginalName($row['filename']); $lazaretFile->setFilename($row['filepath']); $lazaretFile->setThumbFilename(pathinfo($thumbPath), PATHINFO_BASENAME); $lazaretFile->setCreated(new \DateTime($row['created_on'])); $lazaretFile->setSession($lazaretSession); $app['EM']->persist($lazaretFile); if (0 === ++$i % 100) { $app['EM']->flush(); $app['EM']->clear(); } } } $app['EM']->flush(); $app['EM']->clear(); $stmt->closeCursor(); return true; }
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); }
public function write_databox_pic(Alchemyst $alchemyst, Filesystem $filesystem, databox $databox, SymfoFile $pathfile = null, $pic_type) { $filename = null; if (!is_null($pathfile)) { if (!in_array(mb_strtolower($pathfile->getMimeType()), ['image/jpeg', 'image/jpg', 'image/pjpeg', 'image/png', 'image/gif'])) { throw new \InvalidArgumentException('Invalid file format'); } } if (!in_array($pic_type, [databox::PIC_PDF])) { throw new \InvalidArgumentException('unknown pic_type'); } if ($pathfile) { $filename = $pathfile->getPathname(); $imageSpec = new ImageSpecification(); $imageSpec->setResizeMode(ImageSpecification::RESIZE_MODE_INBOUND_FIXEDRATIO); $imageSpec->setDimensions(120, 35); $tmp = tempnam(sys_get_temp_dir(), 'tmpdatabox') . '.jpg'; try { $alchemyst->turninto($pathfile->getPathname(), $tmp, $imageSpec); $filename = $tmp; } catch (\MediaAlchemyst\Exception $e) { } } $file = $this->app['root.path'] . '/config/minilogos/' . $pic_type . '_' . $databox->get_sbas_id() . '.jpg'; $custom_path = $this->app['root.path'] . '/www/custom/minilogos/' . $pic_type . '_' . $databox->get_sbas_id() . '.jpg'; foreach ([$file, $custom_path] as $target) { if (is_file($target)) { $filesystem->remove($target); } if (is_null($filename)) { continue; } $filesystem->mkdir(dirname($target)); $filesystem->copy($filename, $target); $filesystem->chmod($target, 0760); } $databox->delete_data_from_cache('printLogo'); return $this; }
public static function updateIcon(Application $app, $sbas_id, $bit, $switch, UploadedFile $file) { $switch = in_array($switch, ['on', 'off']) ? $switch : false; if (!$switch) { throw new Exception_InvalidArgument(); } $url = self::getUrl($app, $sbas_id); $path = self::getPath($app, $sbas_id); if ($file->getSize() >= 65535) { throw new Exception_Upload_FileTooBig(); } if (!$file->isValid()) { throw new Exception_Upload_Error(); } self::deleteIcon($app, $sbas_id, $bit, $switch); $name = "-stat_" . $bit . "_" . ($switch == 'on' ? '1' : '0') . ".gif"; try { $file = $file->move($app['root.path'] . "/config/status/", $path . $name); } catch (FileException $e) { throw new Exception_Upload_CannotWriteFile(); } $custom_path = $app['root.path'] . '/www/custom/status/'; $app['filesystem']->mkdir($custom_path, 0750); //resize status icon 16x16px $imageSpec = new ImageSpecification(); $imageSpec->setResizeMode(ImageSpecification::RESIZE_MODE_OUTBOUND); $imageSpec->setDimensions(16, 16); $filePath = sprintf("%s%s", $path, $name); $destPath = sprintf("%s%s", $custom_path, basename($path . $name)); try { $app['media-alchemyst']->turninto($filePath, $destPath, $imageSpec); } catch (\MediaAlchemyst\Exception $e) { } self::$_status[$sbas_id]->status[$bit]['img_' . $switch] = $url . $name; self::$_status[$sbas_id]->status[$bit]['path_' . $switch] = $filePath; return true; }
/** * Send a package file to lazaret * * @param File $file The package file * @param Visa $visa The visa related to the package file * @param LazaretSession $session The current LazaretSession * @param Boolean $forced True if the file has been forced to quarantine * * @return LazaretFile */ protected function createLazaret(File $file, Visa $visa, LazaretSession $session, $forced) { $date = new \DateTime(); $file->addAttribute(new MetadataAttr(new Metadata(new TfQuarantine(), new MonoValue($date->format('Y/m/d H:i:s'))))); $lazaretPathname = $this->bookLazaretPathfile($file->getOriginalName()); $lazaretPathnameThumb = $this->bookLazaretPathfile($file->getOriginalName(), 'thumb'); $this->app['filesystem']->copy($file->getFile()->getRealPath(), $lazaretPathname, true); $spec = new ImageSpec(); $spec->setResizeMode(ImageSpec::RESIZE_MODE_INBOUND_FIXEDRATIO); $spec->setDimensions(375, 275); try { $this->app['media-alchemyst']->turnInto($file->getFile()->getPathname(), $lazaretPathnameThumb, $spec); } catch (MediaAlchemystException $e) { } $lazaretFile = new LazaretFile(); $lazaretFile->setBaseId($file->getCollection()->get_base_id()); $lazaretFile->setSha256($file->getSha256()); $lazaretFile->setUuid($file->getUUID()); $lazaretFile->setOriginalName($file->getOriginalName()); $lazaretFile->setForced($forced); $lazaretFile->setFilename(pathinfo($lazaretPathname, PATHINFO_BASENAME)); $lazaretFile->setThumbFileName(pathinfo($lazaretPathnameThumb, PATHINFO_BASENAME)); $lazaretFile->setSession($session); $this->app['orm.em']->persist($lazaretFile); foreach ($file->getAttributes() as $fileAttribute) { $attribute = new LazaretAttribute(); $attribute->setName($fileAttribute->getName()); $attribute->setValue($fileAttribute->asString()); $attribute->setLazaretFile($lazaretFile); $lazaretFile->addAttribute($attribute); $this->app['orm.em']->persist($attribute); } foreach ($visa->getResponses() as $response) { if (!$response->isOk()) { $check = new LazaretCheck(); $check->setCheckClassname(get_class($response->getChecker())); $check->setLazaretFile($lazaretFile); $lazaretFile->addCheck($check); $this->app['orm.em']->persist($check); } } $this->app['orm.em']->flush(); return $lazaretFile; }
/** * @param ImageSpecification $imageSpec * @param int $width * @param int $height */ protected function setSpecificationSize(ImageSpecification $imageSpec, $width, $height) { $imageSpec->setResizeMode(ImageSpecification::RESIZE_MODE_INBOUND_FIXEDRATIO); $imageSpec->setDimensions($width, $height); }