Php Prestashop 1.7:以DB保存产品图像,但图像未正确显示在产品表中
我想获取现有Prestashop 1.6网站的产品图像,并将其导入新的Prestashop 1.7网站。我现在说的不是产品的变化/组合/衰退,而是原始产品 因此,给定以下变量,我编写了以下代码:Php Prestashop 1.7:以DB保存产品图像,但图像未正确显示在产品表中,php,prestashop,prestashop-1.6,prestashop-1.7,Php,Prestashop,Prestashop 1.6,Prestashop 1.7,我想获取现有Prestashop 1.6网站的产品图像,并将其导入新的Prestashop 1.7网站。我现在说的不是产品的变化/组合/衰退,而是原始产品 因此,给定以下变量,我编写了以下代码: $ps16ProductObject是来自Prestashop 1.6的产品,使用Prestashop客户端通过Prestashop的Web服务获取 $prestashop17Product(目前我的代码中没有显示)是从Prestashop1.6导入的Prestashop1.7产品,它确实存在于Pre
是来自Prestashop 1.6的产品,使用Prestashop客户端通过Prestashop的Web服务获取$ps16ProductObject
(目前我的代码中没有显示)是从Prestashop1.6导入的Prestashop1.7产品,它确实存在于Prestashop1.7中(即:它实际上是在DB中创建和保存的)。我想将$prestashop17Product
的图像导入此Prestashop 1.7产品$ps16ProductObject
是类$prestashop17Product
的一个实例,使用prestashop1.6产品class/Product.php
水合($ps16ProductObject
)->hydrome()
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的代码启发)
图像已正确保存到文件系统中,并显示在数据库表image
,image\u lang
,nch\u image\u shop
中
问题是:当我在PS17网站的BO中查看产品表时,它“显示”了图像,但它们是空的,好像无法加载一样。“空”产品映像具有以下路径:/img/p/3/5/9/359-home\u default.jpg
,并且没有任何404错误。我将此路径与手动创建的产品表图像的路径进行了比较,我的结论是此路径完全正确
遗漏了什么以及如何纠正
重要编辑
当我尝试打开
/img/p/XXXX/yyy/ZZZZ/xxxyyyyyzzz-home\u default时,实际上出现了404错误。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);
}
}
下面的代码可以工作。使脚本正常工作的重要新部分是:
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);
}
}