Slide 1

Slide 1 text

David Newton, St. Michael’s Hospital 1 1 Using ImageMagick to resize your Images GTA PHP User Group, 2015-04-07 Twitter: @newtron Github: @nwtn Email: [email protected] Slides + image credits: https://github.com/nwtn/pres-imagemagick-gtaphp

Slide 2

Slide 2 text

2

Slide 3

Slide 3 text

3 Responsive images are a pain in the butt

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

7 ImageMagick, y’all

Slide 8

Slide 8 text

8

Slide 9

Slide 9 text

9 $ brew install imagemagick

Slide 10

Slide 10 text

10 $ brew install php56-imagick

Slide 11

Slide 11 text

11 $ apt-get install php-imagick

Slide 12

Slide 12 text

12 Condition (unoptimized input) File size: mean File size: % difference Photoshop CC, with optimization 260,305 bytes ImageMagick `-resize` 385,795 bytes 48.21%

Slide 13

Slide 13 text

13 How does image resizing work?

Slide 14

Slide 14 text

14 685,936 pixels 189,739 pixels

Slide 15

Slide 15 text

15 …to 64 pixels? How do we get from 16 pixels…

Slide 16

Slide 16 text

16 != resampling

Slide 17

Slide 17 text

17 …to 64 pixels? How do we get from 16 pixels…

Slide 18

Slide 18 text

18 …to 64 pixels? How do we get from 16 pixels… Background interpolation

Slide 19

Slide 19 text

19 …to 64 pixels? How do we get from 16 pixels… Nearest-neighbour interpolation

Slide 20

Slide 20 text

20 Nearest-neighbour interpolation

Slide 21

Slide 21 text

21 Bicubic interpolation

Slide 22

Slide 22 text

22 Nearest-neighbour vs. bicubic interpolation

Slide 23

Slide 23 text

23 7 colours 1,146 colours

Slide 24

Slide 24 text

24

Slide 25

Slide 25 text

More colours = More bytes* 25

Slide 26

Slide 26 text

26 https://github.com/nwtn/image-resize-tests/

Slide 27

Slide 27 text

27 How do we know which options are best?

Slide 28

Slide 28 text

Quality objective: measured with DSSIM 28

Slide 29

Slide 29 text

29 Control Test Difference

Slide 30

Slide 30 text

30

Slide 31

Slide 31 text

Quality subjective: from me 31

Slide 32

Slide 32 text

Test a variety of images 32

Slide 33

Slide 33 text

33 The best* settings for ImageMagick

Slide 34

Slide 34 text

34 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB INPUT_PATH With optimization:

Slide 35

Slide 35 text

35 https://github.com/toy/image_optim https://github.com/ajslater/picopt https://imageoptim.com/

Slide 36

Slide 36 text

36 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH Without optimization:

Slide 37

Slide 37 text

37 mogrify vs. convert

Slide 38

Slide 38 text

38

Slide 39

Slide 39 text

39 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 40

Slide 40 text

40 resampling filter

Slide 41

Slide 41 text

41 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 42

Slide 42 text

42 Adaptive resize Distort resize Geometry Interpolative resize Liquid rescale Resize Sample Scale Thumbnail

Slide 43

Slide 43 text

43 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 44

Slide 44 text

44

Slide 45

Slide 45 text

45

Slide 46

Slide 46 text

46 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 47

Slide 47 text

47

Slide 48

Slide 48 text

48 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 49

Slide 49 text

49 Fancy

Slide 50

Slide 50 text

50 sharpening

Slide 51

Slide 51 text

51 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 52

Slide 52 text

52 -unsharp 0.25x0.25+8+0.045 radius sigma gain threshold

Slide 53

Slide 53 text

53 -unsharp 0.25x0.25+8+0.045 radius sigma gain threshold

Slide 54

Slide 54 text

54 colour reduction

Slide 55

Slide 55 text

55 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 56

Slide 56 text

56

Slide 57

Slide 57 text

57

Slide 58

Slide 58 text

58

Slide 59

Slide 59 text

59

Slide 60

Slide 60 text

60

Slide 61

Slide 61 text

61 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -colors NUMBER_OF_COLORS -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH If output file size is larger than input:

Slide 62

Slide 62 text

62 7 colours 1,146 colours

Slide 63

Slide 63 text

63 7 colours 7 colours

Slide 64

Slide 64 text

64 quality and compression

Slide 65

Slide 65 text

65 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 66

Slide 66 text

66 quality 10 quality 40 quality 70 quality 100

Slide 67

Slide 67 text

67 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 68

Slide 68 text

68 metadata

Slide 69

Slide 69 text

69 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 70

Slide 70 text

70 progressive rendering

Slide 71

Slide 71 text

71 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 72

Slide 72 text

72

Slide 73

Slide 73 text

73

Slide 74

