Jump to content

copyImg() "not" copying Images correctly


Recommended Posts


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) {
        case 'products':
            $image_obj = new Image($id_image);
            $path = $image_obj->getPathForCreation();
        case 'categories':
            $path = _PS_CAT_IMG_DIR_ . (int) $id_entity;
        case 'manufacturers':
            $path = _PS_MANU_IMG_DIR_ . (int) $id_entity;
        case 'suppliers':
            $path = _PS_SUPP_IMG_DIR_ . (int) $id_entity;
    //$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 {
        return false;
    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 = '';

$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)){


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


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.



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

  • 2 months later...
  • 2 years later...
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

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