Jump to content

How to add image to a product from url


Recommended Posts

Hello,

I'm actually creating a module syncing prestashop and my own app. I've already WebService in my app that give a list of products, category, ...

My product json looks like this :

[
        {
            "remote_id": "1",
            "label": "test",
            "remote_id_categorie": 2,
            "price": "0.00000000",
            "stock": 0,
            "code_barre": "",
            "reference": "test",
            "tva": "0.000",
            "url_image": "https://test.titam/viewimage.php?modulepart=product&entity=1&file=%2Ftest%2Fsss.jpeg",
            "fac_qte": "0"
        },
        {
            "remote_id": "33",
            "label": "BISCUIT PCS",
            "remote_id_categorie": "33",
            "price": "1320.00000000",
            "stock": 0,
            "code_barre": "",
            "reference": "BISCUIT",
            "tva": "20.000",
            "url_image": "https://test.titam/viewimage.php?modulepart=product&entity=1&file=%2FBISCUIT%2Findex.jpeg",
            "fac_qte": "0"
        },
        {
            "remote_id": "36",
            "label": "CABLE COAXIAL 75OHM AVEC ALIMENTATION",
            "remote_id_categorie": 0,
            "price": "30000.00000000",
            "stock": 0,
            "code_barre": "",
            "reference": "COAX-ALIM",
            "tva": "20.000",
            "url_image": "",
            "fac_qte": "0"
        }
]

The thing that i don't know is how to add image to my product from my modules.

Note : All product and category is already sync and have the same id in prestashop and in my app.

 

Link to comment
Share on other sites

12 hours ago, Apar said:

When you try to upload a product image from import function from prestashop using CSV or Excel. It uses Image URL to upload it you could try using it's code as a reference. 

Thank you, it works. But image is not showed in the site.

frontsary.thumb.png.bef35560a1540b72e7febeda496c0b4e.png

In the backend all image are there.

1170961842_misysary.thumb.png.83c3c9e196c84c9b5665ff3334e19191.png

There is my code :

<?php

$image = new \Image(null);
$image->id_product = $object->id;
$image->position = \Image::getHighestPosition($object->id) + 1;
$image->cover = true;
$image->add();
if (! File::copyImg($object->id, $image->id, $object->image, 'products', true)) {
  $image->delete();
}

// The copyImg Function
public static function copyImg($id_entity, $id_image, $url, $entity = 'products', $regenerate = true)
    {


        $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
        $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;
            case 'manufacturers':
                $path = _PS_MANU_IMG_DIR_ . (int)$id_entity;
                break;
            case 'suppliers':
                $path = _PS_SUPP_IMG_DIR_ . (int)$id_entity;
                break;
            case 'stores':
                $path = _PS_STORE_IMG_DIR_ . (int)$id_entity;
                break;
        }
		
  		//FIX FOR SSL ISSUE ON LOCAL
        stream_context_set_default(array(
            'ssl' => array(
                'peer_name' => 'generic-server',
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        ));

        $response = file_get_contents($url, false);

        if (file_put_contents($tmpfile, $response)) {
            if (!\ImageManager::checkImageMemoryLimit($tmpfile)) {
                @unlink($tmpfile);
                return false;
            }

            $tgt_width = $tgt_height = 0;
            $src_width = $src_height = 0;
            $error = 0;
            \ImageManager::resize($tmpfile, $path . '.jpg', null, null, 'jpg', false, $error, $tgt_width, $tgt_height, 5, $src_width, $src_height);
            $images_types = \ImageType::getImagesTypes($entity, true);

            if ($regenerate) {
                $previous_path = null;
                $path_infos = array();
                $path_infos[] = array($tgt_width, $tgt_height, $path . '.jpg');
                foreach ($images_types as $image_type) {
                    $tmpfile = self::get_best_path($image_type['width'], $image_type['height'], $path_infos);

                    if (\ImageManager::resize(
                        $tmpfile,
                        $path . '-' . stripslashes($image_type['name']) . '.jpg',
                        $image_type['width'],
                        $image_type['height'],
                        'jpg',
                        false,
                        $error,
                        $tgt_width,
                        $tgt_height,
                        5,
                        $src_width,
                        $src_height
                    )) {
                        
                        if ($tgt_width <= $src_width && $tgt_height <= $src_height) {
                            $path_infos[] = array($tgt_width, $tgt_height, $path . '-' . stripslashes($image_type['name']) . '.jpg');
                        }
                        if ($entity == 'products') {
                            if (is_file(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int)$id_entity . '.jpg')) {
                                unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int)$id_entity . '.jpg');
                            }
                            if (is_file(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int)$id_entity . '_' . (int)\Context::getContext()->shop->id . '.jpg')) {
                                unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int)$id_entity . '_' . (int)\Context::getContext()->shop->id . '.jpg');
                            }
                        }
                    }
                    \Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
                }
            }
        } else {
            @unlink($tmpfile);
            return false;
        }
        return true;
    }

 

Link to comment
Share on other sites

  • 2 weeks later...

Did your issue got solved. If not check if there is database entry for the image you have uploaded using your code. Find the image ID then check if there a image file available with the same Id in img/p folder of your prestashop root folder and its different image types are generated or not.

Link to comment
Share on other sites

13 hours ago, Apar said:

Did your issue got solved. If not check if there is database entry for the image you have uploaded using your code. Find the image ID then check if there a image file available with the same Id in img/p folder of your prestashop root folder and its different image types are generated or not.

Yes. Image are in database and in img/p foldersell.thumb.png.02e46a344413b797d7ad2c2c3af3f365.png

Link to comment
Share on other sites

Check for image URL in the back office product page. Check for zoomed image URL this should be the full url of your original image(This should work if you have image file in place). If this is working then we have some problem with either problem with image database entry or we have some routing issues for the image. (Do remember to turn off browser cache)

screenshot.jpg

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...