本文整理汇总了PHP中Intervention\Image\ImageManager类的典型用法代码示例。如果您正苦于以下问题:PHP ImageManager类的具体用法?PHP ImageManager怎么用?PHP ImageManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ImageManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: thumbAction
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);
}
示例2: postCrop
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);
}
示例3: handle
/**
* @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);
}
}
示例4: handle
/**
* @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;
}
示例5: saveImageLocally
/**
* @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;
}
}
示例6: data
/**
* {@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);
}
示例7: handle
/**
* @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;
}
示例8: image
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();
}
示例9: createImageSet
/**
* 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);
}
示例10: handle
/**
* @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;
}
}
示例11: generate
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;
}
示例12: setUp
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();
}
示例13: processAvatar
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;
}
示例14: run
/**
* 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();
}
示例15: resize
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());
}