Skip to content

Commit 76454e7

Browse files
authored
Merge pull request #10 from codewithkyle/hotfix-1.2.1
Closes #9 - fixed inline transform function
2 parents ec71eaf + 17e55e7 commit 76454e7

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
66

77
## [Unreleased]
88

9+
## [1.2.0] - 2021-06-19
10+
11+
### Fixed
12+
13+
- `craft.jitter.transformImage()` bug ([#9](https://github.com/codewithkyle/craft-jitter/issues/9))
14+
- S3 bucket config bug
15+
- `craft.jitter.srcset()` removes the temp files it creates
16+
917
## [1.2.0] - 2021-06-12
1018

1119
### Fixed
@@ -49,7 +57,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
4957
- delete local files
5058
- delete S3 files
5159

52-
[Unreleased]: https://github.com/codewithkyle/craft-jitter/compare/v1.1.2...HEAD
60+
[Unreleased]: https://github.com/codewithkyle/craft-jitter/compare/v1.2.1...HEAD
61+
[1.2.1]: https://github.com/codewithkyle/craft-jitter/compare/v1.2.0...v1.2.1
62+
[1.2.0]: https://github.com/codewithkyle/craft-jitter/compare/v1.1.2...v1.2.0
5363
[1.1.2]: https://github.com/codewithkyle/craft-jitter/compare/v1.1.1...v1.1.2
5464
[1.1.1]: https://github.com/codewithkyle/craft-jitter/compare/v1.1.0...v1.1.1
5565
[1.1.0]: https://github.com/codewithkyle/craft-jitter/compare/v1.0.0...v1.1.0

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "codewithkyle/jitter",
33
"description": "A just in time image transformation service.",
44
"type": "craft-plugin",
5-
"version": "1.2.0",
5+
"version": "1.2.1",
66
"keywords": [
77
"craft",
88
"cms",

src/services/Transform.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,38 @@ public function clearS3BucketCache()
5858
}
5959
}
6060

61+
public function generateURL(array $params): string
62+
{
63+
$ret = "/jitter/v1/transform?";
64+
$asset = Asset::find()->id($params["id"])->one();
65+
if (empty($asset))
66+
{
67+
Craft::error("Failed to find asset with an id of " . $id, __METHOD__);
68+
}
69+
else
70+
{
71+
$masterImage = $asset->getCopyOfFile();
72+
}
73+
if ($masterImage)
74+
{
75+
$isFrist = true;
76+
foreach ($params as $key => $value)
77+
{
78+
if ($isFrist)
79+
{
80+
$ret .= $key . "=" . $value;
81+
}
82+
else
83+
{
84+
$ret .= "&" . $key . "=" . $value;
85+
}
86+
$isFrist = false;
87+
}
88+
\unlink($masterImage);
89+
}
90+
return $ret;
91+
}
92+
6193
public function generateSourceSet(string $id, array $images): string
6294
{
6395
$masterImage = null;
@@ -116,6 +148,7 @@ public function generateSourceSet(string $id, array $images): string
116148
$ret .= ", ";
117149
}
118150
}
151+
\unlink($masterImage);
119152
}
120153
return $ret;
121154
}
@@ -262,14 +295,18 @@ private function checkCache($settings, string $key): array
262295
if (\file_exists($path))
263296
{
264297
$s3 = $this->connectToS3($settings);
298+
$s3Key = $key;
265299
if (isset($settings['folder']))
266300
{
267-
$key = $settings['folder'] . "/" . $key;
301+
$s3Key = $settings['folder'] . "/" . $s3Key;
268302
}
269-
$response = $s3->getObject([
270-
"Bucket" => getenv("S3_BUCKET"),
271-
"Key" => $key,
303+
$file = $s3->getObject([
304+
"Bucket" => $settings["bucket"],
305+
"Key" => $s3Key,
272306
]);
307+
$response["Body"] = $file["Body"];
308+
$response["Name"] = $key;
309+
$response["ContentType"] = $file["ContentType"];
273310
}
274311
}
275312
else
@@ -282,7 +319,7 @@ private function checkCache($settings, string $key): array
282319
$response["ContentType"] = \mime_content_type($path);
283320
}
284321
}
285-
return $response;
322+
return (array)$response;
286323
}
287324

288325
private function cacheImage($settings, $key, $image): void

src/variables/JitterVariable.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
namespace codewithkyle\jitter\variables;
1212

1313
use codewithkyle\jitter\Jitter;
14+
use codewithkyle\jitter\exceptions\JitterException;
1415

1516
use Craft;
1617
use craft\elements\Asset;
1718

19+
1820
/**
1921
* Jitter Variable
2022
*
@@ -34,19 +36,21 @@ class JitterVariable
3436

3537
public function transformImage(Asset $file, $params): string
3638
{
37-
$request = Craft::$app->getRequest();
38-
$clientAcceptsWebp = $request->accepts('image/webp');
39-
$params = json_decode(json_encode($params), true);
40-
$params['id'] = $file->id;
41-
$response = Jitter::getInstance()->transform->transformImage($params, $clientAcceptsWebp);
42-
if ($response['success'])
39+
$url = "";
40+
try
4341
{
44-
return $response['url'];
42+
$request = Craft::$app->getRequest();
43+
$clientAcceptsWebp = $request->accepts('image/webp');
44+
$params = json_decode(json_encode($params), true);
45+
$params['id'] = $file->id;
46+
$file = Jitter::getInstance()->transform->transformImage($params, $clientAcceptsWebp);
47+
$url = Jitter::getInstance()->transform->generateURL($params);
4548
}
46-
else
49+
catch (JitterException $e)
4750
{
48-
Craft::error($response['error'], __METHOD__);
51+
Craft::error($e->getMessage(), __METHOD__);
4952
}
53+
return $url;
5054
}
5155

5256
public function srcset(Asset $file, array $params): string

0 commit comments

Comments
 (0)