drh2so4/thumbnail
Composer 安装命令:
composer require drh2so4/thumbnail
包简介
Package for making thumbnails instantly.
关键字:
README 文档
README
Laravel Thumbnail Generator
Package for uploading the image and saving that image along with it's thumbnail.
What does it do ?
- Uploads Image
- Make its thumbnail i.e low quality, resized version of its parent image
Why use thumbnails ?
The small file size of thumbnails makes it possible for website designers to offer visitors a lot of content immediately without increasing the loading time of the page. Also why use full glory of that image if you just have to crunched it up to tiny space... Use thumbnail.
Installation
Run Composer Require Command
$ composer require drh2so4/thumbnail
Use thumbnail trait to your model
<?php namespace App; use drh2so4\Thumbnail\Traits\thumbnail; use Illuminate\Database\Eloquent\Model; class Image extends Model { use Thumbnail; protected $fillable = ['image']; }
This model consists of following methods :-
- makeThumbnail
- thumbnail
- uploadImage
- hasThumbnail
- thumbnailCount
- imageDetail
- hardDelete
Usages
Package utilizes it's trait method, let us guide you to use that
makeThumbnail
This method is responsible for actually uploading the image and making its thumbnail.
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'); }
Same can go with update method.
thumbnail
Well, we created our thumbnail but how to use it, let me guide through that When we uploaded image with name suppose "batman". Image is upload with name batman-current_time_instant i.e (batman-1521549.jpg).
What about thumbnail... well thumbnail uses it's parent image name followed by -size i.e batman-1521549-medium-jpg, batman-1521549-small.jpg
How to make thumbnail ?
There are the options you can have for making thumbnails :-
- Default Option
- Universal Custom Thumbnails
- Specfic Custom Thumbnails
Default Option
you can just call the following and the packages will handle the rest
$image->makeThumbnail('image'); //Here the first parameter is image attribute name saved in db
Note : if the attribute dedicated for storing image is named 'image' you don't have to pass image attribute name jusr use $image->makeThumbnail();
Universal Custom Thumbnails
here you should mention the thumbnails that you want to be applied on every case. when you publish thumbnail.php config file you will find 'thumbnails' property where you can mention your custom 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 ] ] */
Note: This will override default option
Specfic Custom Thumbnails
Suppose you have applied Universal Custom Thumbnails but need to have changes for specific image field then you can pass array of custom requirements :
$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);
How about multiple image uploads
If you are performing multiple image upload at once pass image key to thumbnail array. here $img is one of the image in chunk of images passed
// 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); }
How to use thumbnail ?
Just call as following
// 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
if you are using custom thumbnail configured from config file just call as follows
// 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
Notice that parameter of function thumbnail is string same as value given for "thumbnail-name" in config file.
Thumbnail's image property is predefined but if you wish to change that publish it's config file thumbnail.php
php artisan vendor:publish --tag=thumbnail-config
Image Property
You can obtaing the detail image property by using method 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
What Image Property/Detail Gives
Default Thumbnail Image Properties
| Property | Return Type | Description | Example |
|---|---|---|---|
| image | string | Image path stored in DB | $image->imageDetail('image')->image |
| name | string | Image Stored Name (without extension) | $image->imageDetail('image')->name |
| fullname | string | Image Stored Name (with extension) | $image->imageDetail('image')->fullname |
| extension | string | Image Extension Name | $image->imageDetail('image')->extension |
| path | string | Image Storage Path | $image->imageDetail('image')->path |
| directory | string | Image Stored Directory | $image->imageDetail('image')->directory |
| location | string | Image Full Location Path | $image->imageDetail('image')->location |
| property | array | Image Property array | $image->imageDetail('image')->property |
Image Property array ($image->imageDetail('image')->property)
| Property | Return Type | Description | Example |
|---|---|---|---|
| real_name | string | Image Real Name (without timestamp and size label) | $image->imageDetail('image')->property->name |
| size | integer | Image Storage Size | $image->imageDetail('image')->property->fullname |
| directory | string | Image Stored Directory | $image->imageDetail('image')->property->directory |
| location | string | Image Full Location Path | $image->imageDetail('image')->property->location |
| has_thumbnail | boolean | Image's Thumbnail Check | $image->imageDetail('image')->property->has_thumbnail |
| thumbnail_count | integer | Image Thumbnail Count | $image->imageDetail('image')->property->thumbnail_count |
| thumbnails | array | Return all thumbnail Detail | $image->imageDetail('image')->property->thumbnails |
Image Thumbnail Property
| Property | Return Type | Description |
|---|---|---|
| image | string | Thumbnail Name |
| real_name | string | Thumbnail Real Name (without timestamp and size label) |
| size | integer | Thumbnail Storage Size |
| created_date | Carbon | Thumbnail Created Date |
| path | string | Thumbnail Storage Path |
| directory | string | Thumbnail Stored Directory |
| location | string | Thumbnail Full Location Path |
Check if image has thumbnail
$image->hasThumbnail('image'); // Check for any availabe thumbnail $image->hasThumbnail('image','small'); // Second paremater is thumbnail size check
Obtain Image Thumbnail Count
$image->thumbnailCount('image');
Hard Delete Image with Thumbnails
$image->hardDelete('image'); // First parameter is db attribute name for image
Hard Delete Image with Thumbnails adn its parent
$image->hardDeleteWithParent('image'); // First parameter is db attribute name for image
Upload Only Umage
$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
Our config file looks like follows :-
<?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 ], ] */ ];
Feel free to change the values
Default Thumbnail Image Properties
| Thumbnail | Width | Height | Quality |
|---|---|---|---|
| Uploaded Image | 1000 | 800 | 80 |
| Medium Thumbnail | 800 | 600 | 60 |
| Small Thumbnail | 400 | 300 | 30 |
Todos
- Error Handling
- Image Caching
- Maintainabilty
Package Used
License
MIT
DOCTYPE NEPAL || DR.H2SO4
drh2so4/thumbnail 适用场景与选型建议
drh2so4/thumbnail 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36.3k 次下载、GitHub Stars 达 56, 最近一次更新时间为 2020 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel thumbnail package」 「laravel thumbnail」 「laravel thumbnail generator」 「thumbnail generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 drh2so4/thumbnail 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drh2so4/thumbnail 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 drh2so4/thumbnail 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP library for fetching thumbnails from a URL
A Gokaru storage & thumbnail server PHP client
Simple ASCII output of array data
Yii2 Glide Extension
Alfabank REST API integration
统计信息
- 总下载量: 36.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 57
- 点击次数: 19
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-08