如何使用Prestashop使用PHP上传图像?

如何使用Prestashop使用PHP上传图像?,php,image,prestashop,prestashop-1.7,Php,Image,Prestashop,Prestashop 1.7,我正在尝试使用带有Prestashop PHP编码的SOAP API自动上传产品 产品似乎上传正确,但图像在frontoffice中呈现不正确(backoffice会)。我试过在网上找到的几段代码,但都不能满足我的需要 我有一个名为Jubaconfig.php的类,我在其中执行代码以导入产品。每个产品都是通过以下方式创建的: $product = new Product(); $product->name[1] = $productInfo->name

我正在尝试使用带有Prestashop PHP编码的SOAP API自动上传产品

产品似乎上传正确,但图像在frontoffice中呈现不正确(backoffice会)。我试过在网上找到的几段代码,但都不能满足我的需要

我有一个名为
Jubaconfig.php
的类,我在其中执行代码以导入产品。每个产品都是通过以下方式创建的:

$product = new Product();
                $product->name[1] = $productInfo->name;
                $product->reference = $productInfo->product_id;
                $product->description[1] = $productInfo->description;
                $product->description_short[1] = $productInfo->short_description;
                $product->active = 1;
                $product->condition = "new";
                $product->id_tax_rules_group = 1;
                $product->id_manufacturer = 3;
                $product->id_category_default = 49;
                $product->add();
                $product->save();

                $product->addToCategories(array(49));
                StockAvailable::setQuantity((int)$product->id, 0, $product->quantity);
                // Añadimos la imagen al producto

                $cover = true;
                $image_url = ($productInfo->additional_attributes[0]->value);

                var_dump($image_url);
                echo "<br>";

                $image = new Image();
                $image->id_product = $product->id;
                $image->position = Image::getHighestPosition($product->id) + 1;
                $image->cover = $cover;
                if (($image->validateFields(false, true)) === true &&
                    ($image->validateFieldsLang(false, true)) === true && $image->add())
                {
                    $image->associateTo($product->id_shop_default);
                    if (!Jubaconfig::copyImg($product->id, $image->id, $image_url, 'products', false))
                    {
                        $image->delete();
                    }
                }
当我进入后台的产品部分时,产品被正确上传,甚至图片也被正确上传。但是,在frontoffice中,产品仍在显示,但由于图像源不正确,因此未渲染图像


提示:如果我编辑并保存最近添加的产品,图像将开始显示在frontoffice中。但我需要的是显示我导入的所有图像,而不必逐个保存每个产品。

这是我在类似情况下使用的方法(示例是针对产品图像,但您明白了)


对我来说,使用现有的PS core控制器(从1.6+开始)似乎是最简单的方法。

