Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

src/Extension/ProductImageExtension.php (6 issues)

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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $width of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $height of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
79
            return $realWidth < $width && $realHeight < $height && !$upscale
0 ignored issues
show
Bug Best Practice introduced by
The expression return $realWidth < $wid...r->Fit($width, $height) also could return the type SilverStripe\Assets\Storage\DBFile which is incompatible with the documented return type SilverStripe\Assets\Image.
Loading history...
80
                ? $this->owner
81
                : $this->owner->Fit($width, $height);
82
        } else {
83
            if ($width) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $width of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
84
                return $realWidth < $width && !$upscale
0 ignored issues
show
Bug Best Practice introduced by
The expression return $realWidth < $wid...ner->ScaleWidth($width) also could return the type SilverStripe\Assets\Storage\DBFile which is incompatible with the documented return type SilverStripe\Assets\Image.
Loading history...
85
                    ? $this->owner
86
                    : $this->owner->ScaleWidth($width);
87
            } else {
88
                return $realHeight < $height && !$upscale
0 ignored issues
show
Bug Best Practice introduced by
The expression return $realHeight < $he...r->ScaleHeight($height) also could return the type SilverStripe\Assets\Storage\DBFile which is incompatible with the documented return type SilverStripe\Assets\Image.
Loading history...
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