/** * Process command to image source and save to target * * @param string $source * @param string $target * @param string $commands * * @return void */ public function process($source, $target, $commands) { $targetFolder = pathinfo($target, PATHINFO_DIRNAME); if (!file_exists($targetFolder)) { mkdir($targetFolder, 0777, true); } $this->image = $this->getImagineService()->open($source); foreach ($this->analyseCommands($commands) as $command) { if ($this->runCommand($command)) { continue; } $this->runCustomCommand($command); } $this->image->save($target); }
/** * @param ImageInterface $image * @param $namespace * @param $image_hash * @param $image_thumb * @return File */ public function createFromImagine(ImageInterface $image, $namespace, $image_hash, $image_thumb) { umask(00); $dest = $this->createDestinationPath($namespace, $image_hash, $image_thumb); $image->save($dest); return new File($dest); }
/** * @param $tempFileName * @param $originalSize * @param $extension * @param $minQuality * @param $maxQuality * @param int $step * * @return string $path the resulting file path */ private function _autoGuessImageQuality($tempFileName, $originalSize, $extension, $minQuality, $maxQuality, $step = 0) { // Give ourselves some extra time. @set_time_limit(30); if ($step == 0) { $tempFileName = IOHelper::getFolderName($tempFileName) . IOHelper::getFileName($tempFileName, false) . '-temp.' . $extension; } // Find our target quality by splitting the min and max qualities $midQuality = (int) ceil($minQuality + ($maxQuality - $minQuality) / 2); // Set the min and max acceptable ranges. .10 means anything between 90% and 110% of the original file size is acceptable. $acceptableRange = 0.1; clearstatcache(); // Generate a new temp image and get it's file size. $this->_image->save($tempFileName, $this->_getSaveOptions($midQuality, $extension)); $newFileSize = IOHelper::getFileSize($tempFileName); // If we're on step 10 OR we're within our acceptable range threshold OR midQuality = maxQuality (1 == 1), // let's use the current image. if ($step == 10 || abs(1 - $originalSize / $newFileSize) < $acceptableRange || $midQuality == $maxQuality) { clearstatcache(); // Generate one last time. $this->_image->save($tempFileName, $this->_getSaveOptions($midQuality)); return $tempFileName; } $step++; if ($newFileSize > $originalSize) { return $this->_autoGuessImageQuality($tempFileName, $originalSize, $extension, $minQuality, $midQuality, $step); } else { return $this->_autoGuessImageQuality($tempFileName, $originalSize, $extension, $midQuality, $maxQuality, $step); } }
/** * Save image to file * @param $filePath * @param array $options * @return $this */ public function save($filePath, $options = []) { if ($this->image) { $this->image->save($filePath, $options); } return $this; }
/** * {@inheritDoc} */ public function createCache($relativeName, $filter, ImageInterface $image, $formatOrImage = null, array $saveOptions = []) { $cachePath = $this->getCachePath($relativeName, $filter, $formatOrImage); if (!is_dir(dirname($cachePath))) { mkdir(dirname($cachePath), 0755, true); } $image->save($cachePath, $saveOptions); }
public function cache($path, $format, ImageInterface $data) { $path = $this->getFullPath($path); $this->_prepareDirectoryForFile($path); if ($format == "webp") { $data->save($path, ["quality" => 100, "format" => "png"]); $cwebp = new Cwebp(); try { $cwebp->filter($path); } catch (Exception $e) { unlink($path); throw new RuntimeException($e->getMessage()); } } else { $data->save($path, ["quality" => 100, "format" => $format]); } chmod($path, 0777); }
/** * Process command to create 404 image and save to target * * @param string $target * @param string $commands * * @return void */ public function process404($target, $commands) { if (file_exists($target)) { return; } $targetFolder = pathinfo($target, PATHINFO_DIRNAME); if (!file_exists($targetFolder)) { mkdir($targetFolder, 0777, true); } $text = 'Not found'; $backgroundColor = null; $color = null; $width = null; $height = null; foreach ($this->analyseCommands($commands) as $command) { if ('thumb' === $command['command'] || 'resize' === $command['command']) { $width = $command['params'][0]; $height = $command['params'][1]; } elseif ('404' === $command['command']) { if (isset($command['params'][0]) && UrlSafeBase64::valid($command['params'][0])) { $text = UrlSafeBase64::decode($command['params'][0]); } if (isset($command['params'][1])) { $backgroundColor = $command['params'][1]; } if (isset($command['params'][2])) { $color = $command['params'][2]; } if (isset($command['params'][3])) { $width = $command['params'][3]; } if (isset($command['params'][4])) { $height = $command['params'][4]; } } } $this->image404($text, $backgroundColor, $color, $width, $height); $this->image->save($target); }
public function filterDump(ImageInterface $image) { $pb = $this->createProcessBuilder(array('' . $this->facedetectBin)); $uniq = uniqid(); $input = sys_get_temp_dir() . '/evathumber_facedetect_' . $uniq . '_in.png'; $pb->add($input); $image->save($input); $output = sys_get_temp_dir() . '/evathumber_facedetect_' . $uniq . '_out.json'; $pb->add($output); $pb->add($this->cascade); $proc = $pb->getProcess(); $code = $proc->run(); if (0 < $code) { //unlink($input); //return false; } $res = array(); if (file_exists($output)) { $res = json_decode(file_get_contents($output)); @unlink($output); } @unlink($input); return $res; }
/** * Saves a cached image preset. * @param ImageInterface $image the image. * @param string $path the save path (relative to the preset cache path). * @param string $filename the filename. * @param array $options the options for saving the image. * @return ImageInterface the image. */ public function saveCachedImage(ImageInterface $image, $path, $filename, $options = array()) { $path = $this->resolveCachePath() . '/' . $path; $this->getManager()->getFileManager()->createDirectory($path); return $image->save($path . '/' . $filename, $options); }
public function filterDump(ImageInterface $image) { $pb = $this->createProcessBuilder([$this->pngoutBin]); if (null !== $this->color) { $pb->add('-c' . $this->color); } if (null !== $this->filter) { $pb->add('-f' . $this->filter); } if (null !== $this->strategy) { $pb->add('-s' . $this->strategy); } if (null !== $this->blockSplitThreshold) { $pb->add('-b' . $this->blockSplitThreshold); } //EvaThumber changes: $uniq = uniqid(); $input = sys_get_temp_dir() . '/evathumber_pngout_' . $uniq . '_in.png'; $pb->add($input); $image->save($input); $output = sys_get_temp_dir() . '/evathumber_pngout_' . $uniq . '_out.png'; $pb->add($output); $proc = $pb->getProcess(); $code = $proc->run(); if (0 < $code) { unlink($input); return false; //return $image; } unlink($input); return $output; $imageClass = explode('\\', get_class($image)); $imageClass[2] = 'Imagine'; $imagineClass = implode('\\', $imageClass); $imagine = new $imagineClass(); $image = $imagine->load(file_get_contents($output)); unlink($input); unlink($output); return $image; }
/** * Save image * @return ImageInterface */ public function save() { if (file_exists($this->_pathObjectFile)) { $this->_url = $this->_urlObjectFile; return true; } elseif (file_exists($this->_pathObjectFileOrigin)) { $this->_image = Image::getImagine()->open($this->_pathObjectFileOrigin); } elseif (file_exists($this->_pathObjectPlaceholder)) { $this->_url = $this->_urlObjectPlaceholder; return true; } elseif (file_exists($this->_pathObjectPlaceholderOrigin)) { $this->_image = Image::getImagine()->open($this->_pathObjectPlaceholderOrigin); $this->_pathObjectFile = $this->_pathObjectPlaceholder; $this->_urlObjectFile = $this->_urlObjectPlaceholder; } elseif (file_exists($this->_pathRootPlaceholder)) { $this->_url = $this->_urlRootPlaceholder; return true; } elseif (file_exists($this->_pathRootPlaceholderOrigin)) { $this->_image = Image::getImagine()->open($this->_pathRootPlaceholderOrigin); $this->_pathObjectFile = $this->_pathRootPlaceholder; $this->_urlObjectFile = $this->_urlRootPlaceholder; } if ($this->_image) { if (!$this->width || !$this->height) { $ratio = $this->_image->getSize()->getWidth() / $this->_image->getSize()->getHeight(); if ($this->width) { $this->height = ceil($this->width / $ratio); } else { $this->width = ceil($this->height * $ratio); } $box = new Box($this->width, $this->height); $this->_image->resize($box); $this->_image->crop(new Point(0, 0), $box); } else { $box = new Box($this->width, $this->height); $size = $this->_image->getSize(); if ($size->getWidth() <= $box->getWidth() && $size->getHeight() <= $box->getHeight() || !$box->getWidth() && !$box->getHeight()) { } else { $this->_image = $this->_image->thumbnail($box, ManipulatorInterface::THUMBNAIL_OUTBOUND); // calculate points $size = $this->_image->getSize(); $startX = 0; $startY = 0; if ($size->getWidth() < $this->width) { $startX = ceil($this->width - $size->getWidth()) / 2; } if ($size->getHeight() < $this->height) { $startY = ceil($this->height - $size->getHeight()) / 2; } // create empty image to preserve aspect ratio of thumbnail $thumb = Image::getImagine()->create($box, new Color('FFF', 100)); $thumb->paste($this->_image, new Point($startX, $startY)); $this->_image = $thumb; } } $this->_image->save($this->_pathObjectFile); $this->_url = $this->_urlObjectFile; return true; } return false; }
/** * {@inheritdoc} */ public function apply(ImageInterface $image) { return $image->save($this->path, $this->options); }
/** * Save Image to a file * * @param ImageInterface $image * @param string $to * @param string|ImageInterface $source * @param array $options * * @return bool|ImageInterface */ protected function saveImage(ImageInterface $image, $to, $source = '', array $options = array()) { if (!$to && $source && !$source instanceof ImageInterface) { $to = $source; } if ($to) { $result = true; if ($this->getOption('auto_mkdir') && !$this->mkdir($to)) { $result = false; } else { try { $image->save($to, $options); } catch (\Excetpion $e) { $result = false; } } } else { $result = $image; } return $result; }
/** * Saves the image sprite and stylesheet. * * @param Configuration $config The Configuration instance * @param ImageInterface $image The ImageInterface instance * @param string $styles The CSS stylesheet * @return void * * @throws \RuntimeException If the image sprite could not be saved * @throws \RuntimeException If the stylesheet could not be saved */ protected function save(Configuration $config, ImageInterface $image, $styles) { $this->createDirectory(array($config->getImage(), $config->getStylesheet())); try { $image->save($config->getImage(), $config->getOptions()); } catch (\RuntimeException $e) { // @codeCoverageIgnoreStart throw new \RuntimeException(sprintf('Unable to write file "%s".', $config->getImage())); // @codeCoverageIgnoreEnd } if (false === @file_put_contents($config->getStylesheet(), $styles)) { // @codeCoverageIgnoreStart throw new \RuntimeException(sprintf('Unable to write file "%s".', $config->getStylesheet())); // @codeCoverageIgnoreEnd } }
/** * @param \Imagine\Image\ImageInterface $Image * @param string $mark */ private function createMarkedFile(ImageInterface $Image, $mark) { \Yii::beginProfile('create cache file:' . $this->File->id, __METHOD__); list($mark_file_path, $mark_file_web_path) = $this->getMarkedFilePath($mark); $mark_dir_path = dirname($mark_file_path); if (!file_exists($mark_dir_path) || !is_dir($mark_dir_path)) { FileHelper::createDirectory($mark_dir_path); } $Image->save($mark_file_path, ['quality' => 90]); @chmod($mark_file_path, 0664); \Yii::endProfile('create cache file:' . $this->File->id, __METHOD__); }