Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End ImgBrowz0r thumbnail generation Re: ImgBrowz0r thumbnail generation

#80046
dcp3450
Participant

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.

The math:

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);

n: new
c: current
1 & 2: is either height or width depending on what you’re looking for.

Hope this helps!