/** * Handle request to convert it to a Response object. */ public function handle() { try { if (!$this->request->query->has('image')) { throw new FileNotFoundException("No valid image path found in URI", 1); } $nativePath = $this->configuration->getImagesPath() . '/' . $this->request->query->get('image'); $this->nativeImage = new File($nativePath); $this->parseQuality(); if ($this->configuration->hasCaching()) { $cache = new FileCache($this->request, $this->nativeImage, $this->configuration->getCachePath(), $this->logger, $this->quality, $this->configuration->getTtl(), $this->configuration->getGcProbability(), $this->configuration->getUseFileChecksum()); $cache->setDispatcher($this->dispatcher); /** @var Response response */ $this->response = $cache->getResponse(function (InterventionRequest $interventionRequest) { return $interventionRequest->processImage(); }, $this); } else { $this->processImage(); $this->response = new Response((string) $this->image->encode(null, $this->quality), Response::HTTP_OK, ['Content-Type' => $this->image->mime(), 'Content-Disposition' => 'filename="' . $this->nativeImage->getFilename() . '"', 'X-Generator-First-Render' => true]); $this->response->setLastModified(new \DateTime('now')); } } catch (FileNotFoundException $e) { $this->response = $this->getNotFoundResponse($e->getMessage()); } catch (\RuntimeException $e) { $this->response = $this->getBadRequestResponse($e->getMessage()); } }
/** * @return string */ public function toBase64() { if (!$this->image) { throw new \LogicException(sprintf('You must call %s::makeAvatar() first!', __CLASS__)); } return $this->image->encode('data-url', 100)->encoded; }
/** * Builds PSR7 stream based on image data. Method uses Guzzle PSR7 * implementation as easiest choice. * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $format = $this->argument(0)->value(); $quality = $this->argument(1)->between(0, 100)->value(); $this->setOutput(\GuzzleHttp\Psr7\stream_for($image->encode($format, $quality)->getEncoded())); return true; }
/** * {@inheritdoc} */ public function createVariation(ImageFormat $output_format, $quality, ImageDimensions $dimensions = null, ImageCropDimensions $crop_dimensions = null) { try { $img = new InterventionImage($this->data); } catch (\Intervention\Image\Exception\InvalidImageDataStringException $e) { throw new BadImageException('Bad image data', 0, $e); } catch (\Intervention\Image\Exception\ImageNotFoundException $e) { throw new BadImageException('Not an image', 0, $e); } if ($dimensions) { if ($dimensions->getGrab()) { $img->grab($dimensions->getWidth(), $dimensions->getHeight()); } else { $img->resize($dimensions->getWidth(), $dimensions->getHeight(), $dimensions->getMaintainRatio(), $dimensions->canUpscale()); } } return $img->encode($output_format->key(), $quality); }
/** * @param Image $img * @param CommandParams $params * * @return string * * @throws ExecutionFailedException */ private function returnImage(Image $img, CommandParams $params) { switch ($this->returnType) { case self::RETURN_TYPE_URL: case self::RETURN_TYPE_PATH: $tmpDir = $this->executionContext->ensureTempDirectory(); $tmpFile = $tmpDir . '/' . md5($params->getParams()) . '.png'; $img->encode('png')->save($tmpFile); if ($this->returnType === self::RETURN_TYPE_PATH) { return $tmpFile; } return '<img src="/_tmp/' . basename($tmpFile) . '">'; case self::RETURN_TYPE_DATA_URL: return '![Image](' . $img->encode('data-url')->getEncoded() . ')'; case self::RETURN_TYPE_DATA: return $img->encode('png')->getEncoded(); default: throw new ExecutionFailedException("Invalid return type '{$this->returnType}' detected."); } }
/** * Store preview in storage. * * @param \Intervention\Image\Image $image * @param string $path */ private function store($image, $path) { Storage::put($path, $image->encode('jpg')); }
/** * Encode image in different formats * * @param string $format * @param integer $quality * @return string * @static */ public static function encode($format = null, $quality = 90) { return \Intervention\Image\Image::encode($format, $quality); }
/** * @param AttachableModel $model * @param Image $image * @param $templateName */ protected function saveSingleTemplate(AttachableModel $model, Image $image, $templateName) { $this->storage->put($model->getPath($templateName), $image->encode($model->file_extension)); }
private function streamImage(Application $app, Image $image) { $image->encode(null, $this->default_quality); $mime = self::getMimeFromData($image->getEncoded()); return $app->stream(function () use($image) { // echo $image->response(); echo $image->getEncoded(); }, 200, array('Content-Type' => $mime)); }
/** * @param Image $image * @param string $path * * @return bool */ public function save(Image $image, $path) { return $this->flysystem->put($path, $image->encode()); }
/** * @param Image $image * @param string $path * @param string $oldPath * @return bool */ public function update(Image $image, $path, $oldPath) { return $this->delete($oldPath) ? $this->flysystem->put($path, $image->encode()) : FALSE; }
/** * Perform output image manipulation. * @param Request $request The request object. * @param Image $image The source image. * @return Image The manipulated image. */ public function run(Request $request, Image $image) { return $image->encode($this->getFormat($image, $request->get('fm')), $this->getQuality($request->get('q'))); }
/** * This callback is executed before the image is saved. You can override this * if you want to prepere the image for saving (eg set file format etc). * * @param \Intervention\Image\Image $image * @return null */ public function onSaving(Image $image) { $image->encode('jpg', 75); }
/** * Calculates checksum of given image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $this->setOutput(md5($image->encode('png'))); return true; }
public function missingMethod($args) { if (count($args) < 3) { return parent::missingMethod($args); } if ($args[0] == 'force') { array_shift($args); } $modelKeyword = $args[0]; $modelClass = \Devhook::getClassByKey($modelKeyword); $id = $args[1]; $file = $args[2]; $parts = explode('-', $file); if (count($parts) == 3) { list($field, $sizeKey, $file) = $parts; @(list($imageId, $ext) = explode('.', $file)); } else { $imageId = false; @(list($field, $file) = $parts); @(list($sizeKey, $ext) = explode('.', $file)); } if (!$field || !$sizeKey || $ext != 'jpg') { return parent::missingMethod($args); } $model = $modelClass::find($id); if (!$model || !$model->{$field}) { return parent::missingMethod($args); } // $fileExt = pathinfo($model->$field, PATHINFO_EXTENSION); $fields = (array) $model->getFields(); $modelField = isset($fields[$field]) ? $fields[$field] : false; if (!$modelField || empty($modelField->type)) { return parent::missingMethod($args); } $imageTypes = array('image'); $type = $modelField->type; $sizes = (array) $modelField->settings('sizes'); $imageAdminThumb = Config::get('devhook.imageAdminThumb'); if (!in_array($type, $imageTypes) || empty($sizes[$sizeKey])) { return parent::missingMethod($args); } $watermark = $modelField->settings('watermark'); $size = $sizes[$sizeKey]; $width = $size[0]; $hegiht = $size[1]; $crop = isset($size[2]) ? $size[2] : false; $quality = isset($size[3]) ? $size[3] : 95; if ($imageId) { $imageFile = \Image::where('imageable_type', $modelKeyword)->where('imageable_id', $id)->find($imageId); if (!$imageFile) { return parent::missingMethod($args); } $imageFile = $imageFile->path; } else { $imageFile = $model->{$field}; } ini_set('memory_limit', '200M'); $image = new IMG(public_path($imageFile)); if ($crop) { $image->grab($width, $hegiht); } else { $image->resize($width, $hegiht, true); } if ($watermark) { if (!$watermark['sizes'] || in_array($sizeKey, $watermark['sizes'])) { $image->insert($watermark['image'], $watermark['offset'][0], $watermark['offset'][1], $watermark['position']); } } $tmpFile = public_path($model->{$field}) . '.tmp_' . uniqid(); $newFile = public_path(Config::get('devhook.imageRoute') . '/' . implode('/', $args)); $newPath = dirname($newFile); if (!File::exists($newPath)) { File::makeDirectory($newPath, 0777, true); } $image->save($newFile, $quality); $img = $image->encode($ext, $quality); $response = Response::make($img, 200); $response->header('Content-Type', 'image/jpeg'); return $response; }
/** * Encode * * @param string $format * @param integer $quality * * @return binary */ public function encode($format = null, $quality = null) { return $this->image->encode($format, $quality); }