コード例 #1
0
ファイル: Media.php プロジェクト: NatashaOlut/Mage_Test
 public function testAddImage()
 {
     $product = new Mage_Catalog_Model_Product();
     $product->setId(1);
     $file = $this->_model->addImage($product, self::$_mediaTmpDir . '/magento_small_image.jpg');
     $this->assertStringMatchesFormat('/m/a/magento_small_image%sjpg', $file);
 }
コード例 #2
0
ファイル: Media.php プロジェクト: uaudio/magento-filestorage
 /**
  * Add image to media gallery and return new filename
  *
  * @param Mage_Catalog_Model_Product $product
  * @param string                     $file              file path of image in file system
  * @param string|array               $mediaAttribute    code of attribute with type 'media_image',
  *                                                      leave blank if image should be only in gallery
  * @param boolean                    $move              if true, it will move source file
  * @param boolean                    $exclude           mark image as disabled in product page view
  * @return string
  */
 public function addImage(Mage_Catalog_Model_Product $product, $file, $mediaAttribute = null, $move = false, $exclude = true)
 {
     if (!Mage::helper('uaudio_storage')->isEnabled()) {
         return parent::addImage(product, $file, $mediaAttribute, $move, $exclude);
     }
     $file = realpath($file);
     if (!$file || !file_exists($file)) {
         Mage::throwException(Mage::helper('catalog')->__('Image does not exist.'));
     }
     Mage::dispatchEvent('catalog_product_media_add_image', array('product' => $product, 'image' => $file));
     $pathinfo = pathinfo($file);
     $imgExtensions = array('jpg', 'jpeg', 'gif', 'png');
     if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) {
         Mage::throwException(Mage::helper('catalog')->__('Invalid image file type.'));
     }
     $fileName = Mage_Core_Model_File_Uploader::getCorrectFileName($pathinfo['basename']);
     $dispretionPath = Mage_Core_Model_File_Uploader::getDispretionPath($fileName);
     $fileName = $dispretionPath . DS . $fileName;
     $dest = $this->_getConfig()->getTmpMediaPath($fileName);
     $storageModel = Mage::getSingleton('core/file_storage')->getStorageModel();
     $storageModel->setAllowRenameFiles(true);
     try {
         if ($move) {
             $uploadDestination = $storageModel->moveFile($file, $dest);
         } else {
             $uploadDestination = $storageModel->moveUploadFile($file, $dest);
         }
         if (!$uploadDestination) {
             throw new Exception();
         }
         $fileName = basename($uploadDestination);
         $dispretionPath = Mage_Core_Model_File_Uploader::getDispretionPath($fileName);
         $fileName = $dispretionPath . DS . $fileName;
     } catch (Exception $e) {
         Mage::throwException(Mage::helper('catalog')->__('Failed to move file: %s', $e->getMessage()));
     }
     $fileName = str_replace(DS, '/', $fileName);
     $attrCode = $this->getAttribute()->getAttributeCode();
     $mediaGalleryData = $product->getData($attrCode);
     $position = 0;
     if (!is_array($mediaGalleryData)) {
         $mediaGalleryData = array('images' => array());
     }
     foreach ($mediaGalleryData['images'] as &$image) {
         if (isset($image['position']) && $image['position'] > $position) {
             $position = $image['position'];
         }
     }
     $position++;
     $mediaGalleryData['images'][] = array('file' => $fileName, 'position' => $position, 'label' => '', 'disabled' => (int) $exclude);
     $product->setData($attrCode, $mediaGalleryData);
     if (!is_null($mediaAttribute)) {
         $this->setMediaAttribute($product, $mediaAttribute, $fileName);
     }
     return $fileName;
 }
コード例 #3
0
ファイル: Product.php プロジェクト: cewolf2002/magento
 /**
  * Save data row with gallery image info only
  *
  * @param Mage_Catalog_Model_Product $product
  * @param array $importData
  *
  * @return Mage_Catalog_Model_Convert_Adapter_Product
  */
 public function saveImageDataRow($product, $importData)
 {
     $imageData = array('label' => $importData['_media_lable'], 'position' => $importData['_media_position'], 'disabled' => $importData['_media_is_disabled']);
     $imageFile = trim($importData['_media_image']);
     $imageFile = ltrim($imageFile, DS);
     $imageFilePath = Mage::getBaseDir('media') . DS . 'import' . DS . $imageFile;
     $updatedFileName = $this->_galleryBackendModel->addImage($product, $imageFilePath, null, false, (bool) $importData['_media_is_disabled']);
     $this->_galleryBackendModel->updateImage($product, $updatedFileName, $imageData);
     $this->_addAffectedEntityIds($product->getId());
     $product->setIsMassupdate(true)->setExcludeUrlRewrite(true)->save();
     return $this;
 }