首页 > 解决方案 > Prestashop 1.7:将产品图像保存在数据库中,但图像未正确显示在产品表中

问题描述

我想获取现有 Prestashop 1.6 网站的产品图像并将它们导入新的 Prestashop 1.7 网站。我现在不是在谈论产品变化/组合/偏角,而只是关于原始产品。

因此,给定以下变量,我编写了以下代码:

(受代码的启发/controllers/admin/AdminProductsController.php

图像已正确保存到文件系统中,并出现在 DB 表image、、、。image_langnch_image_shop

问题是:当我转到 PS17 网站的 BO 中的产品表时,它“显示”了图像但它们是空的,好像无法加载一样。“空”产品图像具有此路径:/img/p/3/5/9/359-home_default.jpg并且没有任何 404 错误。我已经将此路径与手动创建的产品表的图像路径进行了比较,我的结论是该路径是完全正确的。

请问缺少什么以及如何更正?

重要编辑

当我尝试打开时实际上有一个 404 错误/img/p/XXXX/YYYY/ZZZZ/XXXYYYYZZZ-home_default.jpg

标签: phpprestashopprestashop-1.6prestashop-1.7

解决方案


以下代码有效。使脚本工作的重要新部分是:

            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);

        }
    }

推荐阅读