Call-back icon  Sales: Call 877.832.5289 (N America)|310.295.4144 (International)

Magento

eCommerce Software for Online Growth

Magento Forum

   
Page 2 of 2
Image Resize Function Scale to Original Dimensions? 
 
Moshe
Magento Team
 
Avatar
Total Posts:  1771
Joined:  2007-08-07
Los Angeles
 

@Andrew Smith: you could copy this file to app/code/local/Varien/Image/Adapter/Gd2.php and update it there.

 Signature 

- I would love to change the world, but they won’t give me the source code -

 
Magento Community Magento Community
Magento Community
Magento Community
 
gavh
Member
 
Total Posts:  36
Joined:  2008-04-01
 

The javascript addition is great. How would you center the image?

 
Magento Community Magento Community
Magento Community
Magento Community
 
chezky
Jr. Member
 
Total Posts:  3
Joined:  2007-11-06
 
ryu007 - 07 April 2008 12:04 PM

In order to resolve image proportion, replace this old method with this new :
in lib/Varien/Image/Adapter/Gd2.php :

public function resize($dstWidth=null$dstHeight=null)
    
{
        
if( !isset($dstWidth) && !isset($dstHeight) ) {
            
throw new Exception("Invalid image dimensions.");
        
}

        
if( !isset($dstWidth) ) {
            $width2height 
$this->_imageSrcWidth $this->_imageSrcHeight;
            
$dstWidth round($dstHeight $width2height);
        
elseif( !isset($dstHeight) ) {
            $height2width 
$this->_imageSrcHeight $this->_imageSrcWidth;
            
$dstHeight round($dstWidth $height2width);
        
}

/*
        if( $this->keepProportion() ) {
            if ($this->_imageSrcWidth / $this->_imageSrcHeight >= $dstWidth / $dstHeight) {
                $width = $dstWidth;
                $xOffset = 0;

                $height = round(($width / $this->_imageSrcWidth) * $this->_imageSrcHeight);
                $yOffset = round(($dstHeight - $height) / 2);
            } else {
                $height = $dstHeight;
                $yOffset = 0;

                $width = round(($height / $this->_imageSrcHeight) * $this->_imageSrcWidth);
                $xOffset = round(($dstWidth - $width) / 2);
            }
        } else {
            $xOffset = 0;
            $yOffset = 0;
            $width = $dstWidth;
            $height = $dstHeight;
        }
*/

        
if ($this->_imageSrcWidth $this->_imageSrcHeight >= $dstWidth $dstHeight{
          $width 
$dstWidth;
          
$height round($this->_imageSrcHeight $width $this->_imageSrcWidth);
          
          
$yOffset round(($dstHeight $height) / 2);
          
$xOffset 0;
        
else {
          $height 
$dstHeight;
          
$width round($this->_imageSrcWidth $height $this->_imageSrcHeight);
        
          
$yOffset 0;
          
$xOffset round(($dstWidth $width) / 2);
        
}

        $imageNewHandler 
imagecreatetruecolor($dstWidth$dstHeight);
        
        if( !
$this->keepProportion() ) {
          $bgcolour 
imagecolorallocate($imageNewHandler255255255); // white background
          
imagefill($imageNewHandler00$bgcolour);

          
$width round($this->_imageSrcWidth $height $this->_imageSrcHeight);

          if (
$width $dstWidth{
            $xOffset 
floor(($dstWidth $width) / 2);
          
}
        }

        
if ($this->_fileType == IMAGETYPE_PNG{
            $this
->_saveAlpha($imageNewHandler);
        
}

        imagecopyresampled
($imageNewHandler$this->_imageHandler$xOffset$yOffset00$width$height$this->_imageSrcWidth$this->_imageSrcHeight);

        
$this->_imageHandler $imageNewHandler;

        
$this->refreshImageDimensions();
    
}
Respect orginal dimensions and constraint width or height.
Hard for me to translate properly in english (shame on me) but you will understand after some test.

Cheers,
Ilan

Ilan, I updated gd2.php but nothing actualy changed my images are still not in proportion. is there anything else exept gd2.php that needs to be updated? Please help.
Chezky

 
Magento Community Magento Community
Magento Community
Magento Community
 
Flowstack
Sr. Member
 
Total Posts:  130
Joined:  2008-01-30
London, United Kingdom
 

@chezky - Obvious I know, but have to ask: Did you refresh cache?

 
Magento Community Magento Community
Magento Community
Magento Community
 
macflux
Jr. Member
 
Total Posts:  4
Joined:  2008-05-26
 
Flowstack - 15 May 2008 12:40 AM

@chezky - Obvious I know, but have to ask: Did you refresh cache?

im having the same trouble after refreshing the cache...the image is still stretched to the square format

 
Magento Community Magento Community
Magento Community
Magento Community
 
Daniel_79
Jr. Member
 
Avatar
Total Posts:  23
Joined:  2008-04-19
 

I’m also having some problems with the scaling of images

I created a 135x135 image in photoshop and upload it, then magento seems to rescale it to 135x135 again using some technique that warps the image slightly.

I am attaching the two images to this reply.

original.jpg is the image I upload

magento version.jpg is the file saved on magento server, if you look at the red area you will see some difference in the colors (you have to click to open in full size to see how the orignail should look)

Image Attachments
original.jpgmagento version.jpg
 
Magento Community Magento Community
Magento Community
Magento Community
 
ukdazza
Sr. Member
 
Total Posts:  179
Joined:  2008-04-16
 
Flowstack - 04 April 2008 05:59 AM


1. Open default/template/catalog/product/view/media.phtml.

Ed

Right, am I being thick or is this not a complete directly series?...I dont have a “default” or “template” .... sorry if being thick but where does one look for this file?

 
Magento Community Magento Community
Magento Community
Magento Community
 
oxygen
Member
 
Total Posts:  69
Joined:  2008-06-09
 

hi Moshe, I followed your method and it works fine for my images, except, the thumbnail at the RELATED PRODUCTS (right hand bar), still the same.

However, the thumbnail on MY CART/Recently Added Items, is ok.

Anyone can help? Please?

Thank you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
redsuperhawk
Jr. Member
 
Total Posts:  8
Joined:  2008-03-02
 

I would just like to say thank you to those who have offered the solution to this problem. You save me some major headaches, and I greatly appreciate that!

Jeff
redsuperhawk

 
Magento Community Magento Community
Magento Community
Magento Community
 
magentomatt
Member
 
Total Posts:  31
Joined:  2008-08-08
 

just wanted to thank everyone for you’re contributions here.  It helped me quite a bit (i have no idea what i’m doing)

 
Magento Community Magento Community
Magento Community
Magento Community
 
Thejosh13
Sr. Member
 
Total Posts:  171
Joined:  2008-06-23
 

Does this fix file need to updated for the more recent releases of Magento?

 
Magento Community Magento Community
Magento Community
Magento Community
 
benau
Jr. Member
 
Total Posts:  18
Joined:  2008-07-20
 

This is how I fixed the image scaling problem in my Magento 1.0 installation. This solution is based on what I have read by others in this thread. Thank you to them! I’m hoping the fine level of detail I’ve put down here will assist those new to Magento (and who isn’t new to Magento?!!!).

Before uploading these changed files, consider making backups or renaming the live files on your server with the file extension .bak instead of overwriting.

1. To change the dimensions of the 135x135 pixel image displayed for Simple Products and Grouped Products (and I’m guessing also Configurable Products) I needed to edit this file:

app/design/frontend/default/default/template/catalog/product/list.phtml

Line 45 reads:

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>

I found that changing the height value to 0 was a bad idea (not to mention the other reasons not to do this mentioned in this thread) so I chose 64 for my height:

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 64); ?>" width="135" height="64" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>

Line 93 is very similar and reads:

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
                </
a>

I changed line 93 to:

<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 64); ?>" width="135" height="64" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
                </
a>


2. To change the dimensions of the 68x68 pixel image displayed under the ‘More Views’ heading on a Simple Products or Grouped Products page (and I’m guessing also a Configurable Products page) you need to edit this file:

app/design/frontend/default/default/template/catalog/product/view/media.html

Line 54 reads:

<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(68, 68); ?>" width="68" height="68" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/>

Again, I chose to set an actual height value, so mine read:

<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(68, 32); ?>" width="68" height="32" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/>

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 2 of 2
 
Sales: Call 877.832.5289 (North America) 310.295.4144 (International)
© Copyright 2008 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
53226 users|868 users currently online|107289 forum posts