1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\PerfectCmsImages\Tasks; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
6
|
|
|
use SilverStripe\Dev\BuildTask; |
7
|
|
|
use SilverStripe\ORM\DataObject; |
8
|
|
|
use SilverStripe\ORM\DB; |
9
|
|
|
|
10
|
|
|
class PerfectCmsImagesBuildTaskCheckImages extends BuildTask |
11
|
|
|
{ |
12
|
|
|
protected $title = 'Check Size of Images Uploaded'; |
13
|
|
|
|
14
|
|
|
protected $description = 'Checks the size of certain images to make sure they match specifications'; |
15
|
|
|
|
16
|
|
|
public function run($request) |
17
|
|
|
{ |
18
|
|
|
$this->outputToScreen('Expected URL parameters: ?parent=SiteTree&fieldname=MyImage&width=100&height=200', 'created'); |
19
|
|
|
$parent = $request->getVar('parent'); |
20
|
|
|
$fieldName = $request->getVar('fieldname'); |
21
|
|
|
$width = $request->getVar('width'); |
22
|
|
|
$height = $request->getVar('height'); |
23
|
|
|
if ($height || $width) { |
24
|
|
|
if (class_exists($parent)) { |
25
|
|
|
$singleton = Injector::inst()->get($parent); |
26
|
|
|
if ($singleton instanceof DataObject) { |
27
|
|
|
if ($singleton->hasMethod($fieldName)) { |
28
|
|
|
$objects = $parent::get()->where('"' . $fieldName . 'ID" <> 0 AND "' . $fieldName . 'ID" IS NOT NULL'); |
29
|
|
|
for ($i = 0; $i < 100000; ++$i) { |
30
|
|
|
$array = []; |
31
|
|
|
$obj = $objects->limit(1, $i)->first(); |
32
|
|
|
if (! $obj) { |
33
|
|
|
break; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$image = $obj->{$fieldName}(); |
37
|
|
|
if ($image && $image instanceof $image && $image->exists()) { |
38
|
|
|
if ($width) { |
39
|
|
|
$realWidth = $image->getWidth(); |
40
|
|
|
if ($realWidth !== $width) { |
41
|
|
|
$array[] = 'width is ' . round($width / $realWidth, 2) . '% of what it should be'; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($height) { |
46
|
|
|
$realHeight = $image->getHeight(); |
47
|
|
|
if ($realHeight !== $height) { |
48
|
|
|
$array[] = 'height is ' . round($height / $realHeight, 2) . '% of what it should be'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (count($array) > 0) { |
53
|
|
|
$this->outputToScreen('ERRORS WITH: ' . $obj->getTitle() . ' --- ' . implode('; ', $array), 'deleted'); |
54
|
|
|
} else { |
55
|
|
|
$this->outputToScreen('PERFECT PASS FOR: ' . $obj->getTitle()); |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
|
|
$this->outputToScreen('Skipping ' . $obj->getTitle() . ' as it does not have a valid image attached to it.'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
|
|
$this->outputToScreen('Please specify a valid field name like this fieldname=xxx, where xxx is the field name (e.g. Image).', 'deleted'); |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
$this->outputToScreen('Please specify a valid class name like this parent=xxx, where xxx is the class name that is a valid data object.', 'deleted'); |
66
|
|
|
} |
67
|
|
|
} else { |
68
|
|
|
$this->outputToScreen('Please specify a valid class name like this parent=xxx, where xxx is the class name.', 'deleted'); |
69
|
|
|
} |
70
|
|
|
} else { |
71
|
|
|
$this->outputToScreen('Please specify at least one of height or width.', 'deleted'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
echo '<h1>--- COMPLETED ---</h1>'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $message |
79
|
|
|
* @param string $type |
80
|
|
|
*/ |
81
|
|
|
protected function outputToScreen($message, $type = '') |
82
|
|
|
{ |
83
|
|
|
echo ' '; |
84
|
|
|
flush(); |
85
|
|
|
ob_end_flush(); |
86
|
|
|
DB::alteration_message($message, $type); |
87
|
|
|
ob_start(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|