Although this may not be a direct answer to your question, from my experience it is always better to handle image manipulation on another server as you don't want a single user to kill the entire webserver with a few images.
If you handle your images on a different server and it dies, the worst thing that happens is that the image will return 404, but the webserver itself will keep running just fine.
In any case, I would definitely recommend using library such as ImageMagick. It is easier to use and I believe you can set memory limit which it cannot exceed.
The best approach I found for image manipulation so far is AWS Lambda. You can easily write a serverless nodeJS script which downloads your image, performs any modifications to it and uploads it back to your server. Unless you need to process thousands of images every day, you can run it almost for free and it can handle up to 100 simultaneous invocations (it basically starts up to 100 servers at once) so in theory it can be almost 100x faster than if you have to queue your requests.