2013-07-11 80 views
1

我想創建一個模塊,以彌補產品, 圖像,這裏是當我不知道該怎麼辦上傳的圖像產品PHP(的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中沒有任何內容我應該怎麼做才能使其顯示並上傳不同尺寸的圖像

+0

你爲什麼試圖在後端顯示圖像? –

+0

被管理員看到,所以他/她接受它(正如你在代碼中看到的那樣,該產品默認是禁用的) – user2507171

回答

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

我希望能幫到你。

+0

我已經找到了解決方案,但是感謝你的回答它必須工作! – user2507171

+0

@ user2507171好。什麼是解決方案? – 2016-02-11 08:38:31