上传产品图片PHP(Prestashop)

时间:2013-07-11 21:46:59

标签: php image upload hyperlink prestashop

我想创建一个模块来提升产品图片, 这是我不知道该做什么的部分

$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中没有任何内容可以使它显示并上传不同大小的图像

1 个答案:

答案 0 :(得分: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);

我希望这会对你有帮助。