尝试在 prestashop 1.7.3.0 中使用 api 向产品添加封面图片
trying to add a cover image to a product using api in prestashop 1.7.3.0
我安装了 prestashop 1.7.3.0,我正在尝试为产品添加封面图片。
这是我用 addProductImage 函数创建的 class
<?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
我错过了什么?
更新
模板中显示产品图片的代码:
<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}"
>
这是使用图像 class 为产品创建封面图像的产品 class:
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);
...
}
}
更新 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
好像是假的url图片缺少图片id
如果我将 http://prestashop.ufk:8080/7410-small_default/.jpg
更改为 http://prestashop.ufk:8080/7410-small_default/1.jpg
,我将在数据库中看到第一张图片。
好的..在这次更新中我基本上弄清楚了发生了什么..
url 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//.jpg [L]
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p///.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p////.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/////.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p//////.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///////.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////////.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/////////.jpg [L]
RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/.jpg [L]
RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/.jpg [L]
我需要将所有 .+\.jpg$
更改为 .*\.jpg$
以便重写对我的图像起作用。
在urlhttp://prestashop.ufk:8080/7410-home_default/.jpg
中,7410是图片id,不知道.jpg
.
前应该忽略哪些数据
现在..我猜我错过了什么,这就是为什么我需要改变
在 .htaccess 中重写规则。有什么想法吗?
您正在使用的数组在此处生成:
/Adapter/Image/ImageRetriever.php -> public function getImage($object, $id_image)
生成 URL 的函数在这里:
/classes/Link.php -> public function getImageLink($name, $ids, $type = null)
我想你现在会发现问题所在了:)
我安装了 prestashop 1.7.3.0,我正在尝试为产品添加封面图片。
这是我用 addProductImage 函数创建的 class
<?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
我错过了什么?
更新
模板中显示产品图片的代码:
<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}"
>
这是使用图像 class 为产品创建封面图像的产品 class:
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);
...
}
}
更新 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
好像是假的url图片缺少图片id
如果我将 http://prestashop.ufk:8080/7410-small_default/.jpg
更改为 http://prestashop.ufk:8080/7410-small_default/1.jpg
,我将在数据库中看到第一张图片。
好的..在这次更新中我基本上弄清楚了发生了什么..
url 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//.jpg [L]
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p///.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p////.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/////.jpg [L]
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p//////.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///////.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////////.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/////////.jpg [L]
RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/.jpg [L]
RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/.jpg [L]
我需要将所有 .+\.jpg$
更改为 .*\.jpg$
以便重写对我的图像起作用。
在urlhttp://prestashop.ufk:8080/7410-home_default/.jpg
中,7410是图片id,不知道.jpg
.
现在..我猜我错过了什么,这就是为什么我需要改变 在 .htaccess 中重写规则。有什么想法吗?
您正在使用的数组在此处生成:
/Adapter/Image/ImageRetriever.php -> public function getImage($object, $id_image)
生成 URL 的函数在这里:
/classes/Link.php -> public function getImageLink($name, $ids, $type = null)
我想你现在会发现问题所在了:)