Completed
Push — master ( 3b6041...637013 )
by Damian
10s
created

AssetAdminFile::getInsertWidth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Controller;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\Director;
7
use SilverStripe\ORM\DataExtension;
8
9
/**
10
 * Update File dataobjects to be editable in this asset admin
11
 *
12
 * @property File $owner
13
 */
14
class AssetAdminFile extends DataExtension
15
{
16
    /**
17
     * Max width for inserted images
18
     *
19
     * @config
20
     * @var int
21
     */
22
    private static $insert_width = 600;
0 ignored issues
show
Unused Code introduced by
The property $insert_width is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
24
    /**
25
     * Max height for inserted images
26
     *
27
     * @config
28
     * @var int
29
     */
30
    private static $insert_height = 400;
0 ignored issues
show
Unused Code introduced by
The property $insert_height is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
31
32
    public function updateCMSEditLink(&$link)
33
    {
34
        // Update edit link for this file to point to the new asset admin
35
        $controller = AssetAdmin::singleton();
36
        $link = Director::absoluteURL($controller->getFileEditLink($this->owner));
37
    }
38
39
    /**
40
     * Calculate width to insert into html area
41
     *
42
     * @return int|null
43
     */
44
    public function getInsertWidth() {
45
        $size = $this->getInsertDimensions();
46
        return $size ? $size['width'] : null;
47
    }
48
49
    /**
50
     * Calculate width to insert into html area
51
     *
52
     * @return int
53
     */
54
    public function getInsertHeight() {
55
        $size = $this->getInsertDimensions();
56
        return $size ? $size['height'] : null;
57
    }
58
59
    /**
60
     * Get dimensions of this image sized within insert_width x insert_height
61
     *
62
     * @return array|null
63
     */
64
    protected function getInsertDimensions() {
65
        $dimensions = $this->owner->getDimensions('array');
66
        if (!$dimensions) {
67
            return null;
68
        }
69
70
        list ($width, $height) = $dimensions;
71
        $maxWidth = $this->owner->config()->get('insert_width');
72
        $maxHeight = $this->owner->config()->get('insert_height');
73
74
        // Within bounds
75
        if ($width < $maxWidth && $height < $maxHeight) {
76
            return [
77
                'width' => $width,
78
                'height' => $height,
79
            ];
80
        }
81
82
        // Check if sizing by height or width
83
		if( ($width * $maxHeight) < ($height * $maxWidth) ) {
84
			// Size by height
85
            return [
86
                'width' => intval(($width * $maxHeight) / $height + 0.5),
87
                'height' => $maxHeight,
88
            ];
89
90
		} else {
91
			// Size by width
92
            return [
93
                'width' => $maxWidth,
94
                'height' => intval(($height * $maxWidth) / $width + 0.5),
95
            ];
96
		}
97
    }
98
}
99