Jump to content

Add image to product programmatically --> crash


Recommended Posts

$url = "http://photos.hotelbeds.com/giata/00/001075/001075a_hb_r_001.jpg";

$shops = Shop::getShops(true, null, true);

$image = new Image();
$image->id_product = $product->id;
$image->position = 1;
$image->cover = true; // or false;
$image->associateTo($shops);


Logger::addLog(date('Y-m-d H:i:s') . ' - Adding Image : ' . $url ,1);

if (($image->validateFields(false, true)) === true &&
($image->validateFieldsLang(false, true)) === true && $image->add())
{
	if ($this->copyImg($product->id, $image->id, $url, "products", false))
	{
		Logger::addLog(date('Y-m-d H:i:s') . ' - AdminImportController : ' . $url ,1);
	}
	else
	{
		Logger::addLog(date('Y-m-d H:i:s') . ' - AdminImportController Failed : ' . $url ,1);
		$image->delete();
	}
}

Hello,

I need your help, I created a module that uses an external API wich provide produts.

I am using the following method to trigger my sync:
public function hookActionCronJob ()
and I created a cron.php to trigger the hook
(https://www.h-hennes.fr/blog/2017/12/01/prestashop-gestion-des-taches-cron/)

My sync is working well and I am able to create the products.
Lately I tried to add the images and it doesn't work.

The method: $ this-> copyImg ($ product-> id, $ image-> id, $ url, "products", false) seems to crash and my sync stops there.

Being in the module, I don't know how to report an error.
I have a global try / catch but nothing goes back in the logs, we said that the sync hangs on it and remains blocked.

I broke everything down a bit in the lower layers by outputting the methods for "debugging" them and I suspect this method does not work: imagecreatefromjpeg ($ filename);

Could you help me by telling me how I can debug or where by telling me where the problem could come from, I'm a little lost ...

Thanks in advance,

Regards

 

hh_cronuser.php - Visual Studio Code.jpg

Link to comment
Share on other sites

 $image->id_product = $product->id;
    $image->position = Image::getHighestPosition($product->id) + 1;
    $image->cover = true; // or false;

    
    if (($image->validateFields(false, true)) === true &&
        ($image->validateFieldsLang(false, true)) === true && 
            $image->add()
            ) {
            $image->associateTo($shops);
            if (!copyImg($product->id, $image->id, $url_imagem, 'products', true)) {
                $image->delete();
            }
    }

Link to comment
Share on other sites

Hello,

Thank you for your reply.

I have not posted the correct version of the above code.
Here it is in the screenshot. In fact, the method that causes me problem is the following:

AdminImportController :: copyImg

I have organized my code as in the example provided and I have the same result.

The last line in the logs is "Image Added" and then nothing ...

Logs - Google Chrome.jpg

hh_cronuser.php - Visual Studio Code_2.jpg

Link to comment
Share on other sites

UPDATE

If I try this code https://www.lije-creative.com/prestashop-creer-produit-php/ with this picture for all product : -->http://photos.hotelbeds.com/giata/00/001075/001075a_hb_r_001.jpg<--

It works !

Same code with this picture : -->https://lmbl.lu/mani/img/products/10036.jpg<-- doesn't work ...

But if I try to add this picture to the product with the web interface, it works ...

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