/** * 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'); } } } }
/** * * @param $id * @param $by * @param $alt * @param $type * @param $url * @return unknown_type */ public static function getImage($id, $by = 'id', $alt = '', $type = 'thumb', $url = false, $resize = false, $options = array(), $main_product = false) { $style = ""; $height_style = ""; $width_style = ""; $dimensions = ""; if (!empty($options['width'])) { $dimensions .= "width=\"" . $options['width'] . "\" "; } if (!empty($options['height'])) { $dimensions .= "height=\"" . $options['height'] . "\" "; } if (!empty($options['height'])) { $height_style = "max-height: " . $options['height'] . "px;"; } if (!empty($options['width'])) { $width_style = "max-width: " . $options['width'] . "px;"; } if (!empty($height_style) || !empty($width_style)) { $style = "style='{$height_style} {$width_style}'"; } switch ($type) { case "full": $path = 'products_images'; break; case "thumb": default: $path = 'products_thumbs'; break; } $tmpl = ""; if (strpos($id, '.')) { // then this is a filename, return the full img tag if file exists, otherwise use a default image $src = JFile::exists(Tienda::getPath($path) . '/' . $id) ? Tienda::getUrl($path) . $id : JURI::root(true) . '/media/com_tienda/images/placeholder_239.gif'; // if url is true, just return the url of the file and not the whole img tag $tmpl = $url ? $src : "<img " . $dimensions . " src='" . $src . "' alt='" . JText::_($alt) . "' title='" . JText::_($alt) . "' align='middle' border='0' " . $style . " />"; } else { if (!empty($id)) { if (isset($this) && is_a($this, 'TiendaHelperProduct')) { $helper =& $this; } else { $helper = TiendaHelperBase::getInstance('Product'); } $model = Tienda::getClass('TiendaModelProducts', 'models.products'); $model->setId((int) $id); $item = $model->getItem(); $full_image = !empty($item->product_full_image) ? $item->product_full_image : null; $thumb_image = !empty($item->product_thumb_image) ? $item->product_thumb_image : $full_image; switch ($type) { case "full": $image_ref = $full_image; break; case "thumb": default: $image_ref = $thumb_image; break; } if (filter_var($image_ref, FILTER_VALIDATE_URL) !== false) { // $full_image contains a valid URL $src = $image_ref; if ($url) { return $src; } elseif (!empty($src)) { $tmpl = "<img src='" . $src . "' alt='" . JText::_($alt) . "' title='" . JText::_($alt) . "' />"; return $tmpl; } } $row = $helper->load((int) $id, true, false); // load the item, get the filename, create tmpl $urli = $row->getImageUrl(); $dir = $row->getImagePath(); if ($path == 'products_thumbs') { $dir .= 'thumbs'; $urli .= 'thumbs/'; } if ($main_product) { $dispatcher = JDispatcher::getInstance(); $dispatcher->trigger('onGetProductMainImage', array($row->product_id, &$full_image, $options)); } $dirname = dirname($image_ref); if (!empty($dirname) && $dirname !== ".") { $dir = JPath::clean(JPATH_SITE . "/" . dirname($image_ref)); $urli = JURI::root(true) . '/' . dirname($image_ref) . '/'; $file = JPath::clean(JPATH_SITE . "/" . $image_ref); $id = JURI::root(true) . '/' . $image_ref; } else { $file = $dir . '/' . $image_ref; $id = $urli . $image_ref; } // Gotta do some resizing first? if ($resize) { // Add a suffix to the thumb to avoid conflicts with user settings $suffix = ''; if (isset($options['width']) && isset($options['height'])) { $suffix = '_' . $options['width'] . 'x' . $options['height']; } elseif (isset($options['width'])) { $suffix = '_w' . $options['width']; } elseif (isset($options['height'])) { $suffix = '_h' . $options['height']; } // Add suffix to file path $dot = strrpos($file, '.'); $resize = substr($file, 0, $dot) . $suffix . substr($file, $dot); if (!JFile::exists($resize)) { Tienda::load('TiendaImage', 'library.image'); $image = new TiendaImage($file); $image->load(); // If both width and height set, gotta figure hwo to resize if (isset($options['width']) && isset($options['height'])) { // If width is larger, proportionally if ($options['width'] / $image->getWidth() < $options['height'] / $image->getHeight()) { $image->resizeToWidth($options['width']); $image->save($resize); } else { $image->resizeToHeight($options['height']); $image->save($resize); } } elseif (isset($options['width'])) { $image->resizeToWidth($options['width']); $image->save($resize); } elseif (isset($options['height'])) { $image->resizeToHeight($options['height']); $image->save($resize); } } // Add suffix to url path $dot = strrpos($id, '.'); $id = substr($id, 0, $dot) . $suffix . substr($id, $dot); } $src = JFile::exists($file) ? $id : JURI::root(true) . '/media/com_tienda/images/placeholder_239.gif'; $tmpl = $url ? $src : "<img " . $dimensions . " src='" . $src . "' alt='" . JText::_($alt) . "' title='" . JText::_($alt) . "' align='middle' border='0' " . $style . " />"; } } return $tmpl; }
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(); } } }