1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Extension; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\Image; |
6
|
|
|
use SilverStripe\Core\Config\Configurable; |
7
|
|
|
use SilverStripe\ORM\DataExtension; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Adds some image size functions to the Image DataObject. |
11
|
|
|
* |
12
|
|
|
* @package shop |
13
|
|
|
*/ |
14
|
|
|
class ProductImageExtension extends DataExtension |
15
|
|
|
{ |
16
|
|
|
use Configurable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Image |
20
|
|
|
*/ |
21
|
|
|
protected $owner; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param bool $upscale [optional] |
25
|
|
|
* @return Image |
26
|
|
|
*/ |
27
|
|
|
public function getThumbnail($upscale = false) |
28
|
|
|
{ |
29
|
|
|
$width = self::config()->thumbnail_width; |
30
|
|
|
$height = self::config()->thumbnail_height; |
31
|
|
|
|
32
|
|
|
return $this->getImageAt($width, $height, $upscale); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param bool $upscale [optional] |
37
|
|
|
* @return Image |
38
|
|
|
*/ |
39
|
|
|
public function getContentImage($upscale = false) |
40
|
|
|
{ |
41
|
|
|
$width = self::config()->content_image_width; |
42
|
|
|
$height = self::config()->content_image_height; |
43
|
|
|
|
44
|
|
|
return $this->getImageAt($width, $height, $upscale); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param bool $upscale [optional] |
49
|
|
|
* @return Image |
50
|
|
|
*/ |
51
|
|
|
public function getLargeImage($upscale = false) |
52
|
|
|
{ |
53
|
|
|
$width = self::config()->large_image_width; |
54
|
|
|
$height = self::config()->large_image_height; |
55
|
|
|
|
56
|
|
|
return $this->getImageAt($width, $height, $upscale); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Resizes image by width or height only if the source image is bigger than the given width/height. |
61
|
|
|
* This prevents ugly upscaling. |
62
|
|
|
* |
63
|
|
|
* @param int $width [optional] |
64
|
|
|
* @param int $height [optional] |
65
|
|
|
* @param bool $upscale [optional] |
66
|
|
|
* |
67
|
|
|
* @return Image |
68
|
|
|
*/ |
69
|
|
|
public function getImageAt($width = null, $height = null, $upscale = false) |
70
|
|
|
{ |
71
|
|
|
if (!$this->owner->exists()) { |
72
|
|
|
return $this->owner; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$realWidth = $this->owner->getWidth(); |
76
|
|
|
$realHeight = $this->owner->getHeight(); |
77
|
|
|
|
78
|
|
|
if ($width && $height) { |
|
|
|
|
79
|
|
|
return $realWidth < $width && $realHeight < $height && !$upscale |
|
|
|
|
80
|
|
|
? $this->owner |
81
|
|
|
: $this->owner->Fit($width, $height); |
82
|
|
|
} else { |
83
|
|
|
if ($width) { |
|
|
|
|
84
|
|
|
return $realWidth < $width && !$upscale |
|
|
|
|
85
|
|
|
? $this->owner |
86
|
|
|
: $this->owner->ScaleWidth($width); |
87
|
|
|
} else { |
88
|
|
|
return $realHeight < $height && !$upscale |
|
|
|
|
89
|
|
|
? $this->owner |
90
|
|
|
: $this->owner->ScaleHeight($height); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return bool - is the image large enough that a "large" image makes sense? |
97
|
|
|
*/ |
98
|
|
|
public function HasLargeImage() |
99
|
|
|
{ |
100
|
|
|
$imageWidth = intval($this->owner->getWidth()); |
101
|
|
|
return $imageWidth > self::config()->content_image_width; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: