TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. The effect is nearly invisible but it makes a very large difference in file size, more than 50% reduction in most cases.
TinyPNG offers a online service to integrate it into your workflow and compress all those PNG images on the fly. The service is free under registration for converting up to 500 images per month with no file size limit.
This patch adds a new service (tinypng.inc) to the last dev release of ImageAPI Optimize module. The TinyPNG service is configurable at admin/config/media/image-toolkit. The patch also makes a minor change in imageapi_optimize.module file for allowing descriptions in the services administration panel. It also include a required digital certificate from TinyPNG in order to authenticate for using their service. The certificate is not required to be installed in your machine, it is just read from imageapi_optimize/services directory by this patch.
TinyPNG compression ONLY APPLIES TO PNG files. If your image is uploaded in other format, TinyPNG will not process the image and the default Image Toolkit processing will apply.
Comments
Comment #1
interdruper CreditAttribution: interdruper commentedPatch is provided
If you want to process existing PNG files, remove their image styles with the command
drush image-flush --all
and they will be recreated using TinyPNG the 1st time they are called.Comment #2
interdruper CreditAttribution: interdruper commentedComment #3
jpstrikesback CreditAttribution: jpstrikesback commentedHere is a version without the certs and which sets CURLOPT_HEADER => true as is in tinypng API docs.
Comment #4
yannickooYou should wrap the text itself with t function.
A comma should be added after the line.
After comments should be a
.
,?
or!
.For one line comments you should use
//
instead of/* */
Comment #5
s.perilhou CreditAttribution: s.perilhou commentedBecause of CURLOPT_HEADER set to TRUE, headers are in output. In that case, $response can not be decode. CURLOPT_HEADER must be set to FALSE.
Works fine after this little tweak.
Comment #6
tim.weyand CreditAttribution: tim.weyand commentedComment #7
tim.weyand CreditAttribution: tim.weyand commentedComment #8
redndahead CreditAttribution: redndahead commentedSome whitespace cleanup
Comment #9
thepractice CreditAttribution: thepractice commentedRefactored patch to work with drush patch file.
Comment #10
thepractice CreditAttribution: thepractice commentedTypo in last patch.
Comment #11
thepractice CreditAttribution: thepractice commentedComment #12
redndahead CreditAttribution: redndahead commentedI tested this patch with a 10MB jpeg and 8MB png and it worked great with no issues.
Comment #13
kaidjohnson CreditAttribution: kaidjohnson commentedPatch #11 works great. Awesome feature addon! Thank you!
Comment #14
s_leu CreditAttribution: s_leu as a volunteer commentedThere's some bugs in the patch posted in #11 that leads to fatal errors. I fixed that in this new patch.
Comment #15
Jody LynnI'm using the patch in #14. Works great. Note that this works for jpgs as well as pngs.
Comment #16
Steven Jones CreditAttribution: Steven Jones at ComputerMinds commentedThanks for everyone's work on this, and the reviews.
I'm setting back to needs work, and raising the following points for discussion:
Rather than 'Enter your API key' this should be 'API key' or 'TinyPNG API key' if it's not clear from context.
I think this string should be split into two, and the '#description' property of the form element used.
We can then also remove the 'b' tags.
Seems a bit odd to not just use
form_error($element, '...');
is this done for a reason that I'm missing?This is fine, but may be clearer if we have:
!in_array($image->info['mime_type'], array('image/png', 'image/jpeg'))
Is that clearer? Discuss!
Is there a reason for using cURL here, and not using
drupal_http_request
.Although
drupal_http_request
is a mess, I feel like we should use the provided API, which has a way of being swapped out and tweaked if needed, rather than forcing people to have cURL and hack the module if they need to make tweaks, like certificate bundles etc.Drupal 7's requirements list the json extension, so while this is a nice to have a check here too, I don't think we should bother checking, we should just assume we have it.
Also, we should probably use
drupal_json_decode
because sometimes different PHP versions do return different things from json_decode.So sorry, there are some big things in there, but all up for discussion and further review.
Comment #17
thepractice CreditAttribution: thepractice at Third and Grove commentedI rolled all the changes suggested in #16 into this patch. I agree with all of them.
Comment #18
Steven Jones CreditAttribution: Steven Jones at ComputerMinds commented@thepractice thanks for the updated patch! Upon review, I've found a few more things :)
I don't have time to apply, tweak and commit, so have gone for the review for now, so that someone else could at least move this issue along:
We should mention where the statistics will be displayed (and below I mention changing it to the watchdog).
I don't think we need to return anything here.
I'm not sure this should be a
drupal_set_message
, the end users of the site don't need to see this message.Can we change all these to watchdog messages please?
The debug pair, can become a single watchdog message.
Comment #19
thepractice CreditAttribution: thepractice at Third and Grove commentedI've updated the patch from #17 to address the comments in #18.
1. We state that statistics will be displayed in log messages with a link to /admin/reports/dblog.
2. There is no need to return anything so I've removed the return statements.
3. I converted all the drupal messages to watchdog messages and fixed the first message so the t() paragraph marks close around the array of variables.
Comment #21
Steven Jones CreditAttribution: Steven Jones at ComputerMinds commentedThanks so much for the patch and the patience.
Fixed!