Using 3Rd Party Code Php To Optimize Images

Hello. I am using cs-cart 4.11.5 version, type ULTIMATE.

I need to optimize images with a custom php script, but I don't know where I can use this code on the cs-cart system.

I know I can buy a plugin for this, but I need to use my code. For example, I have a php code (algorithm), and I need to run it. Where can I paste my php code with the image optimization algorithm?