Example #1
0
 /**
  * Migrate the images
  * 
  * @param int $product_id
  * @param string $images
  */
 private function _migrateImages($product_id, $images)
 {
     Tienda::load('TiendaImage', 'library.image');
     foreach ($images->children() as $image) {
         $check = false;
         $multiple = false;
         $image = (string) $image;
         if (JURI::isInternal($image)) {
             $internal = true;
             $int_image = JPATH_SITE . DS . $image;
             if (is_dir($int_image)) {
                 $check = JFolder::exists($int_image);
                 $multiple = true;
             } else {
                 $check = JFile::exists($int_image);
             }
             // Now check the extracted images path
             if (!$check) {
                 $dir = $this->_temp_dir . DS . 'images' . DS;
                 if (is_dir($dir . $image)) {
                     $check = JFolder::exists($dir . $image);
                     $multiple = true;
                 } else {
                     $check = JFile::exists($dir . $image);
                 }
                 if ($check) {
                     $image = $dir . $image;
                 }
             } else {
                 $image = $int_image;
             }
         } else {
             $internal = false;
             $check = $this->url_exists($image);
         }
         // Add a single image
         if (!$multiple) {
             $images_to_copy = array($image);
         } else {
             // Fetch the images from the folder and add them
             $images_to_copy = Tienda::getClass("TiendaHelperProduct", 'helpers.product')->getGalleryImages($image);
             foreach ($images_to_copy as &$i) {
                 $i = $image . DS . $i;
             }
         }
         if ($check) {
             foreach ($images_to_copy as $image_to_copy) {
                 if ($internal) {
                     $img = new TiendaImage($image_to_copy);
                 } else {
                     $tmp_path = JFactory::getApplication()->getCfg('tmp_path');
                     $file = fopen($image_to_copy, 'r');
                     $file_content = stream_get_contents($file);
                     fclose($file);
                     $file = fopen($tmp_path . DS . $image_to_copy, 'w');
                     fwrite($file, $file_content);
                     fclose($file);
                     $img = new TiendaImage($tmp_path . DS . $image_to_copy);
                 }
                 Tienda::load('TiendaTableProducts', 'tables.products');
                 $product = JTable::getInstance('Products', 'TiendaTable');
                 $product->load($product_id);
                 $path = $product->getImagePath();
                 $type = $img->getExtension();
                 $img->load();
                 $img->setDirectory($path);
                 // Save full Image
                 $img->save($path . $img->getPhysicalName());
                 // Save Thumb
                 Tienda::load('TiendaHelperImage', 'helpers.image');
                 $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper');
                 $imgHelper->resizeImage($img, 'product');
             }
         }
     }
 }
 private function _migrateImages($prefix = 'jos_', $vm_prefix = 'vm_', &$results, $internal = true)
 {
     $p = $prefix . $vm_prefix;
     // Fetch the VM full image
     if ($internal) {
         $db = JFactory::getDBO();
     } else {
         $db = $this->_verifyDB();
     }
     $query = "SELECT product_id as id, product_full_image as image FROM {$p}product";
     $db->setQuery($query);
     $products = $db->loadAssocList();
     Tienda::load('TiendaImage', 'library.image');
     if ($internal) {
         $vm_image_path = JPATH_SITE . "/components/com_virtuemart/shop_image/product/";
     } else {
         $state = $this->_getState();
         $url = $state->external_site_url;
         $vm_image_path = $url . "/components/com_virtuemart/shop_image/product/";
     }
     $n = count($results);
     $results[$n]->title = 'Product Images';
     $results[$n]->query = 'Copy Product Images & Resize';
     $results[$n]->error = '';
     $results[$n]->affectedRows = 0;
     foreach ($products as $result) {
         $check = false;
         if ($internal) {
             $check = JFile::exists($vm_image_path . $result['image']);
         } else {
             $check = $this->url_exists($vm_image_path) && $result['image'];
         }
         if ($check) {
             if ($internal) {
                 $img = new TiendaImage($vm_image_path . $result['image']);
             } else {
                 $tmp_path = JFactory::getApplication()->getCfg('tmp_path');
                 $file = fopen($vm_image_path . $result['image'], 'r');
                 $file_content = stream_get_contents($file);
                 fclose($file);
                 $file = fopen($tmp_path . DS . $result['image'], 'w');
                 fwrite($file, $file_content);
                 fclose($file);
                 $img = new TiendaImage($tmp_path . DS . $result['image']);
             }
             Tienda::load('TiendaTableProducts', 'tables.products');
             $product = JTable::getInstance('Products', 'TiendaTable');
             $product->load($result['id']);
             $path = $product->getImagePath();
             $type = $img->getExtension();
             $img->load();
             $name = $img->getPhysicalName();
             // Save full Image
             if (!$img->save($path . $name)) {
                 $results[$n]->error .= '::Could not Save Product Image- From: ' . $vm_image_path . $result['image'] . ' To: ' . $path . $result['image'];
             }
             $img->setDirectory($path);
             // Save Thumb
             Tienda::load('TiendaHelperImage', 'helpers.image');
             $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper');
             if (!$imgHelper->resizeImage($img, 'product')) {
                 $results[$n]->error .= '::Could not Save Product Thumb';
             }
             // Save correct image naming
             $product->product_full_image = $name;
             $product->save();
             $results[$n]->affectedRows++;
         }
     }
     $n++;
     // CATEGORIES
     // Fetch the VM full image
     $query = "SELECT category_id as id, category_full_image as image FROM {$p}category";
     $db->setQuery($query);
     $products = $db->loadAssocList();
     Tienda::load('TiendaImage', 'library.image');
     if ($internal) {
         $vm_image_path = JPATH_SITE . "/components/com_virtuemart/shop_image/category/";
     } else {
         $state = $this->_getState();
         $url = $state->external_site_url;
         $vm_image_path = $url . "/components/com_virtuemart/shop_image/category/";
     }
     $results[$n]->title = 'Category Images';
     $results[$n]->query = 'Copy Category Images & Resize';
     $results[$n]->error = '';
     $results[$n]->affectedRows = 0;
     foreach ($products as $result) {
         $check = false;
         if ($internal) {
             $check = JFile::exists($vm_image_path . $result['image']);
         } else {
             $check = $this->url_exists($vm_image_path) && $result['image'];
         }
         if ($check) {
             if ($internal) {
                 $img = new TiendaImage($vm_image_path . $result['image']);
             } else {
                 $tmp_path = JFactory::getApplication()->getCfg('tmp_path');
                 $file = fopen($vm_image_path . $result['image'], 'r');
                 $file_content = stream_get_contents($file);
                 fclose($file);
                 $file = fopen($tmp_path . DS . $result['image'], 'w');
                 fwrite($file, $file_content);
                 fclose($file);
                 $img = new TiendaImage($tmp_path . DS . $result['image']);
             }
             $img->load();
             $path = Tienda::getPath('categories_images') . DS;
             $name = $img->getPhysicalName();
             // Save full Image
             if (!$img->save($path . $name)) {
                 $results[$n]->error .= '::Could not Save Category Image - From: ' . $vm_image_path . $result['image'] . ' To: ' . $path . $result['image'];
             }
             $img->setDirectory($path);
             // Save Thumb
             Tienda::load('TiendaHelperImage', 'helpers.image');
             $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper');
             if (!$imgHelper->resizeImage($img, 'category')) {
                 $results[$n]->error .= '::Could not Save Category Thumb';
             }
             // Save correct image name
             Tienda::load('TiendaTableCategories', 'tables.categories');
             $category = JTable::getInstance('Categories', 'TiendaTable');
             $category->load($result['id']);
             $category->category_full_image = $name;
             $category->save();
             $results[$n]->affectedRows++;
         }
     }
 }