如何在prestashop中将图像添加到产品中

如何在prestashop中将图像添加到产品中,prestashop,Prestashop,我在代码中添加了一个实用的产品,该产品被正确地添加到presta中,但与它的形象无关 以下是我使用的部分代码: $url= "localhost\prestashop\admin7988\Hydrangeas.jpg" ; $id_productt = $object->id; $shops = Shop::getShops(true, null, true);

我在代码中添加了一个实用的产品,该产品被正确地添加到presta中,但与它的形象无关

以下是我使用的部分代码:

                    $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功能:

         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;
}
有人能帮我吗

您有两个问题:

  • 您正在将第5个参数(带值)传递给copyImg,而函数没有这样的参数
  • 您的foreach($images\u type作为$images\u type)循环也必须包括挂钩(添加开/关花括号)


  • 您还应该检查产品是否正确导入,尤其是“链接重写”以及图像是否以物理方式上传到/img/文件夹中。

    请注意,以下是产品获得的损坏url:tnx alot亲爱的PrestaShop Developer.c回复我是个傻瓜!我解决了我的问题,错误的事情是一个愚蠢的错误,让我困惑了一个星期!这是在URL中,我改为“http://localhost/prestashop/admin7988/hydangeas.jpg”,我的问题得到了解决。乐意效劳。你对密码的正确回答
    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)); 
    }