Jump to content

copyImg() "not" copying Images correctly


SIWA_Fabian
 Share

Recommended Posts

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:

204287023_Screenshotfrom2020-09-0916-34-14.png.930340f4983cda15bc5730411da875d1.png

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 by SIWA_Fabian (see edit history)

Share this post


Link to post
Share on other sites

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();
};

 

Share this post


Link to post
Share on other sites

  • 7 months later...

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.

1194378226_Screenshot_2021-04-14ProductosMoly.thumb.png.bf812cfe35dda5f20694013ff647d9ed.png

 

But if you modify the product you can see the thumb.

403285401_Screenshot_2021-04-14ProductosEditarARNESPIELLUXURYCLAVOS60CMMoly.thumb.png.8982e2c1aac8721fa28fea57dd16444d.png

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.

Share this post


Link to post
Share on other sites

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

 

290374927_Capturadepantalla2021-04-27120417.thumb.jpg.0946ccb07f24dc8412a1ac78943c4dd5.jpg

 

Product details

1741849093_Capturadepantalla2021-04-27120502.thumb.jpg.775f8b4c8ceae78c1af217cc577bb97b.jpg

Share this post


Link to post
Share on other sites

  • 2 months later...

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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More