Jump to content

Import products with image


Recommended Posts

Hello,
I'm trying to develop a module to import products into the prestashop catalog. It works fine, however I have a problem importing the associated images. I found this code to copy an image from an url and associate it with the product:

if (($souche->resource->subject->id) == 'Bactérie') {
$url = 'https://image.flaticon.com/icons/png/512/84/84495.png';
} elseif (($souche->resource->subject->id) == 'Virus') {
$url = 'https://image.flaticon.com/icons/png/512/108/108659.png';
} else {
$url = 'https://image.flaticon.com/icons/png/512/108/108671.png';
}
$this->initImage($product, $url);

then the function:

	public function initImage($product, $url)
    {
        $idProduct = (int)$product->id;
        $image = new Image();
        $image->id_product = $idProduct;
        $image->position = Image::getHighestPosition($idProduct) + 1;
        $image->cover = true;
        if (($image->validateFields(false, true)) === true
            && ($image->validateFieldsLang(false, true)) === true && $image->add()
        ) {
            $image->associateTo(Context::getContext()->shop->id);
            if (!self::copyImg($idProduct, $image->id, $url, 'products', true)) {
                $image->delete();
            }
        }
    }

    public function copyImg(
        $idEntity,
        $idImage,
        $url,
        $entity = 'products',
        $regenerate = true
    ) {
        $tmpFile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
        $watermarkTypes = explode(',', Configuration::get('WATERMARK_TYPES'));


        switch ($entity) {
            case 'products':
                $imageObj = new Image($idImage);
                $path = $imageObj->getPathForCreation();
                break;
            case 'categories':
                $path = _PS_CAT_IMG_DIR_ . (int) $idEntity;
                break;
            case 'manufacturers':
                $path = _PS_MANU_IMG_DIR_ . (int) $idEntity;
                break;
            case 'suppliers':
                $path = _PS_SUPP_IMG_DIR_ . (int) $idEntity;
                break;
        }
        $url = str_replace(' ', '%20', trim($url));

        if (!ImageManager::checkImageMemoryLimit($url)) {
            return false;
        }
        
        if (Tools::copy($url, $tmpFile)) {//exception here
            ImageManager::resize($tmpFile, $path . '.jpg');
            $imagesTypes = ImageType::getImagesTypes($entity);


            if ($regenerate) {
                foreach ($imagesTypes as $imageType) {
                    ImageManager::resize(
                        $tmpFile,
                        $path.'-'.Tools::stripslashes($imageType['name']).'.jpg',
                        $imageType['width'],
                        $imageType['height']
                    );
                    if (in_array($imageType['id_image_type'], $watermarkTypes)) {
                        Hook::exec(
                            'actionWatermark',
                            array(
                                'id_image' => $idImage,
                                'id_product' => $idEntity
                            )
                        );
                    }
                }
            }
        }
        unlink($tmpFile);
        return true;
    }

but I have an exception:

Quote

file_get_contents_curl failed to download https://image.flaticon.com/icons/png/512/108/108671.png : (error code 35)

thank you in advance for your help

Edited by QuentinJou (see edit history)
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
×
×
  • Create New...