Jump to content

How to generate image variations beforehand


picarica
 Share

Recommended Posts

Hello so i am loking for somehow to generate image variatoons before image visit, let me give u a scenario

clinet makes new page in CMS adds tons of images and i in backend have
 

                $large = $image->size(1200, 0);
                $thumb = $image->size(0, 350);

but none of us visit the page, so no variations are generated, but then when customer visits the pages only then the content is being generated, and it takes roughly about 1-2 minutes to generate 50 images, and customer doesnt want to wait that long, so i am asking if i can somehow pre-render this proccess without anyone visiting that current page

Link to comment
Share on other sites

Hi @picarica

You can achive it by hooking InputfieldFile::fileAdded like in the example below:

$this->wire()->addHookAfter('InputfieldFile::fileAdded', function ($event) {
	$inputfield = $event->object;
	if ($inputfield->hasField != 'image' && $inputfield->hasField != 'images') return;
	$image = $event->argumentsByName("pagefile");

	// Main post image
	if ($inputfield->hasField == 'image'
		&& $inputfield->hasPage
		&& $inputfield->hasPage->template->name == 'post') {
		$image->size('postMainImageSmall');
		$image->size('postMainImageMedium');
		$image->size('postMainImageMediumPortrait');
		$image->size('postMainImageLarge');
	}

	// Matrix block 'Gallery'
	if ($inputfield->hasField == 'images'
		&& $inputfield->hasPage
		&& $inputfield->hasPage->repeater_matrix_type == 5
		&& $inputfield->hasPage->template->name == 'repeater_builder') {
		$image->size('postBlockGallerySmall');
		$image->size('postBlockGalleryMedium');
		$image->size('postBlockGalleryLarge');
		$image->maxSize(config('imageSizes')['postBlockGalleryLarge']['width'], config('imageSizes')['postBlockGalleryLarge']['width']);
	}

	// Matrix block 'Slider'
	if ($inputfield->hasField == 'images'
		&& $inputfield->hasPage
		&& $inputfield->hasPage->repeater_matrix_type == 6
		&& $inputfield->hasPage->template->name == 'repeater_builder') {
		$image->size('postBlockGallerySmall');
		$image->size('postBlockGalleryMedium');
		$image->size('postBlockGalleryLarge');
		$image->maxSize(config('imageSizes')['postBlockSliderLarge']['width'], config('imageSizes')['postBlockSliderLarge']['width']);
	}

	// Matrix block 'Image'
	if ($inputfield->hasField == 'image'
		&& $inputfield->hasPage
		&& $inputfield->hasPage->repeater_matrix_type == 7
		&& $inputfield->hasPage->template->name == 'repeater_builder') {
		$image->size('postBlockImageSmall');
		$image->size('postBlockImageMedium');
		$image->size('postBlockImageLarge');
		$image->maxSize(config('imageSizes')['postBlockImageLarge']['width'], config('imageSizes')['postBlockImageLarge']['width']);
	}
});

 

  • Like 2
Link to comment
Share on other sites

I've never done that myself but you could also do a get request to that page automatically on save:

<?php
$wire->addHookAfter("Pages::saved(template=yourtemplate)", function($event) {
  $page = $event->arguments(0);
  (new WireHttp())->get($page->httpUrl);
});

This might slow down saving the page for the client, but it would create all image variations for website visitors and you'd not need to define all the image variations in the hook or keep your template and the hook in sync...

Happy to hear how that worked!

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...