php - Prestashop 1.7:将产品图像保存在数据库中,但图像未正确显示在产品表中
问题描述
我想获取现有 Prestashop 1.6 网站的产品图像并将它们导入新的 Prestashop 1.7 网站。我现在不是在谈论产品变化/组合/偏角,而只是关于原始产品。
因此,给定以下变量,我编写了以下代码:
$ps16ProductObject
是 Prestashop 1.6 的产品,感谢 Prestashop 的 Web 服务,使用 Prestashop 客户端$prestashop17Product
(暂时没有出现在我的代码中)是 Prestashop 1.7 产品,从 Prestashop 1.6 导入,在 Prestashop 1.7 中确实存在(即:它实际上是创建并保存在数据库中)。我想将$ps16ProductObject
的图像导入这个 Prestashop 1.7 产品。是Prestashop 1.6 产品中 hydrad ( )$prestashop17Product
类的一个实例。class/Product.php
->hydrate()
$ps16ProductObject
if(array_key_exists('images', $ps16ProductObject['product']['associations'])) { foreach($ps16ProductObject['product']['associations']['images'] as $imagePs16Id) { $image = new \Image(); $image->id_product = $ps16ProductObject['product']['id']; $image->position = \Image::getHighestPosition($ps16ProductObject['product']['id']) + 1; $image->add(); $new_path = $image->getPathForCreation(); $tmpfile = dirname( __FILE__ ) . '/../temp.jpeg'; $ps16image = $this->ps16Client->get([ 'resource' => 'images/products/' . $ps16ProductObject['product']['id'] . '/' . $imagePs16Id['id'] ]); file_put_contents($tmpfile, $ps16image); \ImageManager::resize($tmpfile, $new_path . '.jpeg'); unlink($tmpfile); } }
(受代码的启发/controllers/admin/AdminProductsController.php
)
图像已正确保存到文件系统中,并出现在 DB 表image
、、、。image_lang
nch_image_shop
问题是:当我转到 PS17 网站的 BO 中的产品表时,它“显示”了图像但它们是空的,好像无法加载一样。“空”产品图像具有此路径:/img/p/3/5/9/359-home_default.jpg
并且没有任何 404 错误。我已经将此路径与手动创建的产品表的图像路径进行了比较,我的结论是该路径是完全正确的。
请问缺少什么以及如何更正?
重要编辑
当我尝试打开时实际上有一个 404 错误/img/p/XXXX/YYYY/ZZZZ/XXXYYYYZZZ-home_default.jpg
。
解决方案
以下代码有效。使脚本工作的重要新部分是:
foreach ($imagesTypes as $imageType) {
\ImageManager::resize($tmpfile, $new_path . '-' . stripslashes($imageType['name']) . '.' . 'jpg', $imageType['width'], $imageType['height'], 'jpg');
if ($generate_hight_dpi_images) {
\ImageManager::resize($tmpfile, $new_path . '-' . stripslashes($imageType['name']) . '2x.' . 'jpg', (int) $imageType['width'] * 2, (int) $imageType['height'] * 2, 'jpg');
}
}
完整代码是:
if(array_key_exists('images', $ps16ProductObject['product']['associations'])) {
foreach($ps16ProductObject['product']['associations']['images'] as $imagePs16Id) {
$image = new \Image();
$image->id_product = (int) $ps16ProductObject['product']['id'];
$image->position = \Image::getHighestPosition($ps16ProductObject['product']['id']) + 1;
if (!\Image::getCover($image->id_product)) {
$image->cover = 1;
} else {
$image->cover = 0;
}
$image->add();
$new_path = $image->getPathForCreation();
$tmpfile = dirname( __FILE__ ) . '/../temp.jpg';
$ps16image = $this->ps16Client->get([
'resource' => 'images/products/' . $ps16ProductObject['product']['id'] . '/' . $imagePs16Id['id']
]);
file_put_contents($tmpfile, $ps16image);
\ImageManager::resize($tmpfile, $new_path . '.' . 'jpg', null, null, 'jpg', false);
$imagesTypes = \ImageType::getImagesTypes('products');
$generate_hight_dpi_images = (bool) \Configuration::get('PS_HIGHT_DPI');
foreach ($imagesTypes as $imageType) {
\ImageManager::resize($tmpfile, $new_path . '-' . stripslashes($imageType['name']) . '.' . 'jpg', $imageType['width'], $imageType['height'], 'jpg');
if ($generate_hight_dpi_images) {
\ImageManager::resize($tmpfile, $new_path . '-' . stripslashes($imageType['name']) . '2x.' . 'jpg', (int) $imageType['width'] * 2, (int) $imageType['height'] * 2, 'jpg');
}
}
unlink($tmpfile);
}
}
推荐阅读
- go - 无法在 vscode 中构建用于调试的 go 包
- security - 网站/服务器所有者不能在散列之前读出密码吗?
- pine-script - 止损和止盈的策略不计算止损
- reactjs - 在 Next.js 中使用 FileViewer 时出现运行时错误
- linux - GitLab CI/CD:这项工作没有痕迹。& 没有工作日志
- c++ - 有没有办法从 C++ 中的函数返回自定义结构?
- javascript - 币安链钱包扩展问题
- vaadin - Vaadin Flow:如何将类名添加到模板
- python - 在 python 中显示来自 mysql 的数据时,时间和日期列看起来几乎不可读。有没有办法以日期和时间格式显示它?
- twitter - Tweepy 机器人正在回复所有转发或回复特定用户推文的人