/
ImageAutorotator.php
108 lines (96 loc) · 2.65 KB
/
ImageAutorotator.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace Concrete\Core\File\Import\Processor;
use Concrete\Core\Config\Repository\Repository;
use Concrete\Core\File\Import\ImportingFile;
use Concrete\Core\File\Import\ImportOptions;
use Imagine\Filter\Basic\Autorotate;
use Imagine\Filter\Transformation;
use Imagine\Image\Metadata\ExifMetadataReader;
class ImageAutorotator implements PreProcessorInterface
{
/**
* Is this pre-processor enabled?
*
* @var bool
*/
private $enabled = true;
/**
* {@inheritdoc}
*
* @see \Concrete\Core\File\Import\Processor\ProcessorInterface::readConfiguration()
*/
public function readConfiguration(Repository $config)
{
$this->setEnabled($config->get('concrete.file_manager.images.use_exif_data_to_rotate_images'));
return $this;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\File\Import\Processor\PreProcessorInterface::getPreProcessPriority()
*/
public function getPreProcessPriority()
{
// This needs to run before the preprocessor that resizes images
return ImageSizeConstrain::PREPROCESSOR_PRIORITY + 10;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\File\Import\Processor\PreProcessorInterface::shouldPreProcess()
*/
public function shouldPreProcess(ImportingFile $file, ImportOptions $options)
{
if (!$this->isEnabled()) {
return false;
}
if (!ExifMetadataReader::isSupported()) {
return false;
}
switch ($file->getFileType()->getName()) {
case 'JPEG':
break;
default:
return false;
}
$image = $file->getImage();
if ($image === null) {
return false;
}
$autorotateFilter = new Autorotate();
$rotationArray = $autorotateFilter->getTransformations($image);
return count($rotationArray) > 0;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\File\Import\Processor\PreProcessorInterface::preProcess()
*/
public function preProcess(ImportingFile $file, ImportOptions $options)
{
$transformation = new Transformation();
$transformation->applyFilter($file->getImage(), new Autorotate());
$file->saveImage();
}
/**
* Is this pre-processor enabled?
*
* @return bool
*/
public function isEnabled()
{
return $this->enabled;
}
/**
* Is this pre-processor enabled?
*
* @param bool $value
*
* @return $this
*/
public function setEnabled($value)
{
$this->enabled = (bool) $value;
return $this;
}
}