Jump to content

copyImg() "not" copying Images correctly


SIWA_Fabian

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

 

Link to comment
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.

Link to comment
Share on other sites

  • 2 months later...
  • 2 years later...
On 4/22/2021 at 12:35 PM, knacky said:

obrazek.thumb.png.de6bc0dfcf26e1393690a637274ae27e.png

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

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