【问题标题】:Get the height of product images within Magento获取 Magento 中产品图像的高度
【发布时间】:2013-01-08 02:10:22
【问题描述】:

在 Magento 的 media.phtml 文件中,您可以获得第一张产品图片的图片高度:

<?php $imageWidth = $this->helper('catalog/image')->init($_product, 'image')->getOriginalWidth(); ?>

但这不适用于更多产品图片(在 foreach 循环中):

<?php if (count($this->getGalleryImages()) > 1): ?>
<?php foreach ($this->getGalleryImages() as $_image): ?>

也没有..

<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())->getOriginalWidth(); ?>

谁有答案?

【问题讨论】:

    标签: php magento


    【解决方案1】:

    目录图像助手缺乏对此类操作的支持。您将不得不初始化您的图像模型并从那里获取大小。所以:

    <?php foreach ($this->getGalleryImages() as $_image): ?>
      <?php $image = new Varien_Image($_image->getPath()); ?>
      <?php echo $image->getOriginalWidth(); ?>
      <?php echo $image->getOriginalHeight(); ?>
    <?php endforeach; ?>
    

    【讨论】:

    • 感谢您的帮助@Jernej,我最终使用了 PHP getimagesize 函数(请参阅下面的答案)。我不确定是否有任何理由不使用其中任何一个。
    【解决方案2】:

    Jernej 的另一种回答是:

    $imagelink = $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile());                        
    
    list($width, $height, $type, $attr) = getimagesize($imagelink);
    
    echo $width; 
    

    【讨论】:

    • 但这会返回一个像example.com一样的url,不是吗?当您使用 getimagesize 时,它​​会向您的网站发出不必要的 http 请求,这会显着减慢处理速度
    • getimagesize 返回一个包含 7 个元素的数组:php.net/manual/en/function.getimagesize.php。其中一个 cmets 说 'getimagesize 将在检查请求的信息之前下载整个图像。这对于远程访问的大图像来说非常慢。'
    • 我建议在执行getimagesize之前从你的$imagelink中str_replace域部分来获取你服务器上的路径,这样php会在本地读取图像并且可以节省加载时间。它会产生很大的不同,特别是如果您在一页中的多个图像上使用它。
    猜你喜欢
    • 1970-01-01
    • 2012-08-13
    • 2011-01-29
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多