Completed
Pull Request — master (#307)
by Damian
01:45
created

AssetAdminFile::getInsertDimensions()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 4
nop 0
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Controller;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Dev\Debug;
8
use SilverStripe\ORM\DataExtension;
9
10
/**
11
 * Update File dataobjects to be editable in this asset admin
12
 *
13
 * @property File $owner
14
 */
15
class AssetAdminFile extends DataExtension
16
{
17
    /**
18
     * Max width for inserted images
19
     *
20
     * @config
21
     * @var int
22
     */
23
    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...
24
25
    /**
26
     * Max height for inserted images
27
     *
28
     * @config
29
     * @var int
30
     */
31
    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...
32
33
    public function updateCMSEditLink(&$link)
34
    {
35
        // Update edit link for this file to point to the new asset admin
36
        $controller = AssetAdmin::singleton();
37
        $link = Director::absoluteURL($controller->getFileEditLink($this->owner));
38
    }
39
40
    /**
41
     * Calculate width to insert into html area
42
     *
43
     * @return int|null
44
     */
45
    public function getInsertWidth() {
46
        $size = $this->getInsertDimensions();
47
        return $size ? $size['width'] : null;
48
    }
49
50
    /**
51
     * Calculate width to insert into html area
52
     *
53
     * @return int
54
     */
55
    public function getInsertHeight() {
56
        $size = $this->getInsertDimensions();
57
        return $size ? $size['height'] : null;
58
    }
59
60
    /**
61
     * Get dimensions of this image sized within insert_width x insert_height
62
     *
63
     * @return array|null
64
     */
65
    protected function getInsertDimensions() {
66
        $dimensions = $this->owner->getDimensions('array');
67
        if (!$dimensions) {
68
            return null;
69
        }
70
71
        list ($width, $height) = $dimensions;
72
        $maxWidth = $this->owner->config()->get('insert_width');
73
        $maxHeight = $this->owner->config()->get('insert_height');
74
75
        // Within bounds
76
        if ($width < $maxWidth && $height < $maxHeight) {
77
            return [
78
                'width' => $width,
79
                'height' => $height,
80
            ];
81
        }
82
83
        // Check if sizing by height or width
84
		if( ($width * $maxHeight) < ($height * $maxWidth) ) {
85
			// Size by height
86
            return [
87
                'width' => intval(($width * $maxHeight) / $height + 0.5),
88
                'height' => $maxHeight,
89
            ];
90
91
		} else {
92
			// Size by width
93
            return [
94
                'width' => $maxWidth,
95
                'height' => intval(($height * $maxWidth) / $width + 0.5),
96
            ];
97
		}
98
    }
99
}
100