kangoo13 Posted July 18, 2013 Share Posted July 18, 2013 Hello everybody, I try to add an image to a product which already exists in the database and I would like to know to add an image to the product ? I saw that there were already class Image and ImageManager but I don't know how to use them.. I already have for every product id_product , now I just have to download the image for X product and put the image to the product.. If you can help me.. thank you! Link to comment Share on other sites More sharing options...
chapster Posted April 18, 2014 Share Posted April 18, 2014 Hi, I have same problem. I have to create my own specific product import (products and categories are done). Only thing is to add images from external URL to my products. In one thread a found something like: $i = new ImageCore(); $i->data = file_get_contents($url); $i->save(); But ImageCore has no data attribute :-( Any suggestion? Thanks. Chap Link to comment Share on other sites More sharing options...
chapster Posted April 19, 2014 Share Posted April 19, 2014 I extracted some code from import and works ... maybe it will help someone: $image = new Image(); $image->id_product = (int) $product->id; $image->position = Image::getHighestPosition($product->id) + 1; $image->cover = true; $image->add(); if (!copyImg($product->id, $image->id, $data["IMGURL"], 'products', !Tools::getValue('regenerate'))) { $image->delete(); } 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)); // 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; } 2 Link to comment Share on other sites More sharing options...
dalekos Posted December 18, 2014 Share Posted December 18, 2014 Nice one chapster! Thanks! Link to comment Share on other sites More sharing options...
GELINGERMEDIA Posted May 7, 2019 Share Posted May 7, 2019 Use this one. It's Better ( on 1.6 at least ) Image::deleteCover((int)Tools::getValue('id_product')); $img = new Image((int)Tools::getValue('id_image')); $img->cover = 1; @unlink(_PS_TMP_IMG_DIR_ . 'product_' . (int)$img->id_product . '.jpg'); @unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int)$img->id_product . '_' . $this->context->shop->id . '.jpg'); if ($img->update()) { echo json_encode(array("result" => "OK", "message" => "<div class='alert-bootbox-modal'><div class='row'><div class='col-md-4'></div><div class='col-md-4'><img class='img-responsive' src='/modules/bambini_newimportproducts/imgs/happy_cat" . rand(1, 5) . ".jpg'></div><div class='col-md-4'></div></div> <div class='row'><div class='col-md-4'></div><div class='col-md-4'><h3>We have made it cover image</h3></div><div class='col-md-4'></div></div> </div>")); die(); } else { echo json_encode(array("result" => "NOK", "message" => "<div class='alert-bootbox-modal'><div class='row'><div class='col-md-4'></div><div class='col-md-4'><img class='img-responsive' src='/modules/bambini_newimportproducts/imgs/sad_cat" . rand(1, 5) . ".jpg'></div><div class='col-md-4'></div></div> <div class='row'><div class='col-md-4'></div><div class='col-md-4'><h3>We had problem with making it cover image</h3></div><div class='col-md-4'></div></div> </div>")); die(); } Link to comment Share on other sites More sharing options...
haunter Posted August 24, 2019 Share Posted August 24, 2019 On 5/7/2019 at 5:32 PM, GELINGERMEDIA said: Use this one. It's Better ( on 1.6 at least ) Image::deleteCover((int)Tools::getValue('id_product')); $img = new Image((int)Tools::getValue('id_image')); $img->cover = 1; @unlink(_PS_TMP_IMG_DIR_ . 'product_' . (int)$img->id_product . '.jpg'); @unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int)$img->id_product . '_' . $this->context->shop->id . '.jpg'); if ($img->update()) { echo json_encode(array("result" => "OK", "message" => "<div class='alert-bootbox-modal'><div class='row'><div class='col-md-4'></div><div class='col-md-4'><img class='img-responsive' src='/modules/bambini_newimportproducts/imgs/happy_cat" . rand(1, 5) . ".jpg'></div><div class='col-md-4'></div></div> <div class='row'><div class='col-md-4'></div><div class='col-md-4'><h3>We have made it cover image</h3></div><div class='col-md-4'></div></div> </div>")); die(); } else { echo json_encode(array("result" => "NOK", "message" => "<div class='alert-bootbox-modal'><div class='row'><div class='col-md-4'></div><div class='col-md-4'><img class='img-responsive' src='/modules/bambini_newimportproducts/imgs/sad_cat" . rand(1, 5) . ".jpg'></div><div class='col-md-4'></div></div> <div class='row'><div class='col-md-4'></div><div class='col-md-4'><h3>We had problem with making it cover image</h3></div><div class='col-md-4'></div></div> </div>")); die(); } But this is not solving copying image from URL. 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