ResizeHelper::resize()   C
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 0
loc 34
rs 6.7272
cc 7
eloc 19
nc 4
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: tetsu0o
5
 * Date: 29/12/14
6
 * Time: 02:03
7
 */
8
9
namespace Mykees\MediaBundle\Helper;
10
11
use Imagine\Image\ImageInterface;
12
13
class ResizeHelper {
14
15
    public $options;
16
    public $webroot;
17
18
19
    public function __construct(array $resize_option, $webroot){
20
        $this->options = $resize_option;
21
        $this->webroot = $webroot.'/img/';
22
    }
23
24
25
    public function resize($image)
26
    {
27
        $absolute_path = $this->webroot . $image;
28
        $absolute_info = pathinfo($absolute_path);
29
        $allowedExtension = ['jpg','JPG','jpeg',"JPEG",'png','PNG','gif','GIF'];
30
        $extension = pathinfo($image);
31
32
        if(in_array($extension['extension'],$allowedExtension))
33
        {
34
            if(!empty($this->options))
35
            {
36
                foreach($this->options['size'] as $k=>$v)
37
                {
38
                    $width = $v['width'];
39
                    $height = $v['height'];
40
                    $dest = $absolute_info['dirname'] . '/' . $absolute_info['filename'] . "_$width" . "x$height" . '.jpg';
41
42
                    if(file_exists($dest))
43
                    {
44
                        return false;
45
                    }
46
47
                    $imagine = new \Imagine\Gd\Imagine();
48
                    $mode = $this->options['mode'];
49
50
                    $imagine->open($absolute_info['dirname'] . '/' . $absolute_info['filename'] . '.jpg')
51
                        ->thumbnail(new \Imagine\Image\Box($width,$height), !empty($mode) && $mode == 'inset' ? ImageInterface::THUMBNAIL_INSET : ImageInterface::THUMBNAIL_OUTBOUND)
52
                        ->save($dest);
53
                }
54
            }
55
        }
56
57
        return true;
58
    }
59
}
60