Introduction to Imagine

Introduction to Imagine

Imagine is a image processing library for PHP 5.3, which changes the way image processing is done

F000c9b4dd0656f60de1dc9e75f7386c?s=128

Bulat Shakirzyanov

October 16, 2011
Tweet

Transcript

  1. 3.

    There are many existing tools • GD • Imagick (ImageMagick

    extension) • Gmagick (GraphicsMagick extension) • Cairo http://www.imagemagick.org/ http://www.graphicsmagick.org/ Sunday, October 16, 11
  2. 4.

    • not testable • inconsistent • have cluttered apis •

    not intuitive Existing tools Sunday, October 16, 11
  3. 5.

    Resize in GD <?php // open original and memorize dimensions

    $src = imagecreatefrompng('/path/to/image.png'); $srcWidth = imagesx($src); $srcHeight = imagesy($src); // desclare target dimensions $destWidth = //target width $destHeight = //target height // create target canvas $dest = imagecreatetruecolor($destWidth, $destHeight); // keep transparency imagealphablending($dest, false); imagesavealpha($dest, true); // stretch image to fit target size // and put it on target canvas imagecopyresampled( $dest, $src, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight ); // save image on filesystem as png imagepng($dest,'/path/to/resized/image.png'); Sunday, October 16, 11
  4. 6.

    Resize in GD <?php // open original and memorize dimensions

    $src = imagecreatefrompng('/path/to/image.png'); $srcWidth = imagesx($src); $srcHeight = imagesy($src); // desclare target dimensions $destWidth = //target width $destHeight = //target height // create target canvas $dest = imagecreatetruecolor($destWidth, $destHeight); // keep transparency imagealphablending($dest, false); imagesavealpha($dest, true); // stretch image to fit target size // and put it on target canvas imagecopyresampled( $dest, $src, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight ); // save image on filesystem as png imagepng($dest,'/path/to/resized/image.png'); Sunday, October 16, 11
  5. 7.

    Resize in GD <?php // open original and memorize dimensions

    $src = imagecreatefrompng('/path/to/image.png'); $srcWidth = imagesx($src); $srcHeight = imagesy($src); // desclare target dimensions $destWidth = //target width $destHeight = //target height // create target canvas $dest = imagecreatetruecolor($destWidth, $destHeight); // keep transparency imagealphablending($dest, false); imagesavealpha($dest, true); // stretch image to fit target size // and put it on target canvas imagecopyresampled( $dest, $src, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight ); // save image on filesystem as png imagepng($dest,'/path/to/resized/image.png'); Sunday, October 16, 11
  6. 8.

    Resize in GD <?php // open original and memorize dimensions

    $src = imagecreatefrompng('/path/to/image.png'); $srcWidth = imagesx($src); $srcHeight = imagesy($src); // desclare target dimensions $destWidth = //target width $destHeight = //target height // create target canvas $dest = imagecreatetruecolor($destWidth, $destHeight); // keep transparency imagealphablending($dest, false); imagesavealpha($dest, true); // stretch image to fit target size // and put it on target canvas imagecopyresampled( $dest, $src, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight ); // save image on filesystem as png imagepng($dest,'/path/to/resized/image.png'); Sunday, October 16, 11
  7. 9.

    Resize in GD <?php // open original and memorize dimensions

    $src = imagecreatefrompng('/path/to/image.png'); $srcWidth = imagesx($src); $srcHeight = imagesy($src); // desclare target dimensions $destWidth = //target width $destHeight = //target height // create target canvas $dest = imagecreatetruecolor($destWidth, $destHeight); // keep transparency imagealphablending($dest, false); imagesavealpha($dest, true); // stretch image to fit target size // and put it on target canvas imagecopyresampled( $dest, $src, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight ); // save image on filesystem as png imagepng($dest,'/path/to/resized/image.png'); Sunday, October 16, 11
  8. 10.

    Resize in GD <?php // open original and memorize dimensions

    $src = imagecreatefrompng('/path/to/image.png'); $srcWidth = imagesx($src); $srcHeight = imagesy($src); // desclare target dimensions $destWidth = //target width $destHeight = //target height // create target canvas $dest = imagecreatetruecolor($destWidth, $destHeight); // keep transparency imagealphablending($dest, false); imagesavealpha($dest, true); // stretch image to fit target size // and put it on target canvas imagecopyresampled( $dest, $src, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight ); // save image on filesystem as png imagepng($dest,'/path/to/resized/image.png'); Sunday, October 16, 11
  9. 11.

    Resize in GD <?php // open original and memorize dimensions

    $src = imagecreatefrompng('/path/to/image.png'); $srcWidth = imagesx($src); $srcHeight = imagesy($src); // desclare target dimensions $destWidth = //target width $destHeight = //target height // create target canvas $dest = imagecreatetruecolor($destWidth, $destHeight); // keep transparency imagealphablending($dest, false); imagesavealpha($dest, true); // stretch image to fit target size // and put it on target canvas imagecopyresampled( $dest, $src, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight ); // save image on filesystem as png imagepng($dest,'/path/to/resized/image.png'); Sunday, October 16, 11
  10. 12.

    Resize in Imagick <?php // declare target dimensions $destWidth =

    //target width $destHeight = //target height // open image $image = new Imagick('/path/to/image.png'); // resize, constraining proportions $image->adaptiveResizeImage($width, $height); // write to disk $image->writeImage('/path/to/resized/image.png'); Sunday, October 16, 11
  11. 13.

    Resize in Imagick <?php // declare target dimensions $destWidth =

    //target width $destHeight = //target height // open image $image = new Imagick('/path/to/image.png'); // resize, constraining proportions $image->adaptiveResizeImage($width, $height); // write to disk $image->writeImage('/path/to/resized/image.png'); Sunday, October 16, 11
  12. 14.

    Resize in Imagick <?php // declare target dimensions $destWidth =

    //target width $destHeight = //target height // open image $image = new Imagick('/path/to/image.png'); // resize, constraining proportions $image->adaptiveResizeImage($width, $height); // write to disk $image->writeImage('/path/to/resized/image.png'); Sunday, October 16, 11
  13. 15.

    Resize in Imagick <?php // declare target dimensions $destWidth =

    //target width $destHeight = //target height // open image $image = new Imagick('/path/to/image.png'); // resize, constraining proportions $image->adaptiveResizeImage($width, $height); // write to disk $image->writeImage('/path/to/resized/image.png'); Sunday, October 16, 11
  14. 16.

    Resize in Imagick <?php // declare target dimensions $destWidth =

    //target width $destHeight = //target height // open image $image = new Imagick('/path/to/image.png'); // resize, constraining proportions $image->adaptiveResizeImage($width, $height); // write to disk $image->writeImage('/path/to/resized/image.png'); Sunday, October 16, 11
  15. 18.

    Imagine... • all drivers implemented the same interfaces • code

    could be reused with any driver • there were interfaces for mocking in tests • API was simple and intuitive Sunday, October 16, 11
  16. 22.

    Resize in Imagine (GD) <?php // declare target dimensions $width

    = //target width $height = //target height // get Imagine instance for GD $imagine = new Imagine\Gd\Imagine(); // resize and save (format is inferred from extension) $imagine->open('/path/to/image.png') ->resize(new Imagine\Image\Box($width, $height)) ->save('/path/to/resized/image.png'); Sunday, October 16, 11
  17. 23.

    Resize in Imagine (Imagick) <?php // declare target dimensions $width

    = //target width $height = //target height // get Imagine instance for Imagick $imagine = new Imagine\Imagick\Imagine(); // resize and save (format is inferred from extension) $imagine->open('/path/to/image.png') ->resize(new Imagine\Image\Box($width, $height)) ->save('/path/to/resized/image.png'); Sunday, October 16, 11
  18. 25.

    Operations • resize • rotate • crop • save •

    copy • paste • apply mask • ellipse • polygon • line • dot • arc • pie slice • text Sunday, October 16, 11
  19. 26.

    manipulations Operations • resize • rotate • crop • save

    • copy • paste • apply mask • ellipse • polygon • line • dot • arc • pie slice • text Sunday, October 16, 11
  20. 27.

    Operations • resize • rotate • crop • save •

    copy • paste • apply mask • ellipse • polygon • line • dot • arc • pie slice • text drawings Sunday, October 16, 11
  21. 30.

    Thumbnail <?php $imagine = new Imagine\Gd\Imagine(); $mode = Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND; //or

    $mode = Imagine\Image\ImageInterface::THUMBNAIL_INSET; $imagine->open('/path/to/google/logo.png') ->thumbnail(new Imagine\Image\Box(100, 100), $mode) ->save('/path/to/google/logo/thumbnail.png'); Sunday, October 16, 11
  22. 32.

    Reflection <?php $imagine = new Imagine\Gd\Imagine(); $logo = $imagine->open('/path/to/google/logo.png'); $size

    = $logo->getSize(); $canvas = $imagine->create( new Imagine\Image\Box($size->getWidth(), $size->getHeight() * 2), new Imagine\Image\Color('000', 100) ); $reflection = $logo->copy() ->flipVertically() ->applyMask( $imagine->create($size) ->fill( new Imagine\Image\Fill\Gradient\Vertical( $size->getHeight(), new Imagine\Image\Color(array(127, 127, 127)), new Imagine\Image\Color('fff') ) ) ); $canvas->paste($logo, new Imagine\Image\Point(0, 0)) ->paste($reflection, new Imagine\Image\Point(0, $size->getHeight())) ->save('/path/to/google/logo/reflection.png'); Sunday, October 16, 11
  23. 33.

    Reflection <?php $imagine = new Imagine\Gd\Imagine(); $logo = $imagine->open('/path/to/google/logo.png'); $size

    = $logo->getSize(); $canvas = $imagine->create( new Imagine\Image\Box($size->getWidth(), $size->getHeight() * 2), new Imagine\Image\Color('000', 100) ); $reflection = $logo->copy() ->flipVertically() ->applyMask( $imagine->create($size) ->fill( new Imagine\Image\Fill\Gradient\Vertical( $size->getHeight(), new Imagine\Image\Color(array(127, 127, 127)), new Imagine\Image\Color('fff') ) ) ); $canvas->paste($logo, new Imagine\Image\Point(0, 0)) ->paste($reflection, new Imagine\Image\Point(0, $size->getHeight())) ->save('/path/to/google/logo/reflection.png'); open image to reflect and remember its size Sunday, October 16, 11
  24. 34.

    Reflection <?php $imagine = new Imagine\Gd\Imagine(); $logo = $imagine->open('/path/to/google/logo.png'); $size

    = $logo->getSize(); $canvas = $imagine->create( new Imagine\Image\Box($size->getWidth(), $size->getHeight() * 2), new Imagine\Image\Color('000', 100) ); $reflection = $logo->copy() ->flipVertically() ->applyMask( $imagine->create($size) ->fill( new Imagine\Image\Fill\Gradient\Vertical( $size->getHeight(), new Imagine\Image\Color(array(127, 127, 127)), new Imagine\Image\Color('fff') ) ) ); $canvas->paste($logo, new Imagine\Image\Point(0, 0)) ->paste($reflection, new Imagine\Image\Point(0, $size->getHeight())) ->save('/path/to/google/logo/reflection.png'); create empty canvas to fit image and reflection Sunday, October 16, 11
  25. 35.

    Reflection <?php $imagine = new Imagine\Gd\Imagine(); $logo = $imagine->open('/path/to/google/logo.png'); $size

    = $logo->getSize(); $canvas = $imagine->create( new Imagine\Image\Box($size->getWidth(), $size->getHeight() * 2), new Imagine\Image\Color('000', 100) ); $reflection = $logo->copy() ->flipVertically() ->applyMask( $imagine->create($size) ->fill( new Imagine\Image\Fill\Gradient\Vertical( $size->getHeight(), new Imagine\Image\Color(array(127, 127, 127)), new Imagine\Image\Color('fff') ) ) ); $canvas->paste($logo, new Imagine\Image\Point(0, 0)) ->paste($reflection, new Imagine\Image\Point(0, $size->getHeight())) ->save('/path/to/google/logo/reflection.png'); make a copy of source, flipped vertically Sunday, October 16, 11
  26. 36.

    <?php $imagine = new Imagine\Gd\Imagine(); $logo = $imagine->open('/path/to/google/logo.png'); $size =

    $logo->getSize(); $canvas = $imagine->create( new Imagine\Image\Box($size->getWidth(), $size->getHeight() * 2), new Imagine\Image\Color('000', 100) ); $reflection = $logo->copy() ->flipVertically() ->applyMask( $imagine->create($size) ->fill( new Imagine\Image\Fill\Gradient\Vertical( $size->getHeight(), new Imagine\Image\Color(array(127, 127, 127)), new Imagine\Image\Color('fff') ) ) ); $canvas->paste($logo, new Imagine\Image\Point(0, 0)) ->paste($reflection, new Imagine\Image\Point(0, $size->getHeight())) ->save('/path/to/google/logo/reflection.png'); Reflection create image like the one above Sunday, October 16, 11
  27. 37.

    <?php $imagine = new Imagine\Gd\Imagine(); $logo = $imagine->open('/path/to/google/logo.png'); $size =

    $logo->getSize(); $canvas = $imagine->create( new Imagine\Image\Box($size->getWidth(), $size->getHeight() * 2), new Imagine\Image\Color('000', 100) ); $reflection = $logo->copy() ->flipVertically() ->applyMask( $imagine->create($size) ->fill( new Imagine\Image\Fill\Gradient\Vertical( $size->getHeight(), new Imagine\Image\Color(array(127, 127, 127)), new Imagine\Image\Color('fff') ) ) ); $canvas->paste($logo, new Imagine\Image\Point(0, 0)) ->paste($reflection, new Imagine\Image\Point(0, $size->getHeight())) ->save('/path/to/google/logo/reflection.png'); Reflection replace white regions with transparency Sunday, October 16, 11
  28. 38.

    <?php $imagine = new Imagine\Gd\Imagine(); $logo = $imagine->open('/path/to/google/logo.png'); $size =

    $logo->getSize(); $canvas = $imagine->create( new Imagine\Image\Box($size->getWidth(), $size->getHeight() * 2), new Imagine\Image\Color('000', 100) ); $reflection = $logo->copy() ->flipVertically() ->applyMask( $imagine->create($size) ->fill( new Imagine\Image\Fill\Gradient\Vertical( $size->getHeight(), new Imagine\Image\Color(array(127, 127, 127)), new Imagine\Image\Color('fff') ) ) ); $canvas->paste($logo, new Imagine\Image\Point(0, 0)) ->paste($reflection, new Imagine\Image\Point(0, $size->getHeight())) ->save('/path/to/google/logo/reflection.png'); Reflection place original logo on top of created canvas place reflection underneath it Sunday, October 16, 11
  29. 40.

    Piechart <?php $imagine = new Imagine\Imagick\Imagine(); $volume = 20; $size

    = new Imagine\Image\Box(300, 200); $center = new Imagine\Image\Point\Center($size); $canvas = $size->increase($volume); $bg = new Imagine\Image\Color('000000', 100); $color1 = new Imagine\Image\Color('FFEF78'); $color2 = new Imagine\Image\Color('8A834B'); $color3 = new Imagine\Image\Color('8A554B'); $color4 = new Imagine\Image\Color('D94616'); $color5 = new Imagine\Image\Color('FEB48D'); $chart = $imagine->create($canvas, $bg); for ($i = $volume; $i > 0; $i--) { $shift = $center->move($i); $chart->draw() ->pieSlice($shift, $size, -10, 70, $color1->darken(68), true) ->pieSlice($shift, $size, 70, 160, $color2->darken(68), true) ->pieSlice($shift, $size, 160, 170, $color3->darken(68), true) ->pieSlice($shift, $size, 170, 210, $color4->darken(68), true) ->pieSlice($shift, $size, 210, 350, $color5->darken(68), true); } $chart->draw() ->pieSlice($center, $size, -10, 70, $color1, true) ->pieSlice($center, $size, 70, 160, $color2, true) ->pieSlice($center, $size, 160, 170, $color3, true) ->pieSlice($center, $size, 170, 210, $color4, true) ->pieSlice($center, $size, 210, 350, $color5, true); $chart->save('/path/to/chart.png'); Sunday, October 16, 11
  30. 41.

    Piechart <?php $imagine = new Imagine\Imagick\Imagine(); $volume = 20; $size

    = new Imagine\Image\Box(300, 200); $center = new Imagine\Image\Point\Center($size); $canvas = $size->increase($volume); $bg = new Imagine\Image\Color('000000', 100); $color1 = new Imagine\Image\Color('FFEF78'); $color2 = new Imagine\Image\Color('8A834B'); $color3 = new Imagine\Image\Color('8A554B'); $color4 = new Imagine\Image\Color('D94616'); $color5 = new Imagine\Image\Color('FEB48D'); $chart = $imagine->create($canvas, $bg); for ($i = $volume; $i > 0; $i--) { $shift = $center->move($i); $chart->draw() ->pieSlice($shift, $size, -10, 70, $color1->darken(68), true) ->pieSlice($shift, $size, 70, 160, $color2->darken(68), true) ->pieSlice($shift, $size, 160, 170, $color3->darken(68), true) ->pieSlice($shift, $size, 170, 210, $color4->darken(68), true) ->pieSlice($shift, $size, 210, 350, $color5->darken(68), true); } $chart->draw() ->pieSlice($center, $size, -10, 70, $color1, true) ->pieSlice($center, $size, 70, 160, $color2, true) ->pieSlice($center, $size, 160, 170, $color3, true) ->pieSlice($center, $size, 170, 210, $color4, true) ->pieSlice($center, $size, 210, 350, $color5, true); $chart->save('/path/to/chart.png'); get imagine, define chart 3d volume and size Sunday, October 16, 11
  31. 42.

    Piechart <?php $imagine = new Imagine\Imagick\Imagine(); $volume = 20; $size

    = new Imagine\Image\Box(300, 200); $center = new Imagine\Image\Point\Center($size); $canvas = $size->increase($volume); $bg = new Imagine\Image\Color('000000', 100); $color1 = new Imagine\Image\Color('FFEF78'); $color2 = new Imagine\Image\Color('8A834B'); $color3 = new Imagine\Image\Color('8A554B'); $color4 = new Imagine\Image\Color('D94616'); $color5 = new Imagine\Image\Color('FEB48D'); $chart = $imagine->create($canvas, $bg); for ($i = $volume; $i > 0; $i--) { $shift = $center->move($i); $chart->draw() ->pieSlice($shift, $size, -10, 70, $color1->darken(68), true) ->pieSlice($shift, $size, 70, 160, $color2->darken(68), true) ->pieSlice($shift, $size, 160, 170, $color3->darken(68), true) ->pieSlice($shift, $size, 170, 210, $color4->darken(68), true) ->pieSlice($shift, $size, 210, 350, $color5->darken(68), true); } $chart->draw() ->pieSlice($center, $size, -10, 70, $color1, true) ->pieSlice($center, $size, 70, 160, $color2, true) ->pieSlice($center, $size, 160, 170, $color3, true) ->pieSlice($center, $size, 170, 210, $color4, true) ->pieSlice($center, $size, 210, 350, $color5, true); $chart->save('/path/to/chart.png'); get center of the chart account for size of 3d volume in canvas Sunday, October 16, 11
  32. 43.

    Piechart <?php $imagine = new Imagine\Imagick\Imagine(); $volume = 20; $size

    = new Imagine\Image\Box(300, 200); $center = new Imagine\Image\Point\Center($size); $canvas = $size->increase($volume); $bg = new Imagine\Image\Color('000000', 100); $color1 = new Imagine\Image\Color('FFEF78'); $color2 = new Imagine\Image\Color('8A834B'); $color3 = new Imagine\Image\Color('8A554B'); $color4 = new Imagine\Image\Color('D94616'); $color5 = new Imagine\Image\Color('FEB48D'); $chart = $imagine->create($canvas, $bg); for ($i = $volume; $i > 0; $i--) { $shift = $center->move($i); $chart->draw() ->pieSlice($shift, $size, -10, 70, $color1->darken(68), true) ->pieSlice($shift, $size, 70, 160, $color2->darken(68), true) ->pieSlice($shift, $size, 160, 170, $color3->darken(68), true) ->pieSlice($shift, $size, 170, 210, $color4->darken(68), true) ->pieSlice($shift, $size, 210, 350, $color5->darken(68), true); } $chart->draw() ->pieSlice($center, $size, -10, 70, $color1, true) ->pieSlice($center, $size, 70, 160, $color2, true) ->pieSlice($center, $size, 160, 170, $color3, true) ->pieSlice($center, $size, 170, 210, $color4, true) ->pieSlice($center, $size, 210, 350, $color5, true); $chart->save('/path/to/chart.png'); colors of pie slices and background Sunday, October 16, 11
  33. 44.

    Piechart <?php $imagine = new Imagine\Imagick\Imagine(); $volume = 20; $size

    = new Imagine\Image\Box(300, 200); $center = new Imagine\Image\Point\Center($size); $canvas = $size->increase($volume); $bg = new Imagine\Image\Color('000000', 100); $color1 = new Imagine\Image\Color('FFEF78'); $color2 = new Imagine\Image\Color('8A834B'); $color3 = new Imagine\Image\Color('8A554B'); $color4 = new Imagine\Image\Color('D94616'); $color5 = new Imagine\Image\Color('FEB48D'); $chart = $imagine->create($canvas, $bg); for ($i = $volume; $i > 0; $i--) { $shift = $center->move($i); $chart->draw() ->pieSlice($shift, $size, -10, 70, $color1->darken(68), true) ->pieSlice($shift, $size, 70, 160, $color2->darken(68), true) ->pieSlice($shift, $size, 160, 170, $color3->darken(68), true) ->pieSlice($shift, $size, 170, 210, $color4->darken(68), true) ->pieSlice($shift, $size, 210, 350, $color5->darken(68), true); } $chart->draw() ->pieSlice($center, $size, -10, 70, $color1, true) ->pieSlice($center, $size, 70, 160, $color2, true) ->pieSlice($center, $size, 160, 170, $color3, true) ->pieSlice($center, $size, 170, 210, $color4, true) ->pieSlice($center, $size, 210, 350, $color5, true); $chart->save('/path/to/chart.png'); create chart canvas with transparent background Sunday, October 16, 11
  34. 45.

    Piechart <?php $imagine = new Imagine\Imagick\Imagine(); $volume = 20; $size

    = new Imagine\Image\Box(300, 200); $center = new Imagine\Image\Point\Center($size); $canvas = $size->increase($volume); $bg = new Imagine\Image\Color('000000', 100); $color1 = new Imagine\Image\Color('FFEF78'); $color2 = new Imagine\Image\Color('8A834B'); $color3 = new Imagine\Image\Color('8A554B'); $color4 = new Imagine\Image\Color('D94616'); $color5 = new Imagine\Image\Color('FEB48D'); $chart = $imagine->create($canvas, $bg); for ($i = $volume; $i > 0; $i--) { $shift = $center->move($i); $chart->draw() ->pieSlice($shift, $size, -10, 70, $color1->darken(68), true) ->pieSlice($shift, $size, 70, 160, $color2->darken(68), true) ->pieSlice($shift, $size, 160, 170, $color3->darken(68), true) ->pieSlice($shift, $size, 170, 210, $color4->darken(68), true) ->pieSlice($shift, $size, 210, 350, $color5->darken(68), true); } $chart->draw() ->pieSlice($center, $size, -10, 70, $color1, true) ->pieSlice($center, $size, 70, 160, $color2, true) ->pieSlice($center, $size, 160, 170, $color3, true) ->pieSlice($center, $size, 170, 210, $color4, true) ->pieSlice($center, $size, 210, 350, $color5, true); $chart->save('/path/to/chart.png'); build 3d shade of the chart in darker colors Sunday, October 16, 11
  35. 46.

    Piechart <?php $imagine = new Imagine\Imagick\Imagine(); $volume = 20; $size

    = new Imagine\Image\Box(300, 200); $center = new Imagine\Image\Point\Center($size); $canvas = $size->increase($volume); $bg = new Imagine\Image\Color('000000', 100); $color1 = new Imagine\Image\Color('FFEF78'); $color2 = new Imagine\Image\Color('8A834B'); $color3 = new Imagine\Image\Color('8A554B'); $color4 = new Imagine\Image\Color('D94616'); $color5 = new Imagine\Image\Color('FEB48D'); $chart = $imagine->create($canvas, $bg); for ($i = $volume; $i > 0; $i--) { $shift = $center->move($i); $chart->draw() ->pieSlice($shift, $size, -10, 70, $color1->darken(68), true) ->pieSlice($shift, $size, 70, 160, $color2->darken(68), true) ->pieSlice($shift, $size, 160, 170, $color3->darken(68), true) ->pieSlice($shift, $size, 170, 210, $color4->darken(68), true) ->pieSlice($shift, $size, 210, 350, $color5->darken(68), true); } $chart->draw() ->pieSlice($center, $size, -10, 70, $color1, true) ->pieSlice($center, $size, 70, 160, $color2, true) ->pieSlice($center, $size, 160, 170, $color3, true) ->pieSlice($center, $size, 170, 210, $color4, true) ->pieSlice($center, $size, 210, 350, $color5, true); $chart->save('/path/to/chart.png'); draw and save the actual chart Sunday, October 16, 11
  36. 48.

    Color <?php $color = new Imagine\Image\Color('000'); $color->lighten(127); $color->dissolve(50); $color->lighten(127)->dissolve(50); $color->getRed()

    $color->getGreen() $color->getBlue() $color->getAlpha() $color->isOpaque() 0 0 0 0 true Sunday, October 16, 11
  37. 51.

    Make it testable 1. interface end user code interactions 2.

    close unexpected inheritance Sunday, October 16, 11
  38. 53.

    Filter <?php namespace Imagine\Filter; use Imagine\Image\ImageInterface; interface FilterInterface { /**

    * Applies scheduled transformation to ImageInterface instance * Returns processed ImageInterface instance * * @param Imagine\Image\ImageInterface $image * * @return Imagine\Image\ImageInterface */ function apply(ImageInterface $image); } Sunday, October 16, 11
  39. 54.

    Filters Filter is a collection of manipulations, calculations and other

    operations, that can be applied to an image Sunday, October 16, 11
  40. 55.

    Reflection filter <?php class ReflectionFilter implements Imagine\Filter\FilterInterface { private $imagine;

    public function __construct(Imagine\Image\ImagineInterface $imagine) { $this->imagine = $imagine; } public function apply(Imagine\Image\ImageInterface $image) { $size = $image->getSize(); $canvas = new Imagine\Image\Box($size->getWidth(), $size->getHeight() * 2); $reflection = $image->copy() ->flipVertically() ->applyMask($this->getTransparencyMask($size)) ; return $this->imagine->create($canvas, new Imagine\Image\Color('fff', 100)) ->paste($image, new Imagine\Image\Point(0, 0)) ->paste($reflection, new Imagine\Image\Point(0, $size->getHeight())); } private function getTransparencyMask(Imagine\Image\BoxInterface $size) { $white = new Imagine\Image\Color('fff'); $fill = new Imagine\Image\Fill\Gradient\Vertical( $size->getHeight(), $white->darken(127), $white ); return $this->imagine->create($size) ->fill($fill) ; } } Sunday, October 16, 11
  41. 56.

    Reflection filter <?php $imagine = new Imagine\Gd\Imagine(); $filter = new

    ReflectionFilter($imagine); $filter->apply($imagine->open('/path/to/google/logo.png')) ->save('/path/to/google/logo/reflection.png'); Sunday, October 16, 11
  42. 58.

    Transformation operate on a transformation as on a regular image,

    except nothing is being executed <?php $path = '/path/to/processed/image.png'; $size = new Imagine\Image\Box(50, 50); $resize = new Imagine\Image\Box(200, 200); $angle = 90; $background = new Imagine\Image\Color('fff'); $transformation = new Imagine\Filter\Transformation(); $transformation->resize($resize) ->copy() ->rotate($angle, $background) ->thumbnail($size, Imagine\Image\ImageInterface::THUMBNAIL_INSET) ->save($path); Sunday, October 16, 11
  43. 60.

    Transformation Or even batch process... <?php foreach(glob('/path/to/many/images/*.png') as $path) {

    $transformation->apply($imagine->open($path)) ->save('/path/to/processed/image/'.md5($path).'.png'); } Sunday, October 16, 11
  44. 63.
  45. 64.

    Templates Twig PHP <img src="{{ user.photo|apply_filter('preview') }}" alt="avatar" /> <img

    src="<?php echo $view['imagine']->filter($user->getPhoto(), 'preview') ?>" alt="avatar" /> Sunday, October 16, 11
  46. 65.

    Process first request processes image and outputs response other controller

    requests result in a 301 redirect to file <img src="/imagine/preview/users/1/photo.jpg" alt="avatar" /> Sunday, October 16, 11
  47. 67.

    To be improved • advanced operations are still not easy

    (look at the reflection example) • only these drivers are supported • Imagick • GD • Gmagick Sunday, October 16, 11
  48. 68.

    Was improved • thumbnails are easy • code is readable

    • foundation is solid • its available today • its gonna be great Sunday, October 16, 11
  49. 69.

    What’s next? • Documentation - imagine.readthedocs.org • Implement charting API

    (piecharts, bar-charts, linear graphs) • Add advanced filters (reflection, rounded corners, etc.) • Add effects (twirl, blur, sharpen, etc.) Sunday, October 16, 11