Completed
Push — develop ( b4fd40...785f8f )
by greg
03:10
created

Image::setServiceManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace PlaygroundCore\Service;
3
4
use ZfcBase\EventManager\EventProvider;
5
use Zend\ServiceManager\ServiceManager;
6
use Zend\ServiceManager\ServiceLocatorInterface;
7
8
/**
9
 * require php_exif for some methods
10
 */
11
class Image extends EventProvider
12
{
13
    
14
    protected $file;
15
    
16
    /**
17
     * @var resource
18
     */
19
    protected $image;
20
    
21
    /**
22
     *
23
     * @var ServiceManager
24
     */
25
    protected $serviceLocator;
26
27
    public function __construct(ServiceLocatorInterface $locator)
28
    {
29
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
$locator is of type object<Zend\ServiceManag...erviceLocatorInterface>, but the property $serviceLocator was declared to be of type object<Zend\ServiceManager\ServiceManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
30
    }
31
32
    /**
33
     * @param string $file
34
     * @throws \Exception if the file does not exists
35
     * @return \PlaygroundCore\Service\Image
36
     */
37
    public function setImage($file)
38
    {
39
        if (!file_exists($file)) {
40
            throw new \Exception('Not a file: "' . $file . '"', null, null);
41
        }
42
        $this->file = $file;
43
        $this->image = imagecreatefromstring(
44
            file_get_contents($file)
45
        );
46
        return $this;
47
    }
48
    
49
    /**
50
     * @return boolean
51
     */
52
    public function canCorrectOrientation()
53
    {
54
        return function_exists('exif_read_data')
55
                && (substr($this->file, -strlen('.jpg')) === '.jpg'
56
                    || substr($this->file, -strlen('.jpeg')) === '.jpeg');
57
    }
58
    
59
    /**
60
     * Correct image orientation (if present in the medadata)
61
     * use php_exif
62
     * @return \PlaygroundCore\Service\Image
63
     */
64
    public function correctOrientation()
65
    {
66
        try {
67
            $exif = exif_read_data($this->file);
68
        } catch (\Exception $e) {
69
            return $this;
70
        }
71
        if (!empty($exif['Orientation'])) {
72
            switch ($exif['Orientation']) {
73
                case 8:
74
                    $this->image = imagerotate($this->image, 90, 0);
75
                    break;
76
                case 3:
77
                    $this->image = imagerotate($this->image, 180, 0);
78
                    break;
79
                case 6:
80
                    $this->image = imagerotate($this->image, -90, 0);
81
                    break;
82
            }
83
        }
84
        return $this;
85
    }
86
    
87
    /**
88
     * save as jpeg
89
     * @param string $path
90
     * @return \PlaygroundCore\Service\Image
91
     */
92
    public function save($path = null)
93
    {
94
        if (is_null($path)) {
95
            $path = $this->file;
96
        }
97
        imagejpeg($this->image, $path);
98
        return $this;
99
    }
100
    
101
    /**
102
     * output as jpeg
103
     */
104
    public function __toString()
105
    {
106
        echo imagejpeg($this->image);
107
    }
108
}
109