Passed
Push — develop ( c32f42...a4fab3 )
by Andrew
19:05
created

ImageTransform::assetFromAssetOrId()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\helpers;
13
14
use nystudio107\seomatic\Seomatic;
15
use nystudio107\seomatic\helpers\Environment as EnvironmentHelper;
16
17
use Craft;
18
use craft\elements\Asset;
19
use craft\models\AssetTransform;
20
21
/**
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
23
 * @package   Seomatic
24
 * @since     3.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
25
 */
26
class ImageTransform
27
{
28
    // Constants
29
    // =========================================================================
30
31
    const SOCIAL_TRANSFORM_QUALITY = 82;
32
33
    const ALLOWED_SOCIAL_MIME_TYPES = [
34
        'image/jpeg',
35
        'image/png',
36
    ];
37
38
    const DEFAULT_SOCIAL_FORMAT = 'jpg';
39
40
    // Static Properties
41
    // =========================================================================
42
43
    static private $transforms = [
0 ignored issues
show
Coding Style introduced by
Private member variable "transforms" must be prefixed with an underscore
Loading history...
44
        'base' => [
45
            'format' => null,
46
            'quality' => self::SOCIAL_TRANSFORM_QUALITY,
47
            'width' => 1200,
48
            'height' => 630,
49
            'mode' => 'crop',
50
        ],
51
        'facebook' => [
52
            'width' => 1200,
53
            'height' => 630,
54
        ],
55
        'twitter-summary' => [
56
            'width' => 800,
57
            'height' => 800,
58
        ],
59
        'twitter-large' => [
60
            'width' => 800,
61
            'height' => 418,
62
        ],
63
        'schema-logo' => [
64
            'format' => 'png',
65
            'width' => 600,
66
            'height' => 60,
67
            'mode' => 'fit',
68
        ],
69
    ];
70
71
    // Static Methods
72
    // =========================================================================
73
74
    /**
75
     * Transform the $asset for social media sites in $transformName and
76
     * optional $siteId
77
     *
78
     * @param int|Asset $asset         the Asset or Asset ID
79
     * @param string    $transformName the name of the transform to apply
80
     * @param int|null  $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
81
     * @param string    $transformMode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     *
83
     * @return string URL to the transformed image
84
     */
85
    public static function socialTransform(
86
        $asset,
87
        $transformName = '',
88
        $siteId = null,
89
        $transformMode = null
90
    ): string {
91
        $url = '';
92
        $transform = self::createSocialTransform($transformName);
93
        // Let them override the mode
94
        if ($transform !== null) {
95
            $transform->mode = $transformMode ?? $transform->mode;
96
        }
97
        $asset = self::assetFromAssetOrId($asset, $siteId);
98
        if (($asset !== null) && ($asset instanceof Asset)) {
99
            // Make sure the format is an allowed format, otherwise explicitly change it
100
            $mimeType = $asset->getMimeType();
101
            if (!\in_array($mimeType, self::ALLOWED_SOCIAL_MIME_TYPES, false)) {
102
                $transform->format = self::DEFAULT_SOCIAL_FORMAT;
103
            }
104
            // Generate a transformed image
105
            $assets = Craft::$app->getAssets();
106
            // If we're not in local dev, tell it to generate the transform immediately so that
107
            // urls like `actions/assets/generate-transform` don't get cached
108
            $generateNow = Seomatic::$environment === EnvironmentHelper::SEOMATIC_DEV_ENV ? null : true;
109
            $url = $assets->getAssetUrl($asset, $transform, $generateNow);
110
            if ($url === null) {
111
                $url = '';
112
            }
113
            // If we have a url, add an `mtime` param to cache bust
114
            if (!empty($url) && empty(parse_url($url, PHP_URL_QUERY)))) {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')' on line 114 at column 70
Loading history...
115
                $url = UrlHelper::url($url, [
116
                    'mtime' => $asset->dateModified->getTimestamp(),
117
                ]);
118
            }
119
        }
120
121
        return $url;
122
    }
