Skip to content

Commit

Permalink
Vastly increase the speed with which the transforms are created when …
Browse files Browse the repository at this point in the history
…done via Twig templating code

Signed-off-by: Andrew Welch <[email protected]>
  • Loading branch information
khalwat committed Apr 7, 2018
1 parent 622df2f commit 56350ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/services/Optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function handleGenerateTransformEvent(GenerateTransformEvent $event)

$settings = ImageOptimize::$plugin->getSettings();
// Only do this for local Craft transforms
if ($settings->transformMethod == 'craft' && !empty($event->asset) && ImageOptimize::$generatePlaceholders) {
if ($settings->transformMethod == 'craft' && !empty($event->asset)) {
// Apply any filters to the image
if (!empty($event->transformIndex->transform)) {
$this->applyFiltersToImage($event->transformIndex->transform, $event->asset, $event->image);
Expand Down
10 changes: 8 additions & 2 deletions src/services/OptimizedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class OptimizedImages extends Component
*/
public function createOptimizedImages(Asset $asset, $variants = [])
{
Craft::beginProfile('createOptimizedImages', __METHOD__);
if (empty($variants)) {
$settings = ImageOptimize::$plugin->getSettings();
if ($settings) {
Expand All @@ -60,6 +61,7 @@ public function createOptimizedImages(Asset $asset, $variants = [])

$model = new OptimizedImage();
$this->populateOptimizedImageModel($asset, $variants, $model);
Craft::endProfile('createOptimizedImages', __METHOD__);

return $model;
}
Expand All @@ -71,6 +73,7 @@ public function createOptimizedImages(Asset $asset, $variants = [])
*/
public function populateOptimizedImageModel(Asset $asset, $variants, OptimizedImage $model)
{
Craft::beginProfile('populateOptimizedImageModel', __METHOD__);
$settings = ImageOptimize::$plugin->getSettings();
// Empty our the optimized image URLs
$model->optimizedImageUrls = [];
Expand Down Expand Up @@ -137,6 +140,7 @@ public function populateOptimizedImageModel(Asset $asset, $variants, OptimizedIm
);
}
}
Craft::endProfile('populateOptimizedImageModel', __METHOD__);
}

// Protected Methods
Expand Down Expand Up @@ -219,6 +223,7 @@ protected function getTransformFromVariant(Asset $asset, $variant, $retinaSize):
*/
protected function addVariantImageToModel(Asset $asset, OptimizedImage $model, $transform, $variant, $aspectRatio)
{
Craft::beginProfile('addVariantImageToModel', __METHOD__);
// Generate an image transform url
$url = ImageOptimize::$transformClass::getTransformUrl(
$asset,
Expand All @@ -233,11 +238,11 @@ protected function addVariantImageToModel(Asset $asset, OptimizedImage $model, $
if (!empty($url)) {
$model->variantSourceWidths[] = $variant['width'];
// Store & prefetch image at the image URL
ImageOptimize::$transformClass::prefetchRemoteFile($url);
//ImageOptimize::$transformClass::prefetchRemoteFile($url);
$model->optimizedImageUrls[$transform->width] = $url;
// Store & prefetch image at the webp URL
$webPUrl = ImageOptimize::$transformClass::getWebPUrl($url);
ImageOptimize::$transformClass::prefetchRemoteFile($webPUrl);
//ImageOptimize::$transformClass::prefetchRemoteFile($webPUrl);
$model->optimizedWebPImageUrls[$transform->width] = $webPUrl;
$model->focalPoint = $asset->focalPoint;
$model->originalImageWidth = $asset->width;
Expand All @@ -253,6 +258,7 @@ protected function addVariantImageToModel(Asset $asset, OptimizedImage $model, $
__METHOD__
);
}
Craft::endProfile('addVariantImageToModel', __METHOD__);
}

/**
Expand Down

0 comments on commit 56350ea

Please sign in to comment.