class.upload.php is a powerful and mature PHP class to manage uploaded files, and manipulate images in many ways. The script is available under a GPL license.
more info about the class
K2 is setting the JPEG quality to 100 when storing the first image:
https://github.com/getk2/k2/blob/8249eae20390c569a11521ef4f32f75f5376ab3b/administrator/components/com_k2/models/item.php#L264
Your only solution is to hack K2 as it is hardcoded.