Skip to content

Commit

Permalink
新增显示附件图片功能
Browse files Browse the repository at this point in the history
  • Loading branch information
mradang committed Feb 23, 2020
1 parent 38fded4 commit f4cf886
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ composer require mradang/laravel-attachment
1. 添加 .env 环境变量,使用默认值时可省略
```
# 附件存储在 storage 下的目录名(默认:attachments)
ATTACHMENT_FOLDER=attachments
ATTACHMENT_DIRECTORY=attachments
# 缩略图存储在 storage 下的目录名(默认:thumbs)
ATTACHMENT_THUMB_FOLDER=thumbs
ATTACHMENT_THUMBNAIL=thumbs
```

## 添加的内容
Expand All @@ -36,6 +36,7 @@ use mradang\LaravelAttachment\Traits\AttachmentTrait;
> - void attachmentSort(array $data) 附件排序
> - string attachmentUrl($id) 附件 Url
> - response attachmentDownload($id) 下载指定附件
> - response attachmentShowImage($id, int $width, int $height) 显示附件图片
> - void attachmentDelete($id) 删除模型的指定附件
> - void attachmentClear() 清空模型的全部附件
Expand Down
5 changes: 5 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
*/
'directory' => env('ATTACHMENT_DIRECTORY', 'attachments'),

/*
* Thumbnail directory.
*/
'thumbnail' => env('ATTACHMENT_THUMBNAIL', 'thumbnails'),

];
42 changes: 42 additions & 0 deletions src/Jobs/MakeThumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace mradang\LaravelAttachment\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use mradang\LaravelAttachment\Services\AttachmentService;

class MakeThumbnail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $file_storage;
protected $width;
protected $height;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct(string $file_storage, int $width, int $height)
{
$this->file_storage = $file_storage;
$this->width = $width;
$this->height = $height;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
AttachmentService::makeThumb($this->file_storage, $this->width, $this->height);
}
}
66 changes: 66 additions & 0 deletions src/Services/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\File;
use mradang\LaravelAttachment\Jobs\MakeThumbnail;

class AttachmentService
{
Expand Down Expand Up @@ -98,6 +99,71 @@ public static function download($class, $key, $id)
return Storage::disk(config('attachment.disk'))->download($attachment->filename);
}

public static function showImage($class, $key, int $id, int $width, int $height)
{
$attachment = Attachment::where([
'id' => $id,
'attachmentable_id' => $key,
'attachmentable_type' => $class,
])->firstOrFail();

if (empty($attachment->imageInfo)) {
abort('500', 'Not a picture.');
}

if (!$width || !$height) {
return Storage::disk(config('attachment.disk'))->download($attachment->filename);
}

$thumb = self::generateThumbName($attachment->filename, $width, $height);
if (!Storage::disk(config('attachment.disk'))->exists($thumb)) {
dispatch(new MakeThumbnail($attachment->filename, $width, $height));
return Storage::disk(config('attachment.disk'))->download($attachment->filename);
}

return Storage::disk(config('attachment.disk'))->download($thumb);
}

// 生成图片缩略图
public static function makeThumb(string $storage_filename, int $width, int $height)
{
$storagePath = Storage::disk(config('attachment.disk'))->getDriver()->getAdapter()->getPathPrefix();
$pathname = $storagePath . $storage_filename;

if (!is_file($pathname)) {
return false;
}

$imagesize = @getimagesize($pathname);
if (!is_array($imagesize)) {
return false;
}

$thumb_full = $storagePath . self::generateThumbName($storage_filename, $width, $height);

if (!is_file($thumb_full)) {
$path = dirname($thumb_full);
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
$image = new \Gumlet\ImageResize($pathname);
$image->resizeToBestFit($width, $height);
$image->save($thumb_full, \IMAGETYPE_JPEG);
}
}

// 生成缩略图存储文件名
private static function generateThumbName(string $storage_filename, int $width, int $height)
{
return sprintf(
'%s/%s_%dx%d.jpg',
\rtrim(config('attachment.thumbnail'), '/'),
$storage_filename,
$width,
$height
);
}

public static function delete($class, $key, $id)
{
$attachment = Attachment::where([
Expand Down
5 changes: 5 additions & 0 deletions src/Traits/AttachmentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function attachmentDownload($id)
return AttachmentService::download(__CLASS__, $this->getKey(), $id);
}

public function attachmentShowImage($id, int $width = 0, int $height = 0)
{
return AttachmentService::showImage(__CLASS__, $this->getKey(), $id, $width, $height);
}

public function attachmentDelete($id)
{
return AttachmentService::delete(__CLASS__, $this->getKey(), $id);
Expand Down

0 comments on commit f4cf886

Please sign in to comment.