it’s an algorithm. here is the concept:
you need to know what size you need for either the height or width, find the percentage of the difference, then get the opposite side to match that percentage and create the new height and width. It may be best as an example:
Original Image size
image.png – height: 600px – width: 400px
you want a thumbnail that is 245px high. So you need that image to be:
image.png – height: 245px – width: ?px
Just subtracting the heights and widths won’t work, the picture gets distorted.
currentHeight = 600;
currentWidth = 400;
newHeight = 245;
newWidth = 0;
sizeDifference = 0;
//get the percentage difference
sizeDifference = (newHeight *100)/currentHeight; //(245*100)/600 = 40.83333..
//get the new width based on the percentage
newWidth = (currentWidth*sizeDifference)/100;//(400*40.83333…)/100=163.3333…
//set your sizes
currentHeight = newHeight;
currentWidth = ciel(newWidth);//using ciel() will remove the decimal and round accordingly
your new image size is:
image.png – height: 245px – width: 163px
I use this for an artists site to dynamically pull images from the portfolio file and display thumbnails. he can upload any size image he wants and it displays perfect every time.
basic wrap up:
perc_diff = (n1 * 100)/c1;
n2 = (c2 * perc_diff)/100;
c1 = n1;
c2 = ceil(n2);
1 & 2: is either height or width depending on what you’re looking for.
Hope this helps!