Skip to content

Commit

Permalink
Passthrough cached image content instead of performing 302 redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
code418 committed Oct 3, 2018
1 parent c2665e9 commit 51e3a05
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author Richard Brown <richard@agilepixel.io>
* @copyright 2018 Agile Pixel
*
* @version v0.0.3
* @version v0.1.1
*
*/

Expand Down
14 changes: 11 additions & 3 deletions src/ImagesRespond.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author Richard Brown <richard@agilepixel.io>
* @copyright 2018 Agile Pixel
*
* @version v1.0.0
* @version v0.1.1
*
*/

Expand Down Expand Up @@ -68,7 +68,7 @@ public function respond($request, $echo = true)
if ($this->using_webp === false || ($this->using_webp === true && \Imagick::queryFormats('WEBP'))) {
Image::configure(['driver' => 'imagick']);
}
} else if ($this->using_webp === true && !function_exists('imagewebp')) {
} elseif ($this->using_webp === true && !function_exists('imagewebp')) {
$this->using_webp = false;
}

Expand All @@ -87,12 +87,17 @@ public function respond($request, $echo = true)
}

$encode = $matches[4];
$imageType = $encode;
if ($this->using_webp) {
$encode = 'webp';
$imageType = $encode;
}
if ($encode == 'jpeg') {
$encode = 'jpg';
}
if ($encode == 'jpg') {
$imageType = 'jpeg';
}

$size = $matches[2];
$file = __DIR__ . '/' . $this->options['root_dir'].$matches[1].$matches[3].$matches[4];
Expand All @@ -116,7 +121,10 @@ public function respond($request, $echo = true)
$outputfilename = __DIR__ . '/' . $this->options['root_dir'].$outputurl;
if (file_exists($outputfilename)) {
if ($echo) {
header('Location: '.$outputurl, true, 302);
$fp = fopen($outputfilename, 'rb');
header('Content-Type: image/'.$imageType);
header('Content-Length: ' . filesize($outputfilename));
fpassthru($fp);
exit;
} else {
return Image::make($outputfilename);
Expand Down
2 changes: 1 addition & 1 deletion src/rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author Richard Brown <richard@agilepixel.io>
* @copyright 2018 Agile Pixel
*
* @version v0.0.3
* @version v0.1.1
*
*/

Expand Down

0 comments on commit 51e3a05

Please sign in to comment.