-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.patch
More file actions
793 lines (786 loc) · 21.8 KB
/
patch.patch
File metadata and controls
793 lines (786 loc) · 21.8 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
diff --git a/README.txt b/README.txt
index 6baf775..cd7e95c 100644
--- a/README.txt
+++ b/README.txt
@@ -1,78 +1,11 @@
-CONTENTS OF THIS FILE
----------------------
-
- * Introduction
- * Requirements
- * Installation
- * Configuration
- * Maintainers
-
-
-INTRODUCTION
-------------
-Provides TinyPNG integration for Drupal 7. This module creates a new
-ImageToolkit which uses other toolkits in the background. The secondary toolkit
-could be the core GD or ImageMagick toolkit provided by ImageMagick module.
+This module requires tinify package to work.
-In the background TinyPNG module just calls the functions of secondary toolkit
-but in the last step it sends the image to the TinyPNG API to compress it.
-
-What does TinyPNG do?
-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!
+1. Go to your site root directory and run "composer require tinify/tinify:1.4.*".
+2. Enable TinyPNG in the Drupal admin.
-Why should I use TinyPNG?
-PNG is useful because it's the only widely supported format that can store
-partially transparent images. The format uses compression, but the files can
-still be large. Use TinyPNG to shrink images for your apps and sites. It will
-use less bandwidth and load faster.
-For more information about TinyPNG please visit https://tinypng.com/.
+@todo
-
-REQUIREMENS
-------------
-Libraries module
-
-A TinyPNG API key
-You can request one for free at https://tinypng.com/developers.
-
-Tinify PHP library >= 1.3.0
-You can download it from https://github.com/tinify/tinify-php/
-
-
-INSTALLATION
-------------
-Download Tinify PHP library v1.3.0+ from https://github.com/tinify/tinify-php/
-and place it under $DRUPAL_ROOT/sites/all/libraries
-
-You can also install Tinify PHP with drush: just run drush tinypng-library.
-
-After Tinify PHP library installation the Tinfiy.php should be found
-at $DRUPAL_ROOT/sites/all/libraries/tinify-php/lib/Tinify.php
-
-Install as you would normally install a contributed Drupal module.
-See: https://drupal.org/documentation/install/modules-themes/modules-7
-for further information.
-
-INSTALLATION VIA DRUSH
-----------------------
-A Drush command is provided for easy installation of the Tinify-PHP library.
-
-drush tinypng-library
-The command will download the library and unpack it in "sites/all/libraries".
-
-CONFIGURATION
-------------
-Having installed the module, go to admin/config/media/image-toolkit page and
-select TinyPNG as default Image toolkit.
-Save the form. Then select the secondary toolkit.
-Save the form. Then setup the secondary toolkit too.
-
-
-MAINTAINERS
-------------
-Vince Tikász, https://www.drupal.org/u/tikaszvince
-István Csáki, https://www.drupal.org/u/csakiistvan
+1- Make the file size displayed after upload the compressed image size rather than the original.
+After refreshing the page the filesize is correctly displayed.
+2- Create a service to provide this module functionality for other modules to integrate.
diff --git a/include/filtersie.inc b/include/filtersie.inc
deleted file mode 100644
index 8c1ad4a..0000000
--- a/include/filtersie.inc
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-/**
- * @file
- * FiltersIE module related image effects.
- */
-
-/**
- * @ingroup image
- * @{
- */
-
-/**
- * Wrapper for default image_TOOLKIT_filtersie_convolution().
- */
-function image_tinypng_filtersie_convolution(stdClass $image, $matrix, $divisor, $offset) {
- return _tinypng_fallback('filtersie_convolution', $image, $matrix, $divisor, $offset);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_filtersie_UnsharpMask().
- */
-function image_tinypng_filtersie_UnsharpMask(stdClass $image, $amount, $radius, $threshold) {
- return _tinypng_fallback('filtersie_UnsharpMask', $image, $amount, $radius, $threshold);
-}
-
-/**
- * @} End of "addtogroup image".
- */
diff --git a/include/image.inc b/include/image.inc
deleted file mode 100644
index 81fbfe6..0000000
--- a/include/image.inc
+++ /dev/null
@@ -1,63 +0,0 @@
-<?php
-/**
- * @file
- * Image module related image effects.
- */
-
-/**
- * @ingroup image
- * @{
- */
-
-/**
- * Wrapper for default image_TOOLKIT_resize().
- */
-function image_tinypng_resize(stdClass $image, $width, $height) {
- return _tinypng_fallback('resize', $image, $width, $height);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_rotate().
- */
-function image_tinypng_rotate(stdClass $image, $degrees, $background = NULL) {
- return _tinypng_fallback('rotate', $image, $degrees, $background);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_crop().
- */
-function image_tinypng_crop(stdClass $image, $x, $y, $width, $height) {
- return _tinypng_fallback('crop', $image, $x, $y, $width, $height);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_desaturate().
- */
-function image_tinypng_desaturate(stdClass $image) {
- return _tinypng_fallback('desaturate', $image);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_load().
- */
-function image_tinypng_load(stdClass $image) {
- return _tinypng_fallback('load', $image);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_create_tmp().
- */
-function image_tinypng_create_tmp(stdClass $image, $width, $height) {
- return _tinypng_fallback('create_tmp', $image, $width, $height);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_get_info().
- */
-function image_tinypng_get_info(stdClass $image) {
- return _tinypng_fallback('get_info', $image);
-}
-
-/**
- * @} End of "addtogroup image".
- */
diff --git a/include/imagecache_actions.inc b/include/imagecache_actions.inc
deleted file mode 100644
index d4357d4..0000000
--- a/include/imagecache_actions.inc
+++ /dev/null
@@ -1,126 +0,0 @@
-<?php
-/**
- * @file
- * ImageCache Actions module related image effects.
- */
-
-/**
- * @ingroup image
- * @{
- */
-
-/**
- * Wrapper for default image_TOOLKIT_overlay().
- */
-function image_tinypng_overlay(stdClass $image, stdClass $layer, $x, $y, $alpha = 100, $reverse = FALSE) {
- return _tinypng_fallback('overlay', $image, $layer, $x, $y, $alpha, $reverse);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_imagecache_autorotate().
- */
-function image_tinypng_imagecache_autorotate(stdClass $image) {
- return _tinypng_fallback('imagecache_autorotate', $image);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_definecanvas().
- */
-function image_tinypng_definecanvas(stdClass $image, array $data) {
- return _tinypng_fallback('definecanvas', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_imagemask().
- */
-function image_tinypng_imagemask(stdClass $image, stdClass $mask) {
- return _tinypng_fallback('imagemask', $image, $mask);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_roundedcorners().
- */
-function image_tinypng_roundedcorners($image, $action) {
- return _tinypng_fallback('roundedcorners', $image, $action);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_adjustlevels().
- */
-function image_tinypng_adjustlevels(stdClass $image, array $data) {
- return _tinypng_fallback('adjustlevels', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_brightness().
- */
-function image_tinypng_brightness(stdClass $image, array $data) {
- return _tinypng_fallback('brightness', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_coloroverlay().
- */
-function image_tinypng_coloroverlay(stdClass $image, array $data) {
- return _tinypng_fallback('coloroverlay', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_colorshift().
- */
-function image_tinypng_colorshift(stdClass $image, array $data) {
- return _tinypng_fallback('colorshift', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_convert().
- */
-function image_tinypng_convert(stdClass $image, array $data) {
- return _tinypng_fallback('convert', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_desaturatealpha().
- */
-function image_tinypng_desaturatealpha(stdClass $image, array $data) {
- return _tinypng_fallback('desaturatealpha', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_invert().
- */
-function image_tinypng_invert(stdClass $image, array $data) {
- return _tinypng_fallback('invert', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_posterize().
- */
-function image_tinypng_posterize(stdClass $image, $colors) {
- return _tinypng_fallback('posterize', $image, $colors);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_image_effects_text().
- */
-function image_tinypng_image_effects_text(stdClass $image, array $data) {
- return _tinypng_fallback('image_effects_text', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_auto_orient().
- */
-function image_tinypng_auto_orient(stdClass $image, array $data) {
- return _tinypng_fallback('auto_orient', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_strip().
- */
-function image_tinypng_strip(stdClass $image) {
- return _tinypng_fallback('strip', $image);
-}
-
-/**
- * @} End of "addtogroup image".
- */
diff --git a/include/smartcrop.inc b/include/smartcrop.inc
deleted file mode 100644
index 6eb2cca..0000000
--- a/include/smartcrop.inc
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-/**
- * @file
- * Smartcrop module related image effects.
- */
-
-/**
- * @ingroup image
- * @{
- */
-
-/**
- * Wrapper for default image_TOOLKIT_smartcrop_scale().
- */
-function image_tinypng_smartcrop_scale(stdClass $image, $requested_x, $requested_y, $upscale) {
- return _tinypng_fallback('smartcrop_scale', $image, $requested_x, $requested_y, $upscale);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_smartcrop_crop().
- */
-function image_tinypng_smartcrop_crop(stdClass $image, $requested_x, $requested_y, $upscale) {
- return _tinypng_fallback('smartcrop_crop', $image, $requested_x, $requested_y, $upscale);
-}
-
-/**
- * @} End of "addtogroup image".
- */
diff --git a/include/textimage.inc b/include/textimage.inc
deleted file mode 100644
index ad19d81..0000000
--- a/include/textimage.inc
+++ /dev/null
@@ -1,84 +0,0 @@
-<?php
-/**
- * @file
- * Textimage (3.+) module related image effects.
- */
-
-/**
- * @ingroup image
- * @{
- */
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_get_font_path().
- */
-function image_tinypng_textimage_get_font_path($image, $data = array()) {
- return _tinypng_fallback('textimage_get_font_path', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_text_to_image().
- */
-function image_tinypng_textimage_text_to_image($image, $data = array()) {
- return _tinypng_fallback('textimage_text_to_image', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_get_bounding_box().
- */
-function image_tinypng_textimage_get_bounding_box($image, $data = array()) {
- return _tinypng_fallback('textimage_get_bounding_box', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_text_stroke().
- */
-function image_tinypng_textimage_text_stroke($image, $data = array()) {
- return _tinypng_fallback('textimage_text_stroke', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_draw_polygon().
- */
-function image_tinypng_textimage_draw_polygon($image, $data = array()) {
- return _tinypng_fallback('textimage_draw_polygon', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_draw_line().
- */
-function image_tinypng_textimage_draw_line($image, $data = array()) {
- return _tinypng_fallback('textimage_draw_line', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_draw_ellipse().
- */
-function image_tinypng_textimage_draw_ellipse($image, $data = array()) {
- return _tinypng_fallback('textimage_draw_ellipse', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_imagecolor().
- */
-function image_tinypng_textimage_imagecolor($image, $data = array()) {
- return _tinypng_fallback('textimage_imagecolor', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_create_transparent().
- */
-function image_tinypng_textimage_create_transparent(stdClass $image, $width, $height, $transparent = NULL) {
- return _tinypng_fallback('textimage_create_transparent', $image, $data);
-}
-
-/**
- * Wrapper for default image_TOOLKIT_textimage_set_transparency().
- */
-function image_tinypng_textimage_set_transparency(stdClass $image, $color) {
- return _tinypng_fallback('textimage_set_transparency', $image, $data);
-}
-
-/**
- * @} End of "addtogroup image".
- */
diff --git a/tinypng.drush.inc b/tinypng.drush.inc
deleted file mode 100644
index d144d4b..0000000
--- a/tinypng.drush.inc
+++ /dev/null
@@ -1,107 +0,0 @@
-<?php
-/**
- * @file
- * Drush integration for TinyPNG.
- */
-
-/**
- * The TinyPNG library URI.
- */
-define('TINIFY_PHP_VERSION', '1.3.0');
-define('TINIFY_PHP_DOWNLOAD_URI', 'https://github.com/tinify/tinify-php/archive/' . TINIFY_PHP_VERSION . '.zip');
-
-/**
- * Implements of hook_drush_command().
- */
-function tinypng_drush_command() {
- $items = array();
-
- // The key in the $items array is the name of the command.
- $items['tinypng-library'] = array(
- 'callback' => 'drush_tinypng_tinify_download',
- 'description' => dt('Download and install the Tinify PHP library.'),
- 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
- 'arguments' => array(
- 'path' => dt('Optional. A path where to install the Tinify PHP library. If omitted Drush will use the default location.'),
- ),
- );
-
- return $items;
-}
-
-/**
- * Implements of hook_drush_help().
- */
-function tinypng_drush_help($section) {
- switch ($section) {
- case 'drush:tinypng-library':
- return dt('Download and install the Tinify PHP library from https://github.com/tinify/tinify-php, default location is sites/all/libraries.');
- }
-}
-
-/**
- * Implements drush_MODULE_post_COMMAND().
- */
-function drush_tinypng_post_pm_enable() {
- $modules = func_get_args();
-
- // Download library if TinyPNG module has been enabled.
- if (in_array('tinypng', $modules)) {
- drush_tinypng_tinify_download();
- }
-}
-
-/**
- * Command to download the Tinify PHP Library.
- */
-function drush_tinypng_tinify_download() {
- $args = func_get_args();
- if (!empty($args[0])) {
- $path = $args[0];
- }
- else {
- $path = 'sites/all/libraries';
- }
-
- // Create the path if it does not exist.
- if (!is_dir($path)) {
- drush_op('mkdir', $path);
- drush_log(dt('Directory @path was created', array('@path' => $path)), 'notice');
- }
-
- // Set the directory to the download location.
- $olddir = getcwd();
- chdir($path);
-
- // Download the zip archive.
- if ($filepath = drush_download_file(TINIFY_PHP_DOWNLOAD_URI, TINIFY_PHP_VERSION . '.zip')) {
- $filename = basename($filepath);
- $dirname = basename($filepath, '.zip');
-
- // Remove any existing Tinify PHP library directory.
- if (is_dir($dirname) || is_dir(TINIFY_PHP_DIRNAME)) {
- drush_delete_dir($dirname, TRUE);
- drush_delete_dir(TINIFY_PHP_DIRNAME, TRUE);
- drush_log(dt('A existing Tinify PHP library was deleted from @path', array('@path' => $path)), 'notice');
- }
-
- // Decompress the zip archive.
- drush_tarball_extract($filename, $dirname);
- drush_move_dir(TINIFY_PHP_VERSION . '/' . TINIFY_PHP_DIRNAME . '-' . TINIFY_PHP_VERSION, TINIFY_PHP_DIRNAME, TRUE);
- drush_delete_dir(TINIFY_PHP_VERSION);
-
- // Delete the zip archive.
- @chmod($filename, 0777);
- unlink($filename);
- }
-
- if (is_dir(TINIFY_PHP_DIRNAME)) {
- drush_log(dt('Tinify PHP library has been installed in @path', array('@path' => $path)), 'success');
- }
- else {
- drush_log(dt('Drush was unable to install the Tinify PHP library to @path', array('@path' => $path)), 'error');
- }
-
- // Set working directory back to the previous working directory.
- chdir($olddir);
-}
diff --git a/tinypng.info b/tinypng.info
deleted file mode 100644
index 91684e9..0000000
--- a/tinypng.info
+++ /dev/null
@@ -1,5 +0,0 @@
-name = TinyPNG
-description = Provides TinyPNG integration.
-core = 7.x
-configure = admin/config/media/image-toolkit
-dependencies[] = libraries
diff --git a/tinypng.install b/tinypng.install
deleted file mode 100644
index e04da0c..0000000
--- a/tinypng.install
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-/**
- * @file
- * Contains install, update and uninstall functions for TinyPNG module.
- */
-
-/**
- * Implements hook_uninstall().
- */
-function tinypng_uninstall() {
- variable_del('tinypng_api_key');
- variable_del('tinypng_fallback_toolkit');
-}
diff --git a/tinypng.module b/tinypng.module
index 92e4226..67d943d 100644
--- a/tinypng.module
+++ b/tinypng.module
@@ -1,168 +1,38 @@
<?php
-/**
- * @file
- * Provides TinyPNG integration.
- */
-
-define('TINIFY_PHP_DIRNAME', 'tinify-php');
-
-include 'include/image.inc';
-if (module_exists('imagecache_actions')) {
- include 'include/imagecache_actions.inc';
-}
-if (module_exists('smartcrop')) {
- include 'include/smartcrop.inc';
-}
-if (module_exists('textimage')) {
- include 'include/textimage.inc';
-}
-if (module_exists('filtersie')) {
- include 'include/filtersie.inc';
-}
-
-/**
- * Implements hook_help().
- */
-function tinypng_help($path, $arg) {
- switch ($path) {
- case 'admin/help#tinypng':
- $output = file_get_contents(drupal_get_path('module', 'tinypng') . '/README.txt');
- return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>' . check_plain($output) . '</pre>';
- }
-}
-/**
- * @ingroup image
- * @{
- */
+use Drupal\Core\Entity\EntityInterface;
/**
- * Load TinyPNG library.
+ * Implements hook_entity_presave().
+ *
+ * Process the image with TinyPNG service.
*/
-function tinypng_load_library() {
- $dir = libraries_get_path(TINIFY_PHP_DIRNAME);
- if (!$dir) {
- drupal_set_message(t('You have to download Tinify PHP library from !link', array(
- '!link' => t('https://github.com/tinify/tinify-php/', 'https://github.com/tinify/tinify-php/'),
- )));
- }
+function tinypng_entity_presave(EntityInterface $entity) {
+ $config = \Drupal::config('tinypng.settings');
- $api_key = variable_get('tinypng_api_key');
- if (!$dir || !$api_key) {
- return FALSE;
- }
-
- $files = array(
- 'lib/Tinify.php',
- 'lib/Tinify/Client.php',
- 'lib/Tinify/Exception.php',
- 'lib/Tinify/ResultMeta.php',
- 'lib/Tinify/Result.php',
- 'lib/Tinify/Source.php',
- );
- foreach ($files as $file) {
- $path = $dir . '/' . $file;
- if (is_readable($path)) {
- require_once $path;
+ if ($entity->isNew() && $entity->getEntityTypeId() == 'file' && empty(file_validate_is_image($entity)) ) {
+ $config = \Drupal::config('tinypng.settings');
+ $api_key = $config->get('api_key');
+ // Skip if no api key is set.
+ if (empty($api_key)) {
+ return;
}
- }
-
- Tinify\setKey($api_key);
- return TRUE;
-}
-
-/**
- * Implements hook_image_toolkits().
- */
-function tinypng_image_toolkits() {
- return array(
- 'tinypng' => array(
- 'title' => t('TinyPNG'),
- 'available' => TRUE,
- ),
- );
-}
-
-/**
- * Retrieve settings for the TinyPNG toolkit.
- */
-function image_tinypng_settings() {
- $form['tinypng'] = array(
- '#type' => 'fieldset',
- '#title' => t('TinyPNG'),
- '#collapsible' => FALSE,
- '#description' => t('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!'),
- );
-
- $form['tinypng']['tinypng_api_key'] = array(
- '#type' => 'textfield',
- '#title' => t('TinyPNG API key'),
- '#default_value' => variable_get('tinypng_api_key'),
- '#description' => t('Get your API key at !link.', array(
- '!link' => l('https://tinypng.com/developers', 'https://tinypng.com/developers'),
- )),
- );
-
- $toolkits_available = image_get_available_toolkits();
- $fallback = variable_get('tinypng_fallback_toolkit', 'gd');
- unset($toolkits_available['tinypng']);
- $form['tinypng']['tinypng_fallback_toolkit'] = array(
- '#type' => 'select',
- '#title' => t('Fallback image toolkit'),
- '#default_value' => $fallback,
- '#options' => $toolkits_available,
- );
-
-
- // Get the toolkit's settings form.
- $function = 'image_' . $fallback . '_settings';
- if (function_exists($function)) {
- $form['tinypng']['fallback_toolkit_settings'] = $function();
- }
-
- return $form;
-}
-
-/**
- * Wrapper for image toolkit functions.
- */
-function _tinypng_fallback($method) {
- $fallback = variable_get('tinypng_fallback_toolkit', 'gd');
- $args = func_get_args();
- $method = array_shift($args);
- $function = 'image_' . $fallback . '_' . $method;
- if (is_callable($function)) {
- return call_user_func_array($function, $args);
- }
- return FALSE;
-}
-
-/**
- * GD helper to write an image resource to a destination file.
- *
- * @param $image
- * An image object.
- * @param $destination
- * A string file URI or path where the image should be saved.
- * @return
- * TRUE or FALSE, based on success.
- *
- * @see image_save()
- */
-function image_tinypng_save(stdClass $image, $destination) {
- $res = _tinypng_fallback('save', $image, $destination);;
- try {
- if (tinypng_load_library()) {
- $dest = drupal_realpath($destination);
- $res = Tinify\fromFile($dest)->toFile($dest);
+ // Compress image.
+ $image_path = $entity->getFileUri();
+ \Tinify\setKey($api_key);
+ $dest = drupal_realpath($image_path);
+
+ try {
+ if ($config->get('integration_method') == 'download') {
+ $origin = file_create_url($image_path);
+ $res = \Tinify\fromUrl($origin, $dest);
+ }
+ else {
+ $res = Tinify\fromFile($dest)->toFile($dest);
+ }
+ }
+ catch (Exception $ex) {
+ \Drupal::logger('tinypng')->error($ex->getMessage());
}
}
- catch (Exception $e) {
- watchdog('tinypng', $e->getMessage(), array(), WATCHDOG_ERROR);
- }
- return $res;
}
-
-/**
- * @} End of "addtogroup image".
- */