Ejemplo n.º 1
0
 /**
  *
  * @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;
 }