Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Specify image dimensions in an existing template

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
SirKenny
Smarty n00b


Joined: 24 Oct 2014
Posts: 3

PostPosted: Fri Oct 24, 2014 10:30 am    Post subject: Specify image dimensions in an existing template Reply with quote

Hey!
I`m sending from outta space to solve my smarty case Very Happy

It is my first posting and I have to admit, I have to engage with the smarty manual, but I have a problem which has to be solved today / tomorrow and I guess it is quite easy for you guys.

Situation: We built up an online shop with the help a template we bought.

This template uses smarty code. Actually I try to optimize the page for gtmetrix(.com). The only thing left is "Specify image dimension".

On the starting page are pictures of the main categories displayed. Using a listing which is like:

Code:
{foreach name=aussen item=module_data from=$_categories}
         <li><a href="{$module_data.categories_link}" title="{$module_data.categories_heading_title}">
         <p>{img img=$module_data.categories_image type=m_info alt=$module_data.categories_name}</p>
         <p align="center">{$module_data.categories_name}</p></a>{if !$smarty.foreach.aussen.last}{/if}</li>
         {/foreach}


The listing is a box and has the following php file:
Code:
 
<?php
/*
 #########################################################################
 #                       xt:Commerce VEYTON 4.0 Shopsoftware
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #
 # Copyright 2007-2011 xt:Commerce International Ldt. All Rights Reserved.
 # This file may not be redistributed in whole or significant part.
 # Content of this file is Protected By International Copyright Laws.
 #
 # ~~~~~~ xt:Commerce VEYTON 4.0 Shopsoftware IS NOT FREE SOFTWARE ~~~~~~~
 #
 # http://www.xt-commerce.com
 #
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #
 # @version $Id: categories.php 5481 2012-09-18 08:39:47Z tu $
 # @copyright xt:Commerce International Ldt., www.xt-commerce.com
 #
 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 #
 # xt:Commerce International Ldt., Kafkasou 9, Aglantzia, CY-2112 Nicosia
 #
 # office@xt-commerce.com
 #
 #########################################################################
 */

defined('_VALID_CALL') or die('Direct Access is not allowed.');

$tpl_data = array('_categories'=> $category);
?>



I also have an existing functions.img.php which looks like:

Code:

<?php
/*
  #########################################################################
  #                       xt:Commerce VEYTON 4.0 Shopsoftware
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #
  # Copyright 2007-2011 xt:Commerce International Ltd. All Rights Reserved.
  # This file may not be redistributed in whole or significant part.
  # Content of this file is Protected By International Copyright Laws.
  #
  # ~~~~~~ xt:Commerce VEYTON 4.0 Shopsoftware IS NOT FREE SOFTWARE ~~~~~~~
  #
  # http://www.xt-commerce.com
  #
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #
  # @version $Id: function.img.php 5039 2012-03-12 09:39:30Z valentinl $
  # @copyright xt:Commerce International Ltd., www.xt-commerce.com
  #
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #
  # xt:Commerce International Ltd., Kafkasou 9, Aglantzia, CY-2112 Nicosia
  #
  # office@xt-commerce.com
  #
  #########################################################################
 */

defined('_VALID_CALL') or die('Direct Access is not allowed.');

/**
 * Return image tag or path
 *
 * $param may contain the following keys:
 * - type: t|m|mi|w|p (explode by "_)
 * - img: image path
 * - plg: plugin path
 * - subdir: subdirectory path
 * - class: class="{class}"
 * - alt: alt="{alt}"
 * - title: titl="{title}"
 * - itemprop: boolean (if true add itemprop="image")
 * - width: image width
 * - height: image height
 * - path_only: boolean (if true return only path)
 * @global type $language
 * @global type $template
 * @global type $mediaImages
 * @param array $params
 * @param type $smarty
 * @version 1.0.1 add itemprop,title,alt
 */
function smarty_function_img($params, & $smarty) {
    global $language, $template, $mediaImages;
   
    $type_array = explode("_", $params['type']);

    if ($type_array[0] == 't') {

        $url = _SYSTEM_BASE_URL . _SRV_WEB . _SRV_WEB_TEMPLATES . $template->selected_template . '/';
    } elseif ($type_array[0] == 'm') {

        $url = _SYSTEM_BASE_URL . _SRV_WEB . _SRV_WEB_IMAGES;
    } elseif ($type_array[0] == 'mi') {

        $url = _SYSTEM_BASE_URL . _SRV_WEB . _SRV_WEB_ICONS;
    } elseif ($type_array[0] == 'w') {

        $url = _SYSTEM_BASE_URL . _SRV_WEB;
    } elseif ($type_array[0] == 'p') {

        $url = __getImagePath($params['img'], $params['plg'], $params['subdir']);
    } else {
        $url = $params['url'];
    }

    $url = str_replace("/xtAdmin", '', $url);
    /*
      if ($params['img_class'] && !file_exists(_SRV_WEBROOT.'media/images/'.$type_array[1].'/'.$params['img']) && file_exists(_SRV_WEBROOT.'media/images/org/'.$params['img'])) {
      $mediaImages->setClass($params['img_class']);
      $mediaImages->processImage($params['img'], false);
      }
     */

    unset($type_array[0]);

    $file_type = __getFileType($params['img'], $type_array[1]);

    $params['img'] = $file_type['filename'];
    $url .= $file_type['main_dir'];

    if (is_data($type_array)) {
        while (list ($key, $value) = each($type_array)) {
            $url .= $value . '/';
        }
    }

    if (!empty($params['class']))
        $class = ' class="' . $params['class'] . '"';

    if (!empty($params['alt']))
        $class .= ' alt="' . $params['alt'] . '"';

    if (!empty($params['title']))
        $class .= ' title="' . $params['title'] . '"';

    if (!empty($params['itemprop']) && ($params['itemprop'] === true) )
        $class .= ' itemprop="image" ';

    $size = "";

    if (!empty($params['width'])) {
        if (file_exists(_SRV_WEBROOT . 'media/images/' . $type_array[1] . '/' . $params['img'])) {
            $image_size = getimagesize(_SRV_WEBROOT . 'media/images/' . $type_array[1] . '/' . $params['img']);
            $_height = $image_size[1];
            $_width = $image_size[0];

            $ratio_width = $_width / $params['width'];
            $height = $_height / $ratio_width;

            $size = ' width="' . $params['width'] . '" height="' . $height . '"';
        }
    }
    if (!empty($params['height'])) {
        if (file_exists(_SRV_WEBROOT . 'media/images/' . $type_array[1] . '/' . $params['img'])) {
            $image_size = getimagesize(_SRV_WEBROOT . 'media/images/' . $type_array[1] . '/' . $params['img']);
            $_height = $image_size[1];
            $_width = $image_size[0];

            $ratio_width = $_width / $params['width'];
            $height = $_height / $ratio_width;

            $size = ' width="' . $params['width'] . '" height="' . $height . '"';
        }
    }
    if ($size == "") {
        if (file_exists(_SRV_WEBROOT . 'media/images/' . $type_array[1] . '/' . $params['img'])) {
            $image_size = getimagesize(_SRV_WEBROOT . 'media/images/' . $type_array[1] . '/' . $params['img']);
            $_height = $image_size[1];
            $_width = $image_size[0];
            $size = ' width="' . $_width . '" height="' . $_height . '"';
        }
    }

    $img = '<img src="' . $url . $params['img'] . '"' . $class . $size . ' />';

    if (isset($params['path_only'])) {
        echo $url . $params['img'];
    } else {
        echo $img;
    }
}

/**
 * return filepath or false
 * @global type $template
 * @param string $file
 * @param string $dir
 * @param string $subdir
 * @return string|boolean
 */
function __getImagePath($file, $dir, $subdir = '') {
    global $template;

    if ($subdir)
        $subdir = $subdir . '/';

    $img_root_path = _SRV_WEBROOT . _SRV_WEB_TEMPLATES . $template->selected_template . '/' . _SRV_WEB_PLUGINS . $dir . '/images/' . $subdir;
    $img_path = _SYSTEM_BASE_URL . _SRV_WEB . _SRV_WEB_TEMPLATES . $template->selected_template . '/' . _SRV_WEB_PLUGINS . $dir . '/images/' . $subdir;

    $img_root_plugin_path = _SRV_WEBROOT . _SRV_WEB_PLUGINS . $dir . '/images/' . $subdir;
    $img_plugin_path = _SYSTEM_BASE_URL . _SRV_WEB . _SRV_WEB_PLUGINS . $dir . '/images/' . $subdir;

    if (file_exists($img_root_path . $file)) {
        return $img_path;
    } elseif (file_exists($img_root_plugin_path . $file)) {
        return $img_plugin_path;
    } else {
        return false;
    }
}

function __getFileType($img, $type) {

    require_once(_SRV_WEBROOT . _SRV_WEB_FRAMEWORK . 'classes/class.FileHandler.php');

    $tmp_img_data = explode(':', $img);
    $img_type = $tmp_img_data[0];
    $img_name = $tmp_img_data[1];

    $mf = new FileHandler();
    $mf->setParentDir(_SRV_WEB_IMAGES . $img_type . '/' . $type);
    $img_check = $mf->_checkFile($img_name);

    if ($img_check) {
        $img_array = array('main_dir' => $img_type . '/', 'filename' => $img_name);
    } else {

        if (ereg(':', $img))
            $img = $img_name;

        $img_array = array('main_dir' => '', 'filename' => $img);
    }

    return $img_array;
}

?>


In Line 145 is the following:
Code:

$img = '<img src="' . $url . $params['img'] . '"' . $class . $size . ' />';


When I use firefox (Q) to inspect the element it looks like this line.
The size is mentioned but is not displayed in the generated code.

I tried to add different params to this line:
Code:
{img img=$module_data.categories_image type=m_info alt=$module_data.categories_name}


Result: nothing, if I add something with a "$" the whole starting page "crashes".

Is there any way to add this information?

I am very thankful for every kind of help.

I also tried to use the search button, but most of the codes displayed look so different to the code which is in my template.

It is an xt commerce 4.0.16 shop.

Best regards from germany,

Chris[/code]
Back to top
View user's profile Send private message
SirKenny
Smarty n00b


Joined: 24 Oct 2014
Posts: 3

PostPosted: Fri Oct 24, 2014 1:19 pm    Post subject: Reply with quote

I guess I solved it.

At least the information / problem named by gtmetrix(.com) "Specify Image Dimensions" is not diplayed any longer.

Seems to be the right decision to work on this line:
Code:
{img img=$module_data.categories_image type=m_info alt=$module_data.categories_name}


I guess I raped the code but it now looks like:
Code:
<img width="248px" height="160px" {img img=$module_data.categories_image type=m_info alt=$module_data.categories_name}>


I guess there is a better way to solve it!?

Would it be possible to add a variable instead of width="248px" height="160px"?
"$image_size" or something like that?

Best regards

Chris
Back to top
View user's profile Send private message
SirKenny
Smarty n00b


Joined: 24 Oct 2014
Posts: 3

PostPosted: Fri Oct 24, 2014 1:53 pm    Post subject: Reply with quote

Okay its me again.

I finally found the variables I looked for.

I found it in function.img.php.

My final code for the specific line:
Code:
<img ' width="' . $_width . '" height="' . $_height . '"' {img img=$module_data.categories_image type=m_info alt=$module_data.categories_name}>


If you have abetter solution please let me know!

Best regards
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP