Author Topic: Thumbnail image resize - wrong algorithm  (Read 3757 times)

Marco Dimichele

  • Beginner
  • *
  • Posts: 5
Thumbnail image resize - wrong algorithm
« 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;
                }

Thomas

  • Beginner
  • *
  • Posts: 2
Re: Thumbnail image resize - wrong algorithm
« Reply #1 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


SteelRat

  • Jr. Member
  • **
  • Posts: 76
    • A25 WDS
Re: Thumbnail image resize - wrong algorithm
« Reply #2 on: December 26, 2006, 19:34:10 pm »
+1

Pisu

  • Contributing Developer
  • Jr. Member
  • *
  • Posts: 451
  • ..still looking for..
    • Blog di Stefano Bagnatica
Re: Thumbnail image resize - wrong algorithm
« Reply #3 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

So it can be reviewed by developers.
Pisu - Team VMItalia (Supporto Italiano VirtueMart)
www.stefanobagnatica.it | www.vmitalia.net