Slide 74 text

Progressive JPEGs Good? Evil? 74

Slide 75

Slide 75 text

75 colour space

Slide 76

Slide 76 text

76 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH

Slide 77

Slide 77 text

77

Slide 78

Slide 78 text

78 The best* settings for ImageMagick

Slide 79

Slide 79 text

79 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB INPUT_PATH With optimization:

Slide 80

Slide 80 text

80 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8+0.065 -dither None - posterize 136 -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 - define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB -strip INPUT_PATH Without optimization:

Slide 81

Slide 81 text

81 Why even bother?

Slide 82

Slide 82 text

82 Condition (unoptimized input) File size: mean File size: % difference My settings, optimization 218,274 bytes My settings, no optimization 259,852 bytes 19.05%

Slide 83

Slide 83 text

83 Condition (unoptimized input) File size: mean File size: % difference My settings, optimization 218,274 bytes My settings, no optimization 259,852 bytes 19.05% Photoshop CC, optimization 260,305 bytes 19.28% Photoshop CC, no optimization 299,710 bytes 35.26%

Slide 84

Slide 84 text

84 Condition (unoptimized input) File size: mean File size: % difference My settings, optimization 218,274 bytes My settings, no optimization 259,852 bytes 19.05% Photoshop CC, optimization 260,305 bytes 19.28% Photoshop CC, no optimization 299,710 bytes 35.26% basic `-resize` / WordPress* 385,795 bytes 76.75%

Slide 85

Slide 85 text

85 Condition (unoptimized input) File size: mean File size: % difference My settings, optimization 218,274 bytes My settings, no optimization 259,852 bytes 19.05% Photoshop CC, optimization 260,305 bytes 19.28% Photoshop CC, no optimization 299,710 bytes 35.26% CodeIgniter / ExpressionEngine 340,461 bytes 55.98% TYPO3.CMS* 359,112 bytes 64.52% basic `-resize` / WordPress* 385,795 bytes 76.75% Drupal* 397,588 bytes 82.15% Perch* 416,790 bytes 90.95% Craft CMS* 425,259 bytes 94.83% grunt-responsive-images* 500,273 bytes 129.20%

Slide 86

Slide 86 text

86 PHP Imagick

Slide 87

Slide 87 text

87

Slide 88

Slide 88 text

88

Slide 89

Slide 89 text

89 CLI Imagick DSSIM File Size -filter N/A N/A N/A -define filter:support setOption() ? ? -thumbnail thumbnailImage() ✗ ✗ -unsharp unsharpMaskImage() ✔ ✔ -dither & -posterize posterizeImage() ✔ ✔ -quality setImageCompressionQuality() ✔ ✗ -define jpeg:fancy-upsampling setOption() ✔ ✔ -define png:compression-filter setOption() ✔ ✔ -define png:compression-level setOption() ✔ ✔ -define png:compression-strategy setOption() ✔ ✔ -define png:exclude-chunk setOption() ✔ ✔ -interlace setInterlaceScheme() ✔ ✔ -colorspace setColorspace() ✔ ✔ -strip stripImage() ✔ ✔

Slide 90

Slide 90 text

90

Slide 91

Slide 91 text

91 CLI Imagick DSSIM File Size -filter custom ✔ ✗ -define filter:support setOption() ✔ ✗ -thumbnail custom ✔ ✗ -unsharp unsharpMaskImage() ✔ ✔ -dither & -posterize posterizeImage() ✔ ✔ -quality setImageCompressionQuality() ✔ ✔ -define jpeg:fancy-upsampling setOption() ✔ ✔ -define png:compression-filter setOption() ✔ ✔ -define png:compression-level setOption() ✔ ✔ -define png:compression-strategy setOption() ✔ ✔ -define png:exclude-chunk setOption() ✔ ✔ -interlace setInterlaceScheme() ✔ ✔ -colorspace setColorspace() ✔ ✔ -strip stripImage() ✔ ✔

Slide 92

Slide 92 text

92 OK… but it’s still a pain in the butt

Slide 93

Slide 93 text

93 https://github.com/nwtn/php-respimg/ Resizing with smart defaults + more coming soon?

Slide 94

Slide 94 text

94

Slide 95

Slide 95 text

95 https://www.npmjs.com/package/grunt-respimg Resizing with smart defaults SVG to PNG rasterization Optimization

Slide 96

Slide 96 text

96 $ npm install grunt-respimg --save-dev

Slide 97

Slide 97 text

97

Slide 98

Slide 98 text

98

Slide 99

Slide 99 text

David Newton, St. Michael’s Hospital 99 Using ImageMagick to resize your Images GTA PHP User Group, 2015-04-07 Twitter: @newtron Github: @nwtn Email: [email protected] Slides + image credits: https://github.com/nwtn/pres-imagemagick-gtaphp