如何在 Prestashop 中向产品添加图像

how to add an image to product in prestashop

我在我的代码中添加了一个实用的产品,该产品已正确添加到法式面包中,但与它的图像无关。

这是我使用的部分代码:

                    $url=  "localhost\prestashop\admin7988\Hydrangeas.jpg" ;
                $id_productt = $object->id;         
                $shops = Shop::getShops(true, null, true);    
                $image = new Image();
                $image->id_product = $id_productt ;
                $image->position = Image::getHighestPosition($id_productt) + 1 ;
                $image->cover = true; // or false;echo($godyes[$dt][0]['image']);
                if (($image->validateFields(false, true)) === true &&
                    ($image->validateFieldsLang(false, true)) === true && $image->add())
                    {
                        $image->associateTo($shops);
                        if (! self::copyImg($id_productt, $image->id, $url, 'products', false))
                            {
                            $image->delete();
                        }
                    }

但是我的产品还没有任何图片

问题出在 copyImg 方法中...

这是我的 copyImg 函数:

         function copyImg($id_entity, $id_image = null, $url, $entity = 'products')
{
    $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;
    }
    $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 make the import slower.
    // Just hide the warning, the traitment will be the same.
    if (@copy($url, $tmpfile))
    {
        ImageManager::resize($tmpfile, $path.'.jpg');
        $images_types = ImageType::getImagesTypes($entity);
        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 个问题:

  1. 您正在将第 5 个参数(带值)传递给 copyImg,而该函数没有这样的参数。
  2. 您的 foreach ($images_types as $image_type) 循环也必须包含 Hook(添加 open/close 大括号)。

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

您还应该检查产品是否正确导入,特别是 "link_rewrite" 以及图像是否物理上传到 /img/ 文件夹中。