Jump to content

Edit History

QuentinJou

QuentinJou

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

QuentinJou

QuentinJou

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

QuentinJou

QuentinJou

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)) {
            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

×
×
  • Create New...