Jump to content

Edit History

SIWA_Fabian

SIWA_Fabian

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

SIWA_Fabian

SIWA_Fabian

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));       Auskommentiert für Import aus Filesystem


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

×
×
  • Create New...