123
124
    /**
125
     * @param int|Asset $asset         the Asset or Asset ID
126
     * @param string    $transformName the name of the transform to apply
127
     * @param int|null  $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
128
     * @param string    $transformMode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     *
130
     * @return string width of the transformed image
131
     */
132
    public static function socialTransformWidth(
133
        $asset,
134
        $transformName = '',
135
        $siteId = null,
136
        $transformMode = null
137
    ): string {
138
        $width = '';
139
        $transform = self::createSocialTransform($transformName);
140
        // Let them override the mode
141
        if ($transform !== null) {
142
            $transform->mode = $transformMode ?? $transform->mode;
143
        }
144
        $asset = self::assetFromAssetOrId($asset, $siteId);
145
        if (($asset !== null) && ($asset instanceof Asset)) {
146
            $width = (string)$asset->getWidth($transform);
147
            if ($width === null) {
148
                $width = '';
149
            }
150
        }
151
152
        return $width;
153
    }
154
155
    /**
156
     * @param int|Asset $asset         the Asset or Asset ID
157
     * @param string    $transformName the name of the transform to apply
158
     * @param int|null  $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
159
     * @param string    $transformMode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
160
     *
161
     * @return string width of the transformed image
162
     */
163
    public static function socialTransformHeight(
164
        $asset,
165
        $transformName = '',
166
        $siteId = null,
167
        $transformMode = null
168
    ): string {
169
        $height = '';
170
        $transform = self::createSocialTransform($transformName);
171
        // Let them override the mode
172
        if ($transform !== null) {
173
            $transform->mode = $transformMode ?? $transform->mode;
174
        }
175
        $asset = self::assetFromAssetOrId($asset, $siteId);
176
        if (($asset !== null) && ($asset instanceof Asset)) {
177
            $height = (string)$asset->getHeight($transform);
178
            if ($height === null) {
179
                $height = '';
180
            }
181
        }
182
183
        return $height;
184
    }
185
186
    /**
187
     * Return an array of Asset elements from an array of element IDs
188
     *
189
     * @param array|string $assetIds
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
190
     * @param int|null     $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
191
     *
192
     * @return array
193
     */
194
    public static function assetElementsFromIds($assetIds, $siteId = null): array
195
    {
196
        $elements = Craft::$app->getElements();
197
        $assets = [];
198
        if (!empty($assetIds)) {
199
            if (\is_array($assetIds)) {
200
                foreach ($assetIds as $assetId) {
201
                    $assets[] = $elements->getElementById($assetId, Asset::class, $siteId);
202
                }
203
            } else {
204
                $assetId = $assetIds;
205
                $assets[] = $elements->getElementById($assetId, Asset::class, $siteId);
206
            }
207
        }
208
209
        return $assets;
210
    }
211
212
    // Protected Static Methods
213
    // =========================================================================
214
215
    /**
216
     * Return an asset from either an id or an asset
217
     *
218
     * @param int|Asset $asset         the Asset or Asset ID
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 9 found
Loading history...
219
     * @param int|null  $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
220
     *
221
     * @return Asset|null
222
     */
223
    protected static function assetFromAssetOrId($asset, $siteId = null)
224
    {
225
        if (empty($asset)) {
226
            return null;
227
        }
228
        return ($asset instanceof Asset) ? $asset : Craft::$app->getAssets()->getAssetById($asset, $siteId);
229
    }
230
231
    /**
232
     * Create a transform from the passed in $transformName
233
     *
234
     * @param string    $transformName the name of the transform to apply
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
235
     *
236
     * @return AssetTransform|null
237
     */
238
    protected static function createSocialTransform($transformName = 'base')
239
    {
240
        $transform = null;
241
        if (!empty($transformName)) {
242
            $config = array_merge(
243
                self::$transforms['base'],
244
                self::$transforms[$transformName] ?? self::$transforms['base']
245
            );
246
            $transform = new AssetTransform($config);
247
        }
248
249
        return $transform;
250
    }
251
}
252