1

我想创建一个模块来为产品添加图像,这是我不知道该怎么做的部分

$product = new Product();
        $image = new Image();
        $langId = (int) (Configuration::get('PS_LANG_DEFAULT'));
        $name = Tools::getValue('product_name');
        $product->name = array($langId => $name);
        $product->price = Tools::getValue('price');
        $product->wholesale_price = Tools::getValue('wholesale_price');
        $product->active = false;
        $product->is_virtual = true;
        $product->cache_has_attachments = true;
        $product->id_category_default=3;
        $product->reference = $this->context->cookie->id_customer;
        $product->link_rewrite = array($langId => Tools::link_rewrite($name));
        $product->add();

        $image->id_product = intval($product->id);

            $image->position = Image::getHighestPosition($product->id) + 1;
            if(Image::getImagesTotal($product->id)>0)
             $image->cover = false;
            else
              $image->cover = true;
            $languages = Language::getLanguages();
            foreach ($languages as $language)
             $image->legend[$language['id_lang']] = 'Click to view';
            $id_image = $image->id;
            $image->add();
            $tmpName = tempnam(_PS_IMG_DIR_, 'PS');
            move_uploaded_file($_FILES['design']['tmp_name'], $tmpName);

在上面你看到我为产品添加图像的代码,但我的 BackOffice 中什么也没有,我应该怎么做才能让它出现并上传不同尺寸的图像

4

1 回答 1

2

您需要将图像复制到正确的目录中并在所有 PrestaShop 格式上调整其大小。您可以在最后一行之后执行以下操作:

$new_path = $image->getPathForCreation();
ImageManager::resize($tmpName, $new_path.'.'.$image->image_format);
$imagesTypes = ImageType::getImagesTypes('products');
foreach ($imagesTypes as $imageType)
    ImageManager::resize($tmpName, $new_path.'-'.stripslashes($imageType['name']).'.'.$image->image_format, $imageType['width'], $imageType['height'], $image->image_format);

我希望这会对你有所帮助。

于 2013-08-13T16:36:23.330 回答