Untitled
unknown
plain_text
3 months ago
5.4 kB
10
Indexable
public function NewImageUpload($product, array $images)
{
$sku = $product->getSku();
$imagesAdded = 0;
// Remove all existing images from product object (in memory, no save here)
$product->setMediaGalleryEntries([]);
$this->productRepository->save($product);
$this->clearImageOverrides($sku);
// Clean up product image folder
$mediaPath = $this->mediaDirectory->getAbsolutePath('catalog/product/');
$productImageDir = $mediaPath . $sku . '/';
if (!$this->file->fileExists($productImageDir)) {
$this->file->mkdir($productImageDir, 0777);
}
$files = glob($productImageDir . '*');
foreach ($files as $file) {
if (is_file($file)) {
if (!unlink($file)) {
$this->createLog("Failed to delete leftover file: {$file}");
} else {
//$this->createLog("Deleted leftover file: {$file}");
}
}
}
foreach ($images as $imgData) {
if (empty($imgData['image_url']) || empty($imgData['file_name'])) {
continue;
}
$imageUrl = $imgData['image_url'].'?v='.time();
$fileName = $imgData['file_name'];
$sequence = $imgData['sequence'] ?? 0;
try {
$this->curl->setOption(CURLOPT_RETURNTRANSFER, true);
$this->curl->setOption(CURLOPT_FOLLOWLOCATION, true);
$this->curl->setOptions([
CURLOPT_HTTPHEADER => [
'Cache-Control: no-cache, no-store, must-revalidate',
'Pragma: no-cache',
'Expires: 0'
]
]);
$this->curl->get($imageUrl);
if ($this->curl->getStatus() == 200) {
$imageContent = $this->curl->getBody();
if (!empty($imageContent)) {
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$baseName = pathinfo($fileName, PATHINFO_FILENAME);
$sourcePath = $productImageDir . 'temp_' . $baseName . '.' . $ext;
$targetPath = $productImageDir . $baseName . '.jpg';
// Save original file
$this->file->write($sourcePath, $imageContent);
// Convert to jpg
$converted = false;
if ($ext === 'jpg' || $ext === 'jpeg') {
rename($sourcePath, $targetPath);
$converted = true;
} else {
$converted = $this->directConvertToJpg($sourcePath, $targetPath, $ext);
if (file_exists($sourcePath)) {
unlink($sourcePath);
}
}
if ($converted && file_exists($targetPath)) {
$relativeImagePath = 'catalog/product/' . $sku . '/' . basename($targetPath);
$roles = ($sequence == 1) ? ['image', 'small_image', 'thumbnail'] : [];
if (!$this->isImageAlreadyAdded($product, $targetPath)) {
//$product->setStoreId(1); // Set store ID to 0 for default store
$product->addImageToMediaGallery(
$targetPath,
$roles,
false,
false
);
// $product->setStoreId(1);
// $product->save();
$imagesAdded++;
} else {
$this->createLog("Skipped duplicate image: {$relativeImagePath}");
}
}
}
}
} catch (\Exception $e) {
$this->createLog("Failed to download/convert image: {$imageUrl} - " . $e->getMessage());
}
}
$product->save();
// Final save
if ($imagesAdded > 0) {
try {
$product->setStoreId(0);
$this->productRepository->save($product);
$this->createLog("NewImageUpload: final repository save succeeded for {$sku}");
$this->assignRolesToFirstImage($sku);
} catch (\Exception $e) {
// $this->createLog('NewImageUpload: final repository save failed for ' . $sku . ': ' . $e->getMessage());
// Images are already persisted by the per-image model saves in the loop above,
// so we can still assign roles from whatever is already in the gallery.
$this->assignRolesToFirstImage($sku);
}
} else {
$this->createLog("No images were successfully processed for product {$sku}");
}
}Editor is loading...
Leave a Comment