您是否尝试在上传产品后重新生成图像?我的意思是去改进->设计->图像设置->重新生成缩略图,然后选择要重新生成的产品图像是的,但我仍然不工作。问题是,我在前台的图像显示一个问号,直到我在后台保存它们。我认为这与/img/p目录中路径的生成有关,但我仍然不知道如何在添加图像后尝试保存$product。将$product->save()置于脚本末尾。这可能仍然没有效果。问题是,这些图像在数据库中与产品正确关联,但frontoffice中的图片有一个问号,直到我在BackOfficeId中手动保存它们为止。您是否检查图像是否存在于img/p/path中,且使用的类型为?
public function copyImg($id_entity, $id_image = null, $url = '', $entity = 'products', $regenerate = true)
    {
        $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
        $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));

        switch ($entity) {
            default:
            case 'products':
                $image_obj = new Image($id_image);
                $path = $image_obj->getPathForCreation();
                break;
            case 'categories':
                $path = _PS_CAT_IMG_DIR_ . (int) $id_entity;
                break;
            case 'manufacturers':
                $path = _PS_MANU_IMG_DIR_ . (int) $id_entity;
                break;
            case 'suppliers':
                $path = _PS_SUPP_IMG_DIR_ . (int) $id_entity;
                break;
            case 'stores':
                $path = _PS_STORE_IMG_DIR_ . (int) $id_entity;
                break;
        }

        $url = urldecode(trim($url));
        $parced_url = parse_url($url);

        if (isset($parced_url['path'])) {
            $uri = ltrim($parced_url['path'], '/');
            $parts = explode('/', $uri);
            foreach ($parts as &$part) {
                $part = rawurlencode($part);
            }
            unset($part);
            $parced_url['path'] = '/' . implode('/', $parts);
        }

        if (isset($parced_url['query'])) {
            $query_parts = array();
            parse_str($parced_url['query'], $query_parts);
            $parced_url['query'] = http_build_query($query_parts);
        }

        if (!function_exists('http_build_url')) {
            require_once _PS_TOOL_DIR_ . 'http_build_url/http_build_url.php';
        }

        $url = http_build_url('', $parced_url);

        $orig_tmpfile = $tmpfile;

        if (Tools::copy($url, $tmpfile)) {
            // Evaluate the memory required to resize the image: if it's too much, you can't resize it.
            if (!ImageManager::checkImageMemoryLimit($tmpfile)) {
                @unlink($tmpfile);

                return false;
            }

            $tgt_width = $tgt_height = 0;
            $src_width = $src_height = 0;
            $error = 0;
            ImageManager::resize($tmpfile, $path . '.jpg', null, null, 'jpg', false, $error, $tgt_width, $tgt_height, 5, $src_width, $src_height);
            $images_types = ImageType::getImagesTypes($entity, true);

            if ($regenerate) {
                $previous_path = null;
                $path_infos = array();
                $path_infos[] = array($tgt_width, $tgt_height, $path . '.jpg');
                foreach ($images_types as $image_type) {
                    $tmpfile = self::get_best_path($image_type['width'], $image_type['height'], $path_infos);

                    if (ImageManager::resize(
                        $tmpfile,
                        $path . '-' . stripslashes($image_type['name']) . '.jpg',
                        $image_type['width'],
                        $image_type['height'],
                        'jpg',
                        false,
                        $error,
                        $tgt_width,
                        $tgt_height,
                        5,
                        $src_width,
                        $src_height
                    )) {
                        // the last image should not be added in the candidate list if it's bigger than the original image
                        if ($tgt_width <= $src_width && $tgt_height <= $src_height) {
                            $path_infos[] = array($tgt_width, $tgt_height, $path . '-' . stripslashes($image_type['name']) . '.jpg');
                        }
                        if ($entity == 'products') {
                            if (is_file(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '.jpg')) {
                                unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '.jpg');
                            }
                            if (is_file(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '_' . (int) Context::getContext()->shop->id . '.jpg')) {
                                unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '_' . (int) Context::getContext()->shop->id . '.jpg');
                            }
                        }
                    }
                    if (in_array($image_type['id_image_type'], $watermark_types)) {
                        Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
                    }
                }
            }
        } else {
            @unlink($orig_tmpfile);

            return false;
        }
        unlink($orig_tmpfile);

        return true;
    }
**
     * Creates/updates product images
     * @param int    $id_product
     * @param string $img_name
     * @return bool
     */
    private function createImage($id_product, $img_name)
    {
        $url = Configuration::get('My_KEY') . "v1/images/" . $img_name . ".jpg";
        $shops = Shop::getShops(true, null, true);
        $image = new Image();
        $image->id_product = $id_product;
        $image->position = Image::getHighestPosition($id_product) + 1;
        $image->cover = true;
        if(($image->validateFields(false, true)) === true && ($image->validateFieldsLang(false, true)) === true
            && $image->add()) {
            $image->associateTo($shops);
            if(!AdminImportController::copyImg($id_product, $image->id, $url, 'products', true)) {
                file_put_contents($this->log, $this->date . " createImage error creating image" . PHP_EOL, FILE_APPEND);
                $image->delete();

            }
        }
        return true;
    }