public function processAvatar($path) { $manager = $this->imgManager->make($path); $manager->orientate(); $manager->fit($this->width); $name = $this->getFileName(); $manager->save($this->getUploadPath($name)); return $name; }
/** * Perform image manipulations. * @param string $source Source image binary data. * @param array $params The manipulation params. * @return string Manipulated image binary data. */ public function run($source, array $params) { $image = $this->imageManager->make($source); foreach ($this->manipulators as $manipulator) { $manipulator->setParams($params); $image = $manipulator->run($image); } return $image->getEncoded(); }
public function thumbAction(Request $request, Application $app) { $source = $request->get('src', false); $width = $request->get('width', 250); // Do requested thumbnail in correct format already exists ? if ($app['flysystems']['thumbs']->has($width . "/" . $source)) { return $app->redirect($request->getBasePath() . '/thumbs/' . $width . '/' . $source, 301); } // Do requested file exists ? if (!$source || !$app['flysystems']['local']->has($source)) { return new Response("Source file not found.", 404); } try { $contents = $app['flysystems']['local']->read($source); $imageManager = new ImageManager(); $image = $imageManager->make($contents); $image->resize($width, null, function ($constraint) { $constraint->aspectRatio(); }); $info = $app['flysystems']['local']->getWithMetadata($source, ['mimetype']); $image->encode($info['mimetype']); $app['flysystems']['thumbs']->put($width . "/" . $source, $image); return $app->redirect($request->getBasePath() . '/thumbs/' . $width . '/' . $source, 301); } catch (\Exception $e) { return new Response("Erreur !", 500); } // Should not happen, everything failed. Display not found image :( return $app->redirect($request->getBasePath() . '/assets/img/' . $width . '_not-found.png', 302); }
public function postCrop() { $form_data = Input::all(); $image_url = $form_data['imgUrl']; // resized sizes $imgW = $form_data['imgW']; $imgH = $form_data['imgH']; // offsets $imgY1 = $form_data['imgY1']; $imgX1 = $form_data['imgX1']; // crop box $cropW = $form_data['width']; $cropH = $form_data['height']; // rotation angle $angle = $form_data['rotation']; $filename_array = explode('/', $image_url); $filename = $filename_array[sizeof($filename_array) - 1]; $manager = new ImageManager(); $image = $manager->make($image_url); $image->resize($imgW, $imgH)->rotate(-$angle)->crop($cropW, $cropH, $imgX1, $imgY1)->save(env('UPLOAD_PATH') . 'cropped-' . $filename); if (!$image) { return Response::json(['status' => 'error', 'message' => 'Server error while uploading'], 200); } return Response::json(['status' => 'success', 'url' => env('URL') . 'uploads/cropped-' . $filename], 200); }
/** * @param MediaRepositoryInterface $media * @param ImageManager $images * @param Filesystem $files * @return bool * @throws Exception */ public function handle(MediaRepositoryInterface $media, ImageManager $images, Filesystem $files) { list($width, $height) = $this->dimensions($this->size); $path = $this->getPath($files); $constraint = $this->constraint($width, $height); $image = $images->cache(function ($image) use($width, $height, $constraint) { if ($this->cachedPath) { $image = $image->make($this->cachedPath); } else { $image = $image->make(public_path($this->image->path)); } $image->resize($width, $height, $constraint); }, 60, true)->save($path); if ($image) { //always fetch the actual width and height from the image, //one of them could have been null to auto scale the image. $width = $image->getWidth(); $height = $image->getHeight(); //use html public path to store in database $path = $this->getPath($files, true); try { $media->createThumbnailImage($this->getPayload($width, $height, $path), $this->image); } catch (Exception $e) { $files->delete(public_path($path)); unset($image); return false; } unset($image); } }
/** * @param UploadAvatar $command * @return \Flarum\Core\Users\User * @throws \Flarum\Core\Exceptions\PermissionDeniedException */ public function handle(UploadAvatar $command) { $actor = $command->actor; $user = $this->users->findOrFail($command->userId); // Make sure the current user is allowed to edit the user profile. // This will let admins and the user themselves pass through, and // throw an exception otherwise. if ($actor->id !== $user->id) { $user->assertCan($actor, 'edit'); } $tmpFile = tempnam(sys_get_temp_dir(), 'avatar'); $command->file->moveTo($tmpFile); $manager = new ImageManager(); $manager->make($tmpFile)->fit(100, 100)->save(); event(new AvatarWillBeSaved($user, $actor, $tmpFile)); $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]); if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) { $mount->delete($file); } $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $user->changeAvatarPath($uploadName); $mount->move("source://" . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $user->save(); $this->dispatchEventsFor($user); return $user; }
/** * @param \Onyx\Destiny\Objects\Hash $hash * @param string $index * @return bool */ public static function saveImageLocally($hash, $index = 'extra') { // BUG: Can't use variable object indexes implicitly // $hash->{$index} should work but doesn't // map the index explicitly with the attributes dumped into $bug $bug = $hash->getAttributes(); $url = "https://bungie.net" . $bug[$index]; $name = $index != 'extra' ? '_bg' : null; $name = $hash->hash . $name; // Make sure we aren't trying to save something that isn't an image // We only need this check because we cheat and store all hash related objects // in one table. This means we have crazy cheats to get things done. if (strlen($bug[$index]) < 5) { return false; } $location = public_path('uploads/thumbs/'); $filename = $name . "." . pathinfo($bug[$index], PATHINFO_EXTENSION); if (File::isFile($location . $filename)) { return true; } if ($hash instanceof Hash) { $manager = new ImageManager(); try { $img = $manager->make($url); $img->save($location . $filename); } catch (NotReadableException $e) { Log::error('Could not download: ' . $url); } return true; } }
public function resize($image) { $width = config('medias.max_size.width'); $height = config('medias.max_size.height'); // this orientate method needs to be called because sometimes // images uploaded came rotated, but need to be ajusted $img = $this->image->make(Storage::disk(config('medias.disk'))->get($image))->orientate(); if ($img->width() <= $width && $img->height() <= $height) { return; } if ($img->width() > $width && $img->height() > $height) { $img->resize($width, $height, function ($constraint) { $constraint->aspectRatio(); }); Storage::disk(config('medias.disk'))->put($image, $img->stream()); return; } if ($img->width() > $width) { $height = null; } elseif ($img->height() > $height) { $width = null; } $img->resize($width, $height, function ($constraint) { $constraint->aspectRatio(); }); Storage::disk(config('medias.disk'))->put($image, $img->stream()); }
/** * {@inheritdoc} */ public function execute(CommandParams $params, CommandExecutionContext $executionContext) { $fileName = $params->getFirstArgument(); if (!$executionContext->hasFileInWorkingDirectory($fileName)) { throw new ExecutionFailedException("File '{$fileName}' does not exist."); } $img = $this->imageManager->make($executionContext->getPathOfFileInWorkingDirectory($fileName)); if (!$params->hasSecondArgument()) { return $this->returnImage($img, $params); } list($width, $height) = $this->parseDimension($params->getSecondArgument()); switch ($params->getThirdArgument()) { case 'stretch': $img->resize($width, $height); break; case 'fit': $img->fit($width, $height); break; case 'crop': $x = $this->filterIntOrNullArgument($params->getArgument(3)); $y = $this->filterIntOrNullArgument($params->getArgument(4)); $img->crop($width, $height, $x, $y); break; default: $img->resize($width, $height, function (Constraint $constraint) { $constraint->aspectRatio(); }); break; } return $this->returnImage($img, $params); }
/** * {@inheritdoc} */ public function data(ServerRequestInterface $request, Document $document) { $this->assertAdmin($request->getAttribute('actor')); $file = array_get($request->getUploadedFiles(), 'favicon'); $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'favicon'); $file->moveTo($tmpFile); $extension = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION); if ($extension !== 'ico') { $manager = new ImageManager(); $encodedImage = $manager->make($tmpFile)->resize(64, 64, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->encode('png'); file_put_contents($tmpFile, $encodedImage); $extension = 'png'; } $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => new Filesystem(new Local($this->app->publicPath() . '/assets'))]); if (($path = $this->settings->get('favicon_path')) && $mount->has($file = "target://{$path}")) { $mount->delete($file); } $uploadName = 'favicon-' . Str::lower(Str::quickRandom(8)) . '.' . $extension; $mount->move('source://' . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $this->settings->set('favicon_path', $uploadName); return parent::data($request, $document); }
/** * @param UploadAvatar $command * @return \Flarum\Core\User * @throws \Flarum\Core\Exception\PermissionDeniedException */ public function handle(UploadAvatar $command) { $actor = $command->actor; $user = $this->users->findOrFail($command->userId); if ($actor->id !== $user->id) { $this->assertCan($actor, 'edit', $user); } $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'avatar'); $command->file->moveTo($tmpFile); $file = new UploadedFile($tmpFile, $command->file->getClientFilename(), $command->file->getClientMediaType(), $command->file->getSize(), $command->file->getError(), true); $this->validator->assertValid(['avatar' => $file]); $manager = new ImageManager(); $manager->make($tmpFile)->fit(100, 100)->save(); $this->events->fire(new AvatarWillBeSaved($user, $actor, $tmpFile)); $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]); if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) { $mount->delete($file); } $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $user->changeAvatarPath($uploadName); $mount->move("source://" . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $user->save(); $this->dispatchEventsFor($user, $actor); return $user; }
public function image($filename) { $mediaArchivePath = str_replace('\\', '\\\\', storage_path('app/media-archive/' . Auth::user()->id . '/')); // return Storage::disk('local')->get('media-archive/'. Auth::user()->id . '/' . $filename ); $manager = new ImageManager(); return $manager->make($mediaArchivePath . $filename)->response(); }
private function addTextLayer($image, $text, $position) { $coordinates = Image::getTextCoordinates($position); $textLayer = $this->manager->canvas($image->width(), $image->height(), [0, 0, 0, 0]); for ($x = -2; $x <= 2; $x++) { for ($y = -2; $y <= 2; $y++) { $textLayer->text($text, $coordinates['x'] + $x, $coordinates['y'] + $y, function ($font) { $font->file(Image::getFontFile()); $font->size(85); $font->color('#000'); // Glow color $font->align('center'); $font->valign('top'); }); } } //$textLayer->blur(10); $textLayer->text($text, $coordinates['x'], $coordinates['y'], function ($font) { $font->file(Image::getFontFile()); $font->size(85); $font->color('#FFF'); // Text color $font->align('center'); $font->valign('top'); }); return $textLayer; }
/** * Create an array of resized images * * @param string $image * @param int $id */ protected function createImageSet($path, $file) { $manager = new ImageManager(['driver' => 'imagick']); $image = $manager->make($path . $file); $image->resize(768, null, function ($constraint) { $constraint->aspectRatio(); })->sharpen(6)->save($path . '768_' . $file); $image->resize(600, null, function ($constraint) { $constraint->aspectRatio(); })->sharpen(6)->save($path . '600_' . $file); $image->resize(523, null, function ($constraint) { $constraint->aspectRatio(); })->sharpen(6)->save($path . '523_' . $file); $image->resize(480, null, function ($constraint) { $constraint->aspectRatio(); })->sharpen(6)->save($path . '480_' . $file); $image->resize(423, null, function ($constraint) { $constraint->aspectRatio(); })->sharpen(6)->save($path . '423_' . $file); $image->resize(313, null, function ($constraint) { $constraint->aspectRatio(); })->sharpen(6)->save($path . '313_' . $file); $image->destroy(); unlink($path . $file); }
/** * @param UploadAvatar $command * @return \Flarum\Core\User * @throws \Flarum\Core\Exception\PermissionDeniedException */ public function handle(UploadAvatar $command) { $actor = $command->actor; $user = $this->users->findOrFail($command->userId); if ($actor->id !== $user->id) { $this->assertCan($actor, 'edit', $user); } $tmpFile = tempnam($this->app->storagePath() . '/tmp', 'avatar'); $command->file->moveTo($tmpFile); try { $file = new UploadedFile($tmpFile, $command->file->getClientFilename(), $command->file->getClientMediaType(), $command->file->getSize(), $command->file->getError(), true); $this->validator->assertValid(['avatar' => $file]); $manager = new ImageManager(); // Explicitly tell Intervention to encode the image as JSON (instead of having to guess from the extension) $encodedImage = $manager->make($tmpFile)->fit(100, 100)->encode('jpg', 100); file_put_contents($tmpFile, $encodedImage); $this->events->fire(new AvatarWillBeSaved($user, $actor, $tmpFile)); $mount = new MountManager(['source' => new Filesystem(new Local(pathinfo($tmpFile, PATHINFO_DIRNAME))), 'target' => $this->uploadDir]); if ($user->avatar_path && $mount->has($file = "target://{$user->avatar_path}")) { $mount->delete($file); } $uploadName = Str::lower(Str::quickRandom()) . '.jpg'; $user->changeAvatarPath($uploadName); $mount->move('source://' . pathinfo($tmpFile, PATHINFO_BASENAME), "target://{$uploadName}"); $user->save(); $this->dispatchEventsFor($user, $actor); return $user; } catch (Exception $e) { @unlink($tmpFile); throw $e; } }
public function generate() { $image_manager = new ImageManager(); $this->image = $image_manager->make($this->source_path); if ($this->width() && $this->height()) { $this->image->fit($this->width(), $this->height(), function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); } else { if ($this->width() || $this->height()) { $this->image->resize($this->width(), $this->height(), function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); }); } } if ($this->cropWidth() || $this->cropHeight()) { $cw = $this->cropWidth() ?: $this->width() ?: $this->image->width(); $ch = $this->cropHeight() ?: $this->height() ?: $this->image->height(); if (!is_null($this->cropX()) || !is_null($this->cropY())) { $cx = is_null($this->cropX()) ? $cw / 2 - $this->image->width() / 2 : $this->cropX(); $cy = is_null($this->cropY()) ? $ch / 2 - $this->image->height() / 2 : $this->cropY(); $this->image->crop($cw, $ch, round($cx), round($cy)); } else { $this->image->resizeCanvas($cw, $ch, 'center', false, 'ffffff'); } } if ($this->rotation) { $this->image->rotate($this->rotation); } $this->image->save($this->dest_path); return $this; }
/** * {@inheritdoc} */ public function apply($source, $options) { $options = $this->parseOptions($options); $image = $this->imageManager->make($source); $image->crop($options['width'], $options['height'], $options['x'], $options['y']); return $image->encode($image->mime())->getEncoded(); }
public function setUp() { $manager = new ImageManager(); $this->jpg = $manager->canvas(100, 100)->encode('jpg'); $this->png = $manager->canvas(100, 100)->encode('png'); $this->gif = $manager->canvas(100, 100)->encode('gif'); $this->manipulator = new Encode(); }
private function createEmptyImage(int $width, int $height) : Image { $sId = sprintf('%s-%s', $width, $height); if (!isset($this->cachedSizes[$sId])) { $this->cachedSizes[$sId] = $this->imageManager->canvas($width, $height, 'ffffff'); } return clone $this->cachedSizes[$sId]; }
/** * Perform image manipulations. * @param Request $request The request object. * @param string $source Source image binary data. * @return string Manipulated image binary data. */ public function run(Request $request, $source) { $image = $this->imageManager->make($source); foreach ($this->manipulators as $manipulator) { $image = $manipulator->run($request, $image); } return $image->getEncoded(); }
/** * Create Icon From Original */ public function icon($photo, $filename) { $manager = new ImageManager(); $image = $manager->make($photo)->encode('jpg')->resize(93, null, function ($constraint) { $constraint->aspectRatio(); })->save(Config::get('images.icon_size') . $filename); return $image; }
/** * Create Icon From Original */ public function icon($photo, $filename) { $manager = new ImageManager(); $image = $manager->make($photo)->encode('jpg')->resize(700, null, function ($constraint) { $constraint->aspectRatio(); })->save('images/icon/' . $filename); return $image; }
/** * Create Icon From Original */ public function icon($photo, $filename) { $manager = new ImageManager(); $image = $manager->make($photo)->resize(200, null, function ($constraint) { $constraint->aspectRatio(); })->save(public_path('images/icon_size/' . $filename)); return $image; }
public function apply($source, $options) { $options = $this->parseOptions($options); $image = $this->imageManager->make($source); $image->fit($options['width'], $options['height'], function ($constraint) { $constraint->upsize(); }, $options['position']); return $image->encode($image->mime())->getEncoded(); }
public function test_upload_object() { $this->assertInstanceOf('\\Dugun\\UploadBundle\\Service\\DugunUploadService', $this->service); $interventionImageManager = new ImageManager(); $image = $interventionImageManager->make(__DIR__ . '/../Resources/assets/test/file1.jpg'); $this->service->upload($image, 'test.jpg', $delete = false, $overwrite = true); $image = new UploadedFile(__DIR__ . '/../Resources/assets/test/file1.jpg', 'file1.jpg', 'image/jpeg'); $result = $this->service->upload($image, 'test.jpg', $delete = false, $overwrite = true); }
/** * creates a thumbnail * * @param $imagePath * @param null $width * @param null $height * @param $target * @return bool */ public static function createThumbnail($imagePath, $width = null, $height = null, $target) { $manager = new ImageManager(); $manager->make($imagePath)->resize($width, $height, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); })->save($target); return true; }
public function testConfigure() { $overwrite = array('driver' => 'none', 'bar' => 'none'); $config = array('driver' => 'foo', 'bar' => 'baz'); $manager = new ImageManager($overwrite); $manager->configure($config); $this->assertEquals('foo', $manager->config['driver']); $this->assertEquals('baz', $manager->config['bar']); }
public function load($location) { try { $imageManager = new ImageManager(); $image_data = $imageManager->make($location); return new Image(["mime" => $image_data->mime(), "data" => $image_data->encode()]); } catch (\Intervention\Image\Exception\NotReadableException $e) { throw new ImageNotFoundException(); } }
/** * Apply resize * * @param string $path * @return Psr-7 stream */ public function apply($path) { $library = $this->libraryRepostiory->findByPath($path)->toArray(); $storage = $library['description']['is_moved'] ? $library['description']['storage'] : 'local'; $filesystem = $this->filesystemFactory->disk($storage); $stream = $filesystem->getDriver()->readStream($path); $lastModified = $filesystem->lastModified($path); return $this->imageManager->cache(function ($image) use($stream, $lastModified) { $this->process($image->setProperty('lastModified', $lastModified)->make($stream)); }, $this->getLifetime()); }
public function setAvatar($user_id, $avatar) { $source = $avatar['tmp_name']; $filename = $avatar['name']; $manager = new ImageManager(); $upload_dir = BASEPATH . '../assets/upload/avatar/'; $image = $manager->make($source); $image->save($upload_dir . $filename, 90); $data['avatar'] = $filename; $this->db->where('user_id', $user_id); $this->db->update('profile', $data); }