SIWA_Fabian Posted September 9, 2020 Share Posted September 9, 2020 (edited) Hey I'm just writing a CSV importer for Products and more. This is necessary to support some modules we are using. To get Images to my Products i use the copyImg-Method: 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; } //$url = str_replace(' ', '%20', trim($url)); Removed since pictures are coming from the same filesystem and replacing the " " causes problems. // Evaluate the memory required to resize the image: if it's too much, you can't resize it. if (!ImageManager::checkImageMemoryLimit($url)) return false; // 'file_exists' doesn't work on distant file, and getimagesize makes the import slower. // Just hide the warning, the processing will be the same. if (Tools::copy($url, $tmpfile)) { ImageManager::resize($tmpfile, $path . '.jpg'); $images_types = ImageType::getImagesTypes($entity); if ($regenerate) 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)); } } else { unlink($tmpfile); return false; } unlink($tmpfile); return true; } Somehow the Product-Pictures get saved into the "wrong" folder. What do I mean by this? In the Backend Presta looks in following folder for the images: But the Images aren't save there, the same goes for all the others. If I add the pictures myself it works perfectly fine. This is how i call the copyImg-Method: $image = new Image(); $image->id_product = $product->id; $image->position = Image::getHighestPosition($product->id) + 1; $image->cover = true; $image->legend = ''; $image->add(); $line[33] = str_replace(".png", ".jpg", $line[33]); //$line[33] is the points to the array-element which contains the Name of the image. $imagePath = _PS_UPLOAD_DIR_.'importPictures/'.$line[33]; if(!copyImg($product->id, $image->id, $imagePath, 'products', false)){ $image->delete(); }; Did someone already run into this problem? Does someone know what I did wrong? Thanks for your help! Best regards Fabian Edited September 9, 2020 by SIWA_Fabian (see edit history) Link to comment Share on other sites More sharing options...
Guest Posted September 10, 2020 Share Posted September 10, 2020 Change false to true in function copyImg. true = generate miniatures false = not generate miniatures if(!copyImg($product->id, $image->id, $imagePath, 'products', true)){ $image->delete(); }; Link to comment Share on other sites More sharing options...
SIWA_Fabian Posted September 10, 2020 Author Share Posted September 10, 2020 Thanks a lot, it works just fine now. Link to comment Share on other sites More sharing options...
conexionred Posted April 14, 2021 Share Posted April 14, 2021 I use the same method from a script and it works, but the thumbnails is not working. When search for the new product you can see this. But if you modify the product you can see the thumb. And if you save the product withouth modify, then works and also in front. I use true in $regenerate, and my code is the same. Link to comment Share on other sites More sharing options...
SIWA_Fabian Posted April 22, 2021 Author Share Posted April 22, 2021 As soon as you save it works in the preview as well? Link to comment Share on other sites More sharing options...
knacky Posted April 22, 2021 Share Posted April 22, 2021 You must also create a thumbnail in the TMP folder! ImageManager::resize($tmpfile, _PS_TMP_IMG_DIR_ . '/product_mini_' . $image->id . '.jpg', 80, 45); 1 Link to comment Share on other sites More sharing options...
conexionred Posted April 27, 2021 Share Posted April 27, 2021 It works perfect! Thanks a lot... But in the front it still not working. If I go to product and press save without changes, then the picture show on product list and product detail Product list, (Category page). Product details Link to comment Share on other sites More sharing options...
knacky Posted April 27, 2021 Share Posted April 27, 2021 This was just additional code to the previous post. Link to comment Share on other sites More sharing options...
conexionred Posted April 28, 2021 Share Posted April 28, 2021 (edited) Ok, I search for information and the Friendly URLS was the problem. I try to find the part of the code where the awful URLs updated from the folder structure (\img\p\3\8\4\7) to the friendly URLs. ¿Any ideas? Edited April 28, 2021 by conexionred (see edit history) Link to comment Share on other sites More sharing options...
conexionred Posted July 23, 2021 Share Posted July 23, 2021 (edited) Ok, I Fixed it, my problem was that in my module, the Product->link_rewrite was bad defined. Thanks for all! Edited July 23, 2021 by conexionred (see edit history) Link to comment Share on other sites More sharing options...
dandumit Posted October 29, 2023 Share Posted October 29, 2023 On 4/22/2021 at 12:35 PM, knacky said: You must also create a thumbnail in the TMP folder! ImageManager::resize($tmpfile, _PS_TMP_IMG_DIR_ . '/product_mini_' . $image->id . '.jpg', 80, 45); @knacky - I have same problem in 1.7 . in tmp folder have to be all images miniatures or just for cover ? do not appear for me in product list page Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now