尝试在prestashop 1.7.3.0中使用api向产品添加封面图像

问题描述 投票:0回答:1

我安装了prestashop 1.7.3.0,我正在尝试为产品添加封面图像。

这是我使用addProductImage函数创建的类

<?php

require_once _PS_ADMIN_CONTROLLER_DIR_ . 'AdminImportController.php';

class TuxInImage extends AdminImportControllerCore
{

    /**
     * @param $productId
     * @param $imgUrl
     * @param bool $regenerate
     * @throws PrestaShopDatabaseException
     * @throws PrestaShopException
     */
    public static function addProductImage($productId, $imgUrl, $isCover=false,$regenerate = true)
    {
        $img = new Image();
        $img->id_product = $productId;
        $img->cover=$isCover;
        $img->add();
        $imageId = $img->id;

        self::copyImg($productId, $imageId, $imgUrl, 'products', $regenerate);
    }
}

当我执行它时,它会添加封面图像。当我在管理面板上浏览产品时..我可以正确地看到图像。

我注意到,当我在浏览器中打开网站时,它无法加载图像,似乎他们缺少某种文件名。它正在寻找没有名字的.jpg

例如:http://MY_SITE_URL/2510-home_default/.jpg

我错过了什么?

update

模板中显示产品图像的代码:

 <img
            src = "{$product.cover.bySize.home_default.url}"
            alt = "{if !empty($product.cover.legend)}{$product.cover.legend}{else}{$product.name|truncate:30:'...'}{/if}"
            data-full-size-image-url = "{$product.cover.large.url}"
          >

这是使用图像类为产品创建封面图像的产品类:

require_once __DIR__.DIRECTORY_SEPARATOR.'TuxInImage.php';

class TuxInProduct {

    private $heLangId;
    private $enLangId;

    function __construct($heLangId,$enLangId)
    {
        $this->heLangId=$heLangId;
        $this->enLangId=$enLangId;
    }

    function addProduct($categoryId, $carListNTypeId, $productHebrewName, $productEnglishName, $manufacturerNameHe, $manufacturerNameEn, $price,$imgPath) {
        $productObj = new Product();
        $productObj->id_category_default=$categoryId;
        $productObj->price=$price;
        $productObj->name=[$this->heLangId=>$productHebrewName,$this->enLangId=>$productEnglishName];
        $productObj->manufacturer_name=[$this->heLangId=>$manufacturerNameHe,$this->enLangId=>$manufacturerNameEn];
        $productObj->quantity=0;
        $productObj->add();

        $productObj->setWsCategories([$categoryId]);
        $productId = $productObj->id;
        TuxInImage::addProductImage($productId,__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'wind.jpg',true);
        ...
    }

}

update 2

var_dump$product.cover输出:

array (size=9)
  'bySize' => 
    array (size=5)
      'small_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-small_default/.jpg' (length=50)
          'width' => int 98
          'height' => int 98
      'cart_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-cart_default/.jpg' (length=49)
          'width' => int 125
          'height' => int 125
      'home_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-home_default/.jpg' (length=49)
          'width' => int 250
          'height' => int 250
      'medium_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-medium_default/.jpg' (length=51)
          'width' => int 452
          'height' => int 452
      'large_default' => 
        array (size=3)
          'url' => string 'http://prestashop.ufk:8080/7410-large_default/.jpg' (length=50)
          'width' => int 800
          'height' => int 800
  'small' => 
    array (size=3)
      'url' => string 'http://prestashop.ufk:8080/7410-small_default/.jpg' (length=50)
      'width' => int 98
      'height' => int 98
  'medium' => 
    array (size=3)
      'url' => string 'http://prestashop.ufk:8080/7410-home_default/.jpg' (length=49)
      'width' => int 250
      'height' => int 250
  'large' => 
    array (size=3)
      'url' => string 'http://prestashop.ufk:8080/7410-large_default/.jpg' (length=50)
      'width' => int 800
      'height' => int 800
  'legend' => string '' (length=0)
  'cover' => string '1' (length=1)
  'id_image' => string '7410' (length=4)
  'position' => string '1' (length=1)
  'associatedVariants' => 
    array (size=0)
      empty

似乎图像的错误网址缺少图像ID。

如果我将http://prestashop.ufk:8080/7410-small_default/.jpg更改为http://prestashop.ufk:8080/7410-small_default/1.jpg,我会看到db中的第一个图像。

ok.. in this update I figured out mostly what's going on..

网址http://prestashop.ufk:8080/7410-small_default/1.jpg是假的。因为在.jpg之前添加的数字并不重要。它忽略了它。它只需要至少一个角色。

在以下重写规则中:

# Images
RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L]
RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]
RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]

我需要将所有.+\.jpg$更改为.*\.jpg$,以便重写以处理我的图像。

在网址http://prestashop.ufk:8080/7410-home_default/.jpg中,7410是图片ID,所以我不知道在.jpg之前应该忽略哪些数据。

现在..我猜我错过了一些东西,这就是为什么我需要改变.htaccess中的重写规则。任何想法是什么?

php prestashop prestashop-1.7
1个回答
1
投票

您正在使用的数组在此处生成:

/Adapter/Image/ImageRetriever.php -> public function getImage($object, $id_image)

生成URL的功能如下:

/classes/Link.php -> public function getImageLink($name, $ids, $type = null)

我想你现在会发现什么是错的:)

© www.soinside.com 2019 - 2024. All rights reserved.