We have a custom module to show images from sw3 on the product page, so the main image and any gallery images. The code works but going from a list page to the PDP is very slow and when I disable this module so that Magento uses images that have been uploaded things speed up dramatically so I think this custome module is slowing things down a lot. I'm wondering is it the code itself or the fact that it's calling s3.amazonaws.com that is the issue. Is there any way to speed it up?
(Since it works, I'm not going to include the di.xml, module.xml and registration.php files)
Plugin/AddImagesToGalleryBlock.php
<?php
namespace XX\CDNExtraImages\Plugin;
use Magento\Catalog\Block\Product\View\Gallery;
use Magento\Framework\Data\Collection;
use Magento\Framework\Data\CollectionFactory;
use Magento\Framework\DataObject;
class AddImagesToGalleryBlock
{
/**
* @var CollectionFactory
*/
protected $dataCollectionFactory;
/**
* AddImagesToGalleryBlock constructor.
*
* @param CollectionFactory $dataCollectionFactory
*/
public function __construct(
CollectionFactory $dataCollectionFactory
) {
$this->dataCollectionFactory = $dataCollectionFactory;
}
/**
* afterGalleryImages Plugin to change images and use external images stored in custom attribute
*
* @param Gallery $subject
* @param Collection|null $images
* @return Collection|null
*/
public function afterGetGalleryImages(Gallery $subject, $images) {
try {
$product = $subject->getProduct();
$images = $this->dataCollectionFactory->create();
$productName = $product->getName();
$sku = $product->getSku();
$externalImages = array();
$baseimage = "https://xxx.s3.amazonaws.com/".$sku.".jpg";
$base_headers = @get_headers($baseimage);
if(!$base_headers || $base_headers[0] == 'HTTP/1.1 404 Not Found') {
$baseimage = "https://xxx.s3.amazonaws.com/placeholder.jpg";
}
$externalImages[0] = $baseimage;
$max = 5;
//$exists = true;
for ($x = 1; $x <= $max; $x++) {
$newimage = "https://xxx.s3.amazonaws.com/".$sku."_".$x.".jpg";
$file_headers = @get_headers($newimage);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
//$exists = false;
break;
}
else {
//$exists = true;
array_push($externalImages, $newimage);
}
}
foreach ($externalImages as $item) {
$imageId = uniqid();
$small = $item;
$medium = $item;
$large = $item;
$image = [
'file' => $large,
'media_type' => 'image',
'value_id' => $imageId, // unique value
'row_id' => $imageId, // unique value
'label' => $productName,
'label_default' => $productName,
'position' => 100,
'position_default' => 100,
'disabled' => 0,
'url' => $large,
'path' => '',
'small_image_url' => $small,
'medium_image_url' => $medium,
'large_image_url' => $large
];
$images->addItem(new DataObject($image));
}
return $images;
} catch (\Exception $e) {
return $images;
}
}
}
Thanks for any help.
ETA - in response to byalexes' answer. I am waiting to get client's credentials. But in the meantime, should it be something like the following? Do I need to put the s3 address in the Magento admin as the Base url for media files? We do have images imported into magento for the lists etc so don't want to go to s3 for everything.
....
$sku = $product->getSku();
$credentials = new \Aws\Credentials\Credentials($key, $secret);
$s3Client = new \Aws\S3\S3Client([
'version' => $version,
'region' => $region,
'credentials' => $credentials
]);
$externalImages = $s3Client->getIterator('ListObjects', ['Bucket' => $bucket, 'Prefix' => $sku]);
foreach ($externalImages as $item) {$imageId = uniqid();
$small = $item;
$medium = $item;
$large = $item;
$image = [
'file' => $large,
'media_type' => 'image',
.......
];
$images->addItem(new DataObject($image));
}
return $images;