Skip to content

Commit 737ac8e

Browse files
committed
added thumbnail creation for media manager component
1 parent 7734702 commit 737ac8e

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"require": {
1313
"php": ">=5.6.0",
14-
"illuminate/html": "^5.0@dev"
14+
"illuminate/html": "^5.0@dev",
15+
"intervention/image": "^2.3@dev"
1516
},
1617
"require-dev": {
1718
},

src/Darryldecode/Backend/Components/MediaManager/Commands/UploadCommand.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Darryldecode\Backend\Base\Commands\Command;
1212
use Darryldecode\Backend\Base\Commands\CommandResult;
13+
use Darryldecode\Backend\Components\MediaManager\Services\Image;
1314
use Illuminate\Contracts\Bus\SelfHandling;
1415
use Illuminate\Contracts\Events\Dispatcher;
1516
use Illuminate\Contracts\Filesystem\Filesystem;
@@ -40,9 +41,10 @@ public function __construct($files = null, $path = null, $disablePermissionCheck
4041

4142
/**
4243
* @param Repository $config
44+
* @param Image $image
4345
* @return CommandResult
4446
*/
45-
public function handle(Repository $config)
47+
public function handle(Repository $config,Image $image)
4648
{
4749
// check if user has permission
4850
if( ! $this->disablePermissionChecking )
@@ -58,10 +60,31 @@ public function handle(Repository $config)
5860
// upload files
5961
foreach($this->files as $file)
6062
{
63+
// save the file
6164
$file->move(
6265
$config->get('filesystems.disks.local.root').'/'.$this->normalizePath($path),
6366
$file->getClientOriginalName()
6467
);
68+
69+
$filePath = $config->get('filesystems.disks.local.root').'/'.$this->normalizePath($path).$file->getClientOriginalName();
70+
$file_name = pathinfo($filePath, PATHINFO_FILENAME);
71+
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
72+
73+
// produce thumbnail sizes
74+
$sizes = $config->get('backend.backend.thumb_sizes');
75+
76+
if( getimagesize($filePath) )
77+
{
78+
foreach($sizes as $key => $dimension)
79+
{
80+
$image::createThumbnail(
81+
$filePath,
82+
$dimension[0],
83+
$dimension[1],
84+
$config->get('filesystems.disks.local.root').'/'.$this->normalizePath($path).$this->produceThumbFileName($file_name,$key,$extension)
85+
);
86+
}
87+
}
6588
}
6689

6790
// all good
@@ -80,4 +103,18 @@ protected function normalizePath($path)
80103

81104
return ltrim($path, '/');
82105
}
106+
107+
/**
108+
* produce proper thumbname according to size
109+
* ex. from myFile.jpg -> myFile_small.jpg
110+
*
111+
* @param $file_name
112+
* @param $file_size_name
113+
* @param $file_extension
114+
* @return string
115+
*/
116+
protected function produceThumbFileName($file_name, $file_size_name, $file_extension)
117+
{
118+
return $file_name.'_'.$file_size_name.'.'.$file_extension;
119+
}
83120
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: darryl
5+
* Date: 7/28/2015
6+
* Time: 11:29 PM
7+
*/
8+
9+
namespace Darryldecode\Backend\Components\MediaManager\Services;
10+
11+
use Intervention\Image\ImageManager;
12+
13+
class Image {
14+
15+
/**
16+
* creates a thumbnail
17+
*
18+
* @param $imagePath
19+
* @param null $width
20+
* @param null $height
21+
* @param $target
22+
* @return bool
23+
*/
24+
public static function createThumbnail($imagePath, $width = null, $height = null, $target)
25+
{
26+
$manager = new ImageManager();
27+
28+
$manager->make($imagePath)
29+
->resize($width, $height)
30+
->save($target);
31+
32+
return true;
33+
}
34+
}

src/Darryldecode/Backend/Config/backend.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,14 @@
2222
/*
2323
* The title to be use on Backend
2424
*/
25-
'backend_title' => 'Laravel Backend'
25+
'backend_title' => 'Laravel Backend',
26+
27+
/*
28+
* Media Manager thumbnails
29+
*/
30+
'thumb_sizes' => array(
31+
'small' => array(150,120),
32+
'medium' => array(300,200),
33+
'large' => array(600,450),
34+
),
2635
];

0 commit comments

Comments
 (0)