Image::getInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * @license http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 *
15
 * @author Alexander Zakharov <[email protected]>
16
 * @link http://rootlocal.ru
17
 * @copyright Copyright © 2018 rootlocal.ru
18
 */
19
20
namespace image\components;
21
22
use \image\components\Kohana\Image as KohanaImage;
23
use yii\base\ErrorException;
24
use yii\base\BaseObject;
25
26
/**
27
 * Class Image
28
 * @package image\components
29
 */
30
class Image extends BaseObject
31
{
32
33
    /**
34
     * @var  string  default driver: GD, Imagick
35
     */
36
    public $driver;
37
38
    /**
39
     * @var Image
40
     */
41
    private static $_instance;
42
43
    /**
44
     * Loads the image to Image object
45
     * @param string $file the file path to the image
46
     * @param string $driver the image driver to use: GD or ImageMagick
47
     * @throws ErrorException if filename is empty or file doesn't exist
48
     * @return \image\components\Kohana\Image
49
     */
50
    public function load($file = null, $driver = null)
51
    {
52
        if (empty($driver))
53
            $driver = $this->driver;
54
55
        if (empty($file)) {
56
            throw new ErrorException('File name can not be empty');
57
        }
58
59
        if (!realpath($file)) {
60
            throw new ErrorException(sprintf('The file does\'t exist: %s', $file));
61
        }
62
63
        return KohanaImage::factory($file, $driver ? $driver : $this->driver);
64
    }
65
66
    /**
67
     * @param $file string
68
     * @param array $config
69
     * @return \image\components\Kohana\Image
70
     * @throws \yii\base\ErrorException
71
     */
72
    public static function getInstance($file, array $config = [])
73
    {
74
        self::$_instance = new self($config);
75
        return self::$_instance->load($file);
76
    }
77
78
}