forked from andrewblake1/yii2-file-upload-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageHelper.php
49 lines (39 loc) · 1.29 KB
/
ImageHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* @copyright Copyright (c) 2013 2amigOS! Consulting Group LLC
* @link http://2amigos.us
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
*/
namespace dosamigos\fileupload;
use yii\imagine\Image;
use Imagine\Image\ManipulatorInterface;
use Yii;
use Imagine\Image\Box;
/**
* ImageHelper manipulates images
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @link http://www.ramirezcobos.com/
* @link http://www.2amigos.us/
* @package dosamigos\fileupload
*/
class ImageHelper
{
public static function saveTemporaryImage($filename, $name, $size, $basePath)
{
list($width, $height) = explode('x', str_replace('/', '', $size));
$runtimeDir = Yii::$app->getRuntimePath() . '/' . $basePath . '/' . $size;
exec("mkdir -p $runtimeDir");
$name = $runtimeDir . '/' . $name;
$file = Image::getImagine()->open($filename);
if ($file->getSize()->getWidth() < $width) {
$file->resize($file->getSize()->widen($width));
}
if ($file->getSize()->getHeight() < $height) {
$file->resize($file->getSize()->heighten($height));
}
$file->thumbnail(new Box($width, $height), ManipulatorInterface::THUMBNAIL_OUTBOUND)
->save($name);
return $name;
}
}