bebeboutik/modules/importproduit/_importimage.php

181 lines
7.1 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
require_once(dirname(__FILE__).'/../../config/config.inc.php');
@ini_set('max_execution_time', 0);
if (!defined('_PS_BASE_URL_'))
define('_PS_BASE_URL_', Tools::getShopDomain(true));
$path = dirname(__FILE__).'/../../upload/fichierimport/IMG/';
$it = 25000;
$products = Product::getProducts(1, $it, 1000,'id_product','ASC');
foreach( $products as $product ){
$prodObj = new Product($product["id_product"]);
$prodObj->deleteImages();
$product_has_images = (bool)Image::getImages(1, $product["id_product"]);
$url = $path.$product["reference"]."Big.jpg";
$url2 = $path."Art".$product["reference"]."Big.jpg";
$ok = false;
if(file_exists($url2)) {
$image = new Image();
$image->id_product = (int)$product["id_product"];
$image->position = Image::getHighestPosition($product["id_product"]) + 1;
$image->cover = (!$product_has_images) ? true : false;
$field_error = $image->validateFields(false, true);
$lang_field_error = $image->validateFieldsLang(false, true);
if ($field_error === true && $lang_field_error === true && $image->add())
{
$image->associateTo(Shop::getContextListShopID());
if (!copyImg($product["id_product"], $image->id, $url2))
{
$this->warnings[] = sprintf(Tools::displayError('Error copying image: %s'), $url2);
$image->delete();
}
else
$id_image = array($image->id);
}
else
{
$this->warnings[] = sprintf(
Tools::displayError('%s cannot be saved'),
(isset($image->id_product) ? ' ('.$image->id_product.')' : '')
);
$this->errors[] = ($field_error !== true ? $field_error : '').(isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '').mysql_error();
}
$ok = true;
}
if(file_exists($url) && !$ok) {
$image = new Image();
$image->id_product = (int)$product["id_product"];
$image->position = Image::getHighestPosition($product["id_product"]) + 1;
$image->cover = (!$product_has_images) ? true : false;
$field_error = $image->validateFields(false, true);
$lang_field_error = $image->validateFieldsLang(false, true);
if ($field_error === true && $lang_field_error === true && $image->add())
{
$image->associateTo(Shop::getContextListShopID());
if (!copyImg($product["id_product"], $image->id, $url))
{
$this->warnings[] = sprintf(Tools::displayError('Error copying image: %s'), $url);
$image->delete();
}
else
$id_image = array($image->id);
}
else
{
$this->warnings[] = sprintf(
Tools::displayError('%s cannot be saved'),
(isset($image->id_product) ? ' ('.$image->id_product.')' : '')
);
$this->errors[] = ($field_error !== true ? $field_error : '').(isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '').mysql_error();
}
}
for($i = 0 ; $i < 10; $i++){
$url = $path.$product["reference"]."-0".$i."X300.jpg";
$url2 = $path."Art".$product["reference"]."-0".$i."X300.jpg";
$ok = false;
if(file_exists($url2)) {
$image = new Image();
$image->id_product = (int)$product["id_product"];
$image->position = Image::getHighestPosition($product["id_product"]) + 1;
$image->cover = false;
$field_error = $image->validateFields(false, true);
$lang_field_error = $image->validateFieldsLang(false, true);
if ($field_error === true && $lang_field_error === true && $image->add())
{
$image->associateTo(Shop::getContextListShopID());
if (!copyImg($product["id_product"], $image->id, $url2))
{
$this->warnings[] = sprintf(Tools::displayError('Error copying image: %s'), $url2);
$image->delete();
}
else
$id_image = array($image->id);
}
else
{
$this->warnings[] = sprintf(
Tools::displayError('%s cannot be saved'),
(isset($image->id_product) ? ' ('.$image->id_product.')' : '')
);
$this->errors[] = ($field_error !== true ? $field_error : '').(isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '').mysql_error();
}
$ok = true;
}
if(file_exists($url) && !$ok) {
$image = new Image();
$image->id_product = (int)$product["id_product"];
$image->position = Image::getHighestPosition($product["id_product"]) + 1;
$image->cover = false;
$field_error = $image->validateFields(false, true);
$lang_field_error = $image->validateFieldsLang(false, true);
if ($field_error === true && $lang_field_error === true && $image->add())
{
$image->associateTo(Shop::getContextListShopID());
if (!copyImg($product["id_product"], $image->id, $url))
{
$this->warnings[] = sprintf(Tools::displayError('Error copying image: %s'), $url);
$image->delete();
}
else
$id_image = array($image->id);
}
else
{
$this->warnings[] = sprintf(
Tools::displayError('%s cannot be saved'),
(isset($image->id_product) ? ' ('.$image->id_product.')' : '')
);
$this->errors[] = ($field_error !== true ? $field_error : '').(isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '').mysql_error();
}
}
}
echo $product["id_product"]."-".$it." ";
$it++;
ob_end_flush();
flush();
ob_start();
}
echo "stop";
function copyImg($id_entity, $id_image = null, $tmpfile, $entity = 'products')
{
$watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
switch ($entity)
{
default:
case 'products':
$image_obj = new Image($id_image);
$path = $image_obj->getPathForCreation();
break;
case 'categories':
$path = _PS_CAT_IMG_DIR_.(int)$id_entity;
break;
}
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (!ImageManager::checkImageMemoryLimit($tmpfile))
return false;
ImageManager::resize($tmpfile, $path.'.jpg');
$images_types = ImageType::getImagesTypes($entity);
foreach ($images_types as $image_type)
ImageManager::resize($tmpfile, $path.'-'.stripslashes($image_type['name']).'.jpg', $image_type['width'], $image_type['height']);
if (in_array($image_type['id_image_type'], $watermark_types))
Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
return true;
}
?>