astroanu/laravel-image-cache

Imagine 上的 Laravel 图片缓存

1.1.5 2015-05-15 10:58 UTC

This package is auto-updated.

Last update: 2024-09-18 03:59:59 UTC


README

安装

将此添加到 composer 并运行 composer update

"astroanu/laravel-image-cache": "dev-master"

或运行

composer require astroanu/laravel-image-cache

注册 Laravel 提供者

将此添加到 config/app.php

'providers' => [
   	   	 ...
   	   	'Astroanu\ImageCache\ImageCacheProvider'
    ];

配置文件

运行 artisan vendor:publish 以复制配置文件。

php artisan vendor:publish

示例配置文件

return 	array(
    'paths' => array(
        'input' => '../storage/assets', //this is where we will upload images. Also supports Laravel 5 file system.. see below
        'output' => '../storage/imagecache' // this is where we will save cached images. You are free to clean this folder when needed manually. Also supports Laravel 5 file system.. see below
    ),
    'usestorage' => true, // wheather to use Laravel filesystem or not.. see below.
    'imagedriver' => 'imagick', // imagick or gd
    'imagepath' => 'images', // default route for images. http://www.doamin.com/route/xxxx.jpg
    'defaults' => array(
    	'thumbwidth' => 80, // default thumb size
    	'thumbheight' => 80, // default thumb size
    	'jpgquality' => 80, // default jpg quality
    )
);

使用 Laravel 文件系统

图片缓存可以使用 Laravel 5 文件系统,您可以根据 /config/filesystem.php 配置将图片直接保存到云端或本地文件系统。

要设置 Laravel 文件系统,在配置文件中将 'usestorage' => true。

'disks' => [

    'img_cache_input' => [
        'driver' => 'local',
        'root'   => storage_path().'/app/imagecache/input',
    ],
    'img_cache_output' => [
        'driver' => 'local',
        'root'   => storage_path().'/app/imagecache/output',
    ],

    'local' => [
        'driver' => 'local',
        'root'   => storage_path().'/app',
    ],

然后在 config/filesystem.php 中定义输入和输出磁盘。

return  array(
    'paths' => array(
        'input' => 'img_cache_input',
        'output' => 'img_cache_output',
    ),
    'usestorage' => true,
    'imagedriver' => 'imagick',
    'imagepath' => 'images',
    'defaults' => array(
        'thumbwidth' => 80,
        'thumbheight' => 80,
        'jpgquality' => 80
    ),
);

然后在 config/astroanu/imagecache.php 中定义相同的磁盘 ID。

图片路由

/{route}/{folder}/{image id}.{extention} // full image
/{route}/{folder}/{image id}-{squareSize}.{extention} // full image
/{route}/{folder}/{image id}-{width}-{height}.{extention} // resized and cropped

examples:
http://domain.com/route/folder/imageid.jpg
http://domain.com/route/folder/imageid-100-200.jpg

以下图片 URL/路由可用

上传图片

use Astroanu\ImageCache\Uploader;

class Users extends Model {

    // this is required. this is we save the uploaded files.
    protected $imagesdir = 'avatars';  
    
    public function uploadAndSetAvatar($file)
	{
		if (!is_null($file)) {		
		    // here we are saving the returned image id to the model's image attribute
			$this->image = Uploader::upload($file, $this->imagesdir); 
		}
	}
}

使用 Astroanu\ImageCache\Uploader 类上传图片;

$user = new User();
$user->uploadAndSetAvatar(Input::file('image'));
$user->save();

在控制器中简单操作

或者

$fileId = Uploader::upload(Input::file('avatar'), 'avatars'); // store the $fileId on the database

如果您从控制器上传,则这样做

使用 Traits 获取图片

class Users extends Model {
    // this is required. this is where we look for the image file. see uploading.
    protected $imagesdir = 'avatars';  
    use \Astroanu\ImageCache\Traits\GetImage;
}

您可以在模型内部使用 Trait \Astroanu\ImageCache\Traits\GetImage 来获取图片。只需这样

echo User::find(1)->getImage(); // returns the cached image url

然后在您的视图或控制器中

getImage() //  returns image url with the default thumb size: as defined in the config
getImage(50) // returns a cropped and resized square image url
getImage(50, 800) // returns a cropped and resized rectangular image url

getImage() 支持以下参数

在 html 中插入图片

<img src="/images/<?php echo $folder; ?>/<?php echo $imageid; ?>.jpg">
<img src="/images/<?php echo $folder; ?>/<?php echo $imageid; ?>-100.jpg">
<img src="/images/<?php echo $folder; ?>/<?php echo $imageid; ?>-30-40.jpg">

由于您知道图片 ID 和文件夹,因此可以轻松地将图片插入到 html 中

加载图片对象

$image = new Astroanu\ImageCache\Image('forlder', 'imageid');
$blob = $image->resize(200); // resized
$blob = $image->resize(50, 500); // resized
$blob = $image->resize(null); // null will return the original image blob