drh2so4 / thumbnail
制作缩略图的包。
Requires
- intervention/image: ^2.4@dev
- intervention/imagecache: ^2.4
Requires (Dev)
- orchestra/testbench: 5.x-dev
This package is auto-updated.
Last update: 2024-08-30 01:09:54 UTC
README
Laravel Thumbnail Generator
上传图片并保存其缩略图的包。
它做什么?
- 上传图片
- 创建其缩略图,即其父图像的低质量、缩放版本
为什么使用缩略图?
缩略图的小文件大小使得网站设计师能够在不增加页面加载时间的情况下向访客提供大量内容。另外,如果你只需要将其挤压到很小的空间,为何不使用缩略图呢?使用缩略图。
安装
运行Composer Require命令
$ composer require drh2so4/thumbnail
将缩略图特性应用到模型中
<?php namespace App; use drh2so4\Thumbnail\Traits\thumbnail; use Illuminate\Database\Eloquent\Model; class Image extends Model { use Thumbnail; protected $fillable = ['image']; }
此模型包含以下方法:
- makeThumbnail
- thumbnail
- uploadImage
- hasThumbnail
- thumbnailCount
- imageDetail
- hardDelete
用法
包使用其特性方法,让我们来指导您如何使用它
makeThumbnail
此方法负责实际上传图片并创建其缩略图。
public function store(Request $request) { $image = Image::create($this->validateData()); $image->makeThumbnail('image'); //This handles uploading image and storing it's thumbnail return redirect('/imageUpload'); }
同样也适用于更新方法。
thumbnail
好吧,我们已经创建了缩略图,但如何使用它呢?让我来引导您,当我们上传名为“batman”的图片时,图片会以“batman-current_time_instant”命名上传,即(batman-1521549.jpg)。
那么缩略图呢?嗯,缩略图使用其父图像名称后跟-size,即batman-1521549-medium-jpg,batman-1521549-small.jpg
如何创建缩略图?
以下是您可以使用的创建缩略图选项:
- 默认选项
- 通用自定义缩略图
- 特定自定义缩略图
默认选项
您只需调用以下内容,然后包将处理其余部分
$image->makeThumbnail('image'); //Here the first parameter is image attribute name saved in db
注意:如果用于存储图片的属性名为'image',则无需传递图片属性名称,只需使用$image->makeThumbnail();
通用自定义缩略图
在此处,您应指定要在每种情况下应用的缩略图。当发布thumbnail.php配置文件时,您将在其中找到'thumbnails'属性,您可以指定您自定义的缩略图
/* |-------------------------------------------------------------------------- | Custom Thumbnail Creation |-------------------------------------------------------------------------- | Uncomment to create... */ /* "thumbnails" => [ [ "thumbnail-name" => "medium", "thumbnail-width" => 800, "thumbnail-height" => 600, "thumbnail-quality" => 60 ], [ "thumbnail-name" => "small", "thumbnail-width" => 400, "thumbnail-height" => 300, "thumbnail-quality" => 30 ] ] */
注意:这将覆盖默认选项
特定自定义缩略图
假设您已应用通用自定义缩略图,但需要针对特定图像字段进行更改,则可以传递自定义要求的数组
$thumbnails = [ 'storage' => 'customs/embed', 'width' => '600', 'height' => '400', 'quality' => '70', 'thumbnails' => [ [ 'thumbnail-name' => 'customSmall', 'thumbnail-width' => '300', 'thumbnail-height' => '200', 'thumbnail-quality' => '50' ] ] ]; $image->makeThumbnail('image', $thumbnails);
关于多图上传
如果您正在一次性执行多个图像上传,请将图像键传递给缩略图数组。这里$img是传递的一组图像中的一个
// Controller Store Method public function store(Request $request) { $image = Image::create(['images'=>$request->images]); foreach($request->images as $img) { $this->multipleImageUpload($image,$img); } } // Multiple Image Upload private function multipleImageUpload($image, $img) { $multiple = [ 'storage' => 'custom_test/folder/another_folder/image', 'width' => '600', 'height' => '400', 'quality' => '70', 'image' => $img, 'thumbnails' => [ [ 'thumbnail-name' => 'small', 'thumbnail-width' => '300', 'thumbnail-height' => '200', 'thumbnail-quality' => '50' ] ] ]; $image->makeThumbnail('image', $multiple); }
如何使用缩略图?
只需按以下方式调用即可
// Here the first parameter 'image' is the name of sttribute that is saved in db for image @foreach ($images as $image) <img src="{{asset($image->thumbnail('image','small'))}}"> // For small thumbnail <img src="{{asset($image->thumbnail('image','medium'))}}"> // For medium thumbnail @endforeach
如果您正在使用从配置文件配置的自定义缩略图,请按以下方式调用
// Here the first parameter 'image' is the name of sttribute that is saved in db for image // Second parameter is the name of thumbnail that you gave in 'thumbnail-name' in the config file on custom thumbnail field called 'thumbnails' @foreach ($images as $image) <img src="{{asset($image->thumbnail('image','small'))}}"> // For small thumbnail <img src="{{asset($image->thumbnail('image','medium'))}}"> // For medium thumbnail @endforeach
请注意,函数thumbnail的参数是字符串,与配置文件中给定的“thumbnail-name”的值相同。
缩略图的图像属性是预定义的,但如果您希望更改它,请发布其配置文件thumbnail.php
php artisan vendor:publish --tag=thumbnail-config
图像属性
您可以通过使用方法imageDetail($image,$size)获取详细图像属性
// imageDetail method takes two parameter // Model image attribute name (Mandatory) // If you want thumbnail property pass its size (optional) $image->imageDetail('image'); // Parent Image Detail Property $image->imageDetail('image','small') // Small thumbnail related to parent image
图像属性/详细信息提供什么?
默认缩略图图像属性
图像属性数组 ($image->imageDetail('image')->property)
图像缩略图属性
检查图像是否有缩略图
$image->hasThumbnail('image'); // Check for any availabe thumbnail $image->hasThumbnail('image','small'); // Second paremater is thumbnail size check
获取图像缩略图数量
$image->thumbnailCount('image');
硬删除带缩略图的图像
$image->hardDelete('image'); // First parameter is db attribute name for image
硬删除带缩略图及其父图像的图像
$image->hardDeleteWithParent('image'); // First parameter is db attribute name for image
仅上传图像
$solo_image = [ 'storage' => 'solo', 'width' => '600', 'height' => '300', 'quality' => '70', ]; $image->uploadImage('image', $solo_image); // Second Parameter is not necessary if default settings is to be applied // OR $image->uploadImage('image'); // Image Upload with default setting
我们的配置文件如下所示:
<?php return [ /* |-------------------------------------------------------------------------- | Thumbnail Feature |-------------------------------------------------------------------------- | | This option defines whether to use Package's Thumbnail feature or not | Default option is true | */ 'thumbnail' => true, /* |-------------------------------------------------------------------------- | Thumbnail Qualities |-------------------------------------------------------------------------- | | These options are default post image and its thumbnail quality | | */ 'image_quality' => 80, 'medium_thumbnail_quality' => 60, 'small_thumbnail_quality' => 30, /* |-------------------------------------------------------------------------- | Default Image Fit Size |-------------------------------------------------------------------------- | | These options is default post imahe height and width fit size | | */ 'img_width' => 1000, 'img_height' => 800, 'medium_thumbnail_width' => 800, 'medium_thumbnail_height' => 600, 'small_thumbnail_width' => 400, 'small_thumbnail_height' => 300, /* |-------------------------------------------------------------------------- | Image and Thumbnail Storage Path |-------------------------------------------------------------------------- | | Define your default image storage location along with its thumbnail | | */ "storage_path" => "uploads", /* |-------------------------------------------------------------------------- | Custom Thumbnail Creation |-------------------------------------------------------------------------- | Uncomment to create... */ /* "thumbnails" => [ [ "thumbnail-name" => "medium", "thumbnail-width" => 800, "thumbnail-height" => 600, "thumbnail-quality" => 60 ], [ "thumbnail-name" => "small", "thumbnail-width" => 400, "thumbnail-height" => 300, "thumbnail-quality" => 30 ], ] */ ];
请随意更改这些值。
默认缩略图图像属性
待办事项
- 错误处理
- 图片缓存
- 可维护性
使用的包
许可证
MIT
DOCTYPE NEPAL || DR.H2SO4