objective c - Algorithm or math to project a GIF file size? -


i have user's animated gif file 10mb. i'd allow users upload , let me host on server, i'd rescale fit maximum file size of 5mb conserve bandwidth hotlinking.

i have basic method right determines targetwidth , targetheight based on pixel surface area.

it works enough:

    cgfloat aspectratio = originalheight / originalwidth;     cgfloat reductionfactor = desiredfilesize / originalfilesize;     cgfloat targetsurfacearea = originalsurfacearea * reductionfactor;      int targetheight = targetsurfacearea / sqrt(targetsurfacearea/aspectratio);     int targetwidth = targetsurfacearea / targetheight; 

its accurate, ex. results: 27mb file turn 3.3mb, or 13.9mb turn 5.5mb.

i tune accuracy closer 5mb, , hoping know bit more how gif color / frame count better factored algorithm. thanks

not sure you're going find easy way this. projecting compressed size of file without running compression algorithm seems me non deterministic.

however, if have plenty of compute cycles use approximation based approach. use algorithm above give first resize of image. if resulting file > 5mb, half resize percentage , try again. if < 5mb add 50% resize percentage , try again. repeat until sufficiently close 5mb.

so, example

50% = 3.3mb, try halfway between 50 , 100 75% = 6.1mb, try halfway between 75 , 50 62.5% = 4.7mb try halfway between 62.5 , 75 

etc


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -