If you're not comfortable building your own custom module and you're willing to look at alternative Algolia plugins, Scout offers a reasonably simple method for defining the data you want to send to Algolia just inside its config file, and you can easily define image transforms within the transformer function:
// config/scout.php
// Imports
use craft\elements\Entry;
use craft\elements\db\EntryQuery;
use rias\scout\ScoutIndex;
use spacecatninja\imagerx\ImagerX;
return [
'indices' => [
ScoutIndex::create("myAlgoliaIndexName")
->criteria(function(EntryQuery $query) {
return $query->section('myContent');
})
->transformer(function(Entry $entry) {
$logo = $entry->logo->one();
// Generate logo image transform for Algolia using Imager X
// (other image transform plugins/techniques are available!)
$logoTransform = ImagerX::$plugin->imager->transformImage($logo,
[
'width' => 300,
'height' => 300
],
[
'format' => 'webp',
]
);
// Return the data to sync with Algolia
return [
'id' => (int)$entry->id,
'url' => $entry->url,
'title' => $entry->title,
'logo' => $logoTransform->url,
];
}),
],
];
If you want multiple image transforms, you can generate them all and include them in an array of image URLs. The initial Scout indexing operation will take a little time since it has to generate the image transforms but providing those images are cached somewhere once they're generated, subsequent operations ought to be much quicker.
I've shown the code using the Imager X image transformation plugin (which is a paid plugin) because that's what I use with Scout so it was easy to grab a code example, but you could use Craft's own image transforms instead.