ImageRecycle provides APIs to add image/pdf, compress them and download them. This makes things interesting, so basically, these days, I want to shrink images to reduce the space they take on VPS server. ImageRecycle supports optimisation of the following file extensions: JPEG/JPG, GIF, BMP, PNG, PDF.
Here is the API documentation to add a new image. POST /images
Example:
1 2 3 4 5 | curl -X POST -d 'auth={"key":"your_api_key","secret":"your_api_secret"}' -d 'url=https://uploadbeta.com/share-image/2xw' -d 'params={"compression_type":"lossy"}' 'https://api.imagerecycle.com/v1/images' |
curl -X POST -d 'auth={"key":"your_api_key","secret":"your_api_secret"}' -d 'url=https://uploadbeta.com/share-image/2xw' -d 'params={"compression_type":"lossy"}' 'https://api.imagerecycle.com/v1/images'
If a local file is specified instead, you have to use the following syntax:
1 2 3 4 5 | curl -X POST -F 'auth={"key":"your_api_key","secret":"your_api_secret"}' -F 'file=@local_file.jpg' -F 'params={"compression_type":"lossy"}' 'https://api.imagerecycle.com/v1/images' |
curl -X POST -F 'auth={"key":"your_api_key","secret":"your_api_secret"}' -F 'file=@local_file.jpg' -F 'params={"compression_type":"lossy"}' 'https://api.imagerecycle.com/v1/images'
And here is the help from ImageRecycle support team. Thanks for prompt replies:
If you want to send an image stored on your server it’s not a simple POST query you need to send a POST query with the content type multipart/form-data.
With curl you can do that by using the -F option instead of the -d option.
You have also to specify with parameter is a file by using a @ before the file name.
If you want more informations it’s documented on the curl man page available online : http://curl.haxx.se/docs/manpage.htmlHere is the command line you should use :
curl -X POST -F ‘auth={“key”:”your_api_key”,”secret”:”your_api_secret”}’ -F ‘file=@the_file_to_send.png’ -F ‘params={“compression_type”:”lossy”}’ ‘https://api.imagerecycle.com/v1/images’
You can use the parameters
– compression_type”:”lossy” or compression_type”:”lossless” for lossless or lossy compression
– “resize”:{“width”:100,”height”:100} to resize if needed the image, you can specify only with or heightIn this case the request would be :
curl -X POST -F ‘auth={“key”:”your_api_key”,”secret”:”your_api_secret”}’ -F ‘file=@the_file_to_send.png’ -F ‘params={“compression_type”:”lossy”,”resize”:{“width”:100,”height”:100} }’ ‘https://api.imagerecycle.com/v1/images’
So, we can write a PHP Script, like following to optimise the local images (shrink the file size without touching the dimensions or images or PDF).
- POST the local image/pdf to ImageRecycle server using POST /images API
- Rename the local image/pdf to have a backup
- Download the compressed file from ImageRecycle
Here is the screen shot, make sure you replace the app_key and app_seret.
And the server confirms this optimisation from [Optimisation History] panel.
I have a test.jpg which is optimised from 1.7M to 1.6M. It might not be a lot and if I choose other settings, it might give me more savings (such as re-size). [I will write another post on reviewing ImageRecycle and other competitors such as Imagefy.
The complete source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #!/usr/bin/php <?php // Author: https://helloacm.com $key = "app_key"; $secret = "app_secret"; function ImageRecycle($img, $key, $secret) { $cmd = "curl -X POST -F 'auth={\"key\":\"$key\",\"secret\":\"$secret\"}' -F 'file=@$img' -F 'params={\"compression_type\":\"lossy\"}' 'https://api.imagerecycle.com/v1/images'"; echo $cmd . "\n"; $rtn = json_decode(shell_exec($cmd), true); var_dump($rtn); $err = 201; if (isset($rtn['errCode'])) { $err = (integer)$rtn['errCode']; } $id = 0; if (isset($rtn['id'])) { $id = (integer)$rtn['id']; } if (($err == 201) || ($id > 0)) { $optimized_url = $rtn['optimized_url']; if (strlen($optimized_url)) { echo "Renaming original $img to ${img}_original \n"; rename($img, $img."_original"); } echo "Downloading $optimized_url ...\n"; $optimized_content = file_get_contents($optimized_url); echo "Saving optimised to $img ...\n"; file_put_contents($img, $optimized_content); } } foreach (array_slice($argv, 1) as $img) { if (!file_exists($img)) { echo $img . " not found.\n"; } else { ImageRecycle($img, $key, $secret); } } |
#!/usr/bin/php <?php // Author: https://helloacm.com $key = "app_key"; $secret = "app_secret"; function ImageRecycle($img, $key, $secret) { $cmd = "curl -X POST -F 'auth={\"key\":\"$key\",\"secret\":\"$secret\"}' -F 'file=@$img' -F 'params={\"compression_type\":\"lossy\"}' 'https://api.imagerecycle.com/v1/images'"; echo $cmd . "\n"; $rtn = json_decode(shell_exec($cmd), true); var_dump($rtn); $err = 201; if (isset($rtn['errCode'])) { $err = (integer)$rtn['errCode']; } $id = 0; if (isset($rtn['id'])) { $id = (integer)$rtn['id']; } if (($err == 201) || ($id > 0)) { $optimized_url = $rtn['optimized_url']; if (strlen($optimized_url)) { echo "Renaming original $img to ${img}_original \n"; rename($img, $img."_original"); } echo "Downloading $optimized_url ...\n"; $optimized_content = file_get_contents($optimized_url); echo "Saving optimised to $img ...\n"; file_put_contents($img, $optimized_content); } } foreach (array_slice($argv, 1) as $img) { if (!file_exists($img)) { echo $img . " not found.\n"; } else { ImageRecycle($img, $key, $secret); } }
Then, you have run a bash loop to iterate all images/PDF, which should give you instant saving.
I have managed to save over 2GB SSD space on VPS using the above script.
Join ImageRecycle and use the coupon IR-PARTNER-20 to get 20% off on first invoice, exclusive for my readers!
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Submit Sitemaps using PHP automatically?
Next Post: Comparing ImageRecycle and Imagfy - Which is Better?