/** * Batch resize of thumbs * @author Skullbock */ function recreateThumbs() { $per_step = 100; $from_id = JRequest::getInt('from_id', 0); $to = $from_id + $per_step; Tienda::load('TiendaHelperCategory', 'helpers.category'); Tienda::load('TiendaImage', 'library.image'); $width = Tienda::getInstance()->get('category_img_width', '0'); $height = Tienda::getInstance()->get('category_img_height', '0'); $model = $this->getModel('Categories', 'TiendaModel'); $model->setState('limistart', $from_id); $model->setState('limit', $to); $row = $model->getTable(); $count = $model->getTotal(); $categories = $model->getList(); $i = 0; $last_id = $from_id; foreach ($categories as $p) { $i++; $image = $p->category_full_image; $path = Tienda::getPath('categories_images'); if ($image != '') { $img = new TiendaImage($path . '/' . $image); $img->setDirectory(Tienda::getPath('categories_images')); // Thumb Tienda::load('TiendaHelperImage', 'helpers.image'); $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper'); $imgHelper->resizeImage($img, 'category'); } $last_id = $p->category_id; } if ($i < $count) { $redirect = "index.php?option=com_tienda&controller=categories&task=recreateThumbs&from_id=" . ($last_id + 1); } else { $redirect = "index.php?option=com_tienda&view=config"; } $redirect = JRoute::_($redirect, false); $this->setRedirect($redirect, JText::_('COM_TIENDA_DONE'), 'notice'); return; }
/** * 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'); } } } }
/** * Will consolidate a product's images into its currently set path. * If an image already exists in the current path with the same name, * will either leave the iamge in the old path or delete it if delete_duplicates = true * * @param $row Tienda Product * @param $delete_duplicates * @return unknown_type */ function consolidateGalleryImages($row, $delete_duplicates = false) { $file_moved = null; // get the current path for the product $path = $this->getGalleryPath($row); // get the current list of images in the current path $images = $this->getGalleryImages($path, array(), false); // if there are any images in the other possible paths for the product, move them to the current path $dir = Tienda::getPath('products_images'); // merge the SKU-based dir if it exists and isn't the current path if (!empty($row->product_sku) && $this->checkDirectory($dir . '/' . $row->product_sku, false) && $dir . '/' . $row->product_sku . '/' != $path) { $old_dir = $dir . '/' . $row->product_sku . '/'; $files = JFolder::files($old_dir); foreach ($files as $file) { if (!in_array($file, $images)) { if (JFile::move($old_dir . $file, $path . $file)) { // create new thumb too Tienda::load('TiendaImage', 'library.image'); $img = new TiendaImage($path . $file); $img->setDirectory($path); Tienda::load('TiendaHelperImage', 'helpers.image'); $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper'); $imgHelper->resizeImage($img); // delete old thumb JFile::delete($old_dir . 'thumbs/' . $file); $file_moved = true; } } else { // delete the old one? if ($delete_duplicates) { JFile::delete($old_dir . $file); } } } } else { $subdirs = TiendaHelperProduct::getSha1Subfolders($row->product_sku); // merge the SKU-based SHA1 dir if it exists and isn't the current path if (!empty($row->product_sku) && $this->checkDirectory($dir . '/' . $subdirs . $row->product_sku, false) && $dir . '/' . $subdirs . $row->product_sku . '/' != $path) { $old_dir = $dir . '/' . $subdirs . $row->product_sku . '/'; $files = JFolder::files($old_dir); foreach ($files as $file) { if (!in_array($file, $images)) { if (JFile::move($old_dir . $file, $path . $file)) { // create new thumb too Tienda::load('TiendaImage', 'library.image'); $img = new TiendaImage($path . $file); $img->setDirectory($path); Tienda::load('TiendaHelperImage', 'helpers.image'); $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper'); $imgHelper->resizeImage($img); // delete old thumb JFile::delete($old_dir . 'thumbs' . '/' . $file); $file_moved = true; } } else { // delete the old one? if ($delete_duplicates) { JFile::delete($old_dir . $file); } } } } } // merge the ID-based dir if it exists and isn't the current path if ($this->checkDirectory($dir . '/' . $row->product_id, false) && $dir . '/' . $row->product_id . '/' != $path) { $old_dir = $dir . '/' . $row->product_id . '/'; $files = JFolder::files($old_dir); foreach ($files as $file) { if (!in_array($file, $images)) { if (JFile::move($old_dir . $file, $path . $file)) { // create new thumb too Tienda::load('TiendaImage', 'library.image'); $img = new TiendaImage($path . $file); $img->setDirectory($path); Tienda::load('TiendaHelperImage', 'helpers.image'); $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper'); $imgHelper->resizeImage($img); // delete old thumb JFile::delete($old_dir . 'thumbs' . '/' . $file); $file_moved = true; } } else { // delete the old one? if ($delete_duplicates) { JFile::delete($old_dir . $file); } } } } else { $subdirs = TiendaHelperProduct::getSha1Subfolders($row->product_id); // merge the ID-based SHA1 dir if it exists and isn't the current path if ($this->checkDirectory($dir . '/' . $subdirs . $row->product_id, false) && $dir . '/' . $subdirs . $row->product_id . '/' != $path) { $old_dir = $dir . '/' . $subdirs . $row->product_id . '/'; $files = JFolder::files($old_dir); foreach ($files as $file) { if (!in_array($file, $images)) { if (JFile::move($old_dir . $file, $path . $file)) { // create new thumb too Tienda::load('TiendaImage', 'library.image'); $img = new TiendaImage($path . $file); $img->setDirectory($path); Tienda::load('TiendaHelperImage', 'helpers.image'); $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper'); $imgHelper->resizeImage($img); // delete old thumb JFile::delete($old_dir . 'thumbs/' . $file); $file_moved = true; } } else { // delete the old one? if ($delete_duplicates) { JFile::delete($old_dir . $file); } } } } } return $file_moved; }
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++; } } }
/** * Upload via ajax through Uploadify * It's here because when the swf connects to the admin side, it would need to login. */ function uploadifyImage() { JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $product_id = JRequest::getInt('product_id', 0); if ($product_id) { Tienda::load('TiendaImage', 'library.image'); $upload = new TiendaImage(); // handle upload creates upload object properties $upload->handleUpload('Filedata'); // then save image to appropriate folder $product = JTable::getInstance('Products', 'TiendaTable'); $product->load($product_id); $path = $product->getImagePath(); $upload->setDirectory($path); // Do the real upload! $success = $upload->upload(); Tienda::load('TiendaHelperImage', 'helpers.image'); $imgHelper = TiendaHelperBase::getInstance('Image', 'TiendaHelper'); if (!$imgHelper->resizeImage($upload, 'product')) { $success = false; } if ($success) { // save as default? if (empty($product->product_full_image)) { $product->product_full_image = $upload->getPhysicalName(); $product->save(); } echo JText::_('COM_TIENDA_IMAGE_UPLOADED_CORRECTLY'); } else { echo 'Error: ' . $upload->getError(); } } }