【问题标题】:Upload an image for a product PHP (Prestashop)上传产品图片 PHP (Prestashop)
【发布时间】:2013-07-11 21:46:59
【问题描述】:

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

$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 中什么也没有,我应该怎么做才能让它显示并上传不同尺寸的图片

【问题讨论】:

  • 为什么要在后台显示图片?
  • 管理员可以看到,所以他/她接受(如您在代码中看到的,该产品默认禁用)

标签: php image upload hyperlink prestashop


【解决方案1】:

您需要将图像复制到正确的目录中并在所有 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);

希望对你有帮助。

【讨论】:

  • 我已经找到了解决方案,但感谢您的回答它必须有效!
  • @user2507171 好。解决办法是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
相关资源
最近更新 更多