# VirtueMart Forum

## VirtueMart Dev/Coding Central: VM1 (old version) => Development Projects, Modifications, Hacks & Tweaks. VM1.1 => Topic started by: Marco Dimichele on December 22, 2006, 17:33:15 pm

Title: Thumbnail image resize - wrong algorithm
Post by: Marco Dimichele on December 22, 2006, 17:33:15 pm
Hi, I've found a wrong behaviour in the method used to determine the new size for a resized image when creating a thumbnail, in classes.img2thumb.php (under administrator/components/com_virtuemart/classes).
At line 186 you have:
Code: [Select]
`               \$orig_size = getimagesize(\$filename);                \$maxX = \$newxsize;                \$maxY = \$newysize;                if (\$orig_size[0]<\$orig_size[1])                {                        \$newxsize = \$newysize * (\$orig_size[0]/\$orig_size[1]);                        \$adjustX = (\$maxX - \$newxsize)/2;                        \$adjustY = 0;                }                else                {                        \$newysize = \$newxsize / (\$orig_size[0]/\$orig_size[1]);                        \$adjustX = 0;                        \$adjustY = (\$maxY - \$newysize)/2;                }`The "if" statement is based only upon square new sizes, for example:
let's suppose we have set a thumb max size of 100x80, and consider having an image of 120x110.
The algorithm above will enter the "else" block determining a ratio of 1.2 and a resulting thumb of 100x91.666, thus exceeding the maxY limit of 80.
The condition instead, should consider the original image ratio, comparing it with the newsize ratio, i.e.:  if ((\$orig_size[0]/\$orig_size[1])<(\$maxX/\$maxY))...
So the correct code should be like this:
Code: [Select]
`                \$orig_size = getimagesize(\$filename);                \$maxX = \$newxsize;                \$maxY = \$newysize;                if ((\$orig_size[0]/\$orig_size[1])<(\$maxX/\$maxY))                {                        \$newxsize = \$newysize * (\$orig_size[0]/\$orig_size[1]);                        \$adjustX = (\$maxX - \$newxsize)/2;                        \$adjustY = 0;                }                else                {                        \$newysize = \$newxsize / (\$orig_size[0]/\$orig_size[1]);                        \$adjustX = 0;                        \$adjustY = (\$maxY - \$newysize)/2;                }`
Title: Re: Thumbnail image resize - wrong algorithm
Post by: Thomas on December 23, 2006, 17:55:25 pm
YES! "Du er manden i skysovs" as we say in danish when a guy is really good, and find a solution!

Thank you for this great re-coding! It solved my problem instantly, and the thumbnails is now working perfectly!

- fastwrite, Denmark

Title: Re: Thumbnail image resize - wrong algorithm
Post by: SteelRat on December 26, 2006, 19:34:10 pm
+1
Title: Re: Thumbnail image resize - wrong algorithm
Post by: Pisu on December 27, 2006, 16:03:11 pm
I noticed that in SVN there is still the same code. Please submit it to bugtracker:

http://virtuemart.net/index.php?redirected=1&option=com_flyspray&Itemid=91&project=1&option=com_flyspray&Itemid=91 (http://virtuemart.net/index.php?redirected=1&option=com_flyspray&Itemid=91&project=1&option=com_flyspray&Itemid=91)

So it can be reviewed by developers.
Title: Re: Thumbnail image resize - wrong algorithm
Post by: jack on April 28, 2007, 16:44:52 pm
http://virtuemart.net/index.php?option=com_smf&Itemid=71&topic=27836.0