SlideShare une entreprise Scribd logo
1  sur  48
Télécharger pour lire hors ligne
Image Manipulation in
   WordPress 3.5
         WordCamp Phoenix 2013




   Mike Schroder (DH-Shredder)
   @GetSource - http://www.getsource.net
Who Am I?
•   Mike Schroder, a.k.a DH-Shredder, a.k.a. @GetSource

•   Third Culture Kid, enjoy Coffee & Sailing

•   WordPress 3.5 Recent Rockstar and wp-cli Contributor

•   Co-Author of WP_Image_Editor

•   Happy DreamHost Employee
What was wrong?
GD was used directly.
This meant a mess of
functions for Developers.
What changed?
GD was abstracted.
WP_Image_Editor is born.
What’s WP_Image_Editor?
Centralized way to read an image file
 directly, manipulate it, and output
Community Built.
Primarily Marko Heijnen and I, with awesome
    help from Japh Thomson, Kurt Payne,
       Andrew Nacin and Cristi Burcă
3.5 comes with Editors for
GD and Imagick support
Imagick 2.2.0+ compiled with
   Imagemagick 6.2.9+
      for full support
With Imagick, this means
support for Color Profiles
GD   Imagick
GD   Imagick
Imagick
GD        Imagick
GD   Imagick
Imagick
GD        Imagick
What’s the catch?
Deprecated filters from GD
       abstraction
Deprecated Filters

•   image_save_pre is now image_editor_save_pre

•   wp_save_image_file is now wp_save_image_editor_file

•   image_edit_before_change is now
    wp_image_editor_before_change
What’s it missing?
 Centralized way to read an image
attachment from the database and
  manage its sizes and properties
That’s WP_Image.
   ...Hopefully
Okay.

So, what can I do with it?
Resize, Batch Resize, Crop,
 Flip, Rotate, and Stream.
     wp-includes/class-wp-image-editor.php
Check for Support.

/**
  * Tests whether there is an editor that supports a given mime type or methods.
  *
  * @since 3.5.0
  * @access public
  *
  * @param string|array $args Array of requirements.
  *        Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
  * @return boolean true if an eligible editor is found; false otherwise
  */
function wp_image_editor_supports( $args = array() ) {
     ...
}
Instantiate an Editor.

/**
  * Returns a WP_Image_Editor instance and loads file into it.
  *
  * @since 3.5.0
  * @access public
  *
  * @param string $path Path to file to load
  * @param array $args Additional data.
  *        Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
  * @return WP_Image_Editor|WP_Error
  */
function wp_get_image_editor( $path, $args = array() ) {
     ...
}
Resize.

! /**
! * Resizes current image.
! *
! * @since 3.5.0
! * @access public
! * @abstract
! *
! * @param int $max_w
! * @param int $max_h
! * @param boolean $crop
! * @return boolean|WP_Error
! */
! abstract public function resize( $max_w, $max_h, $crop = false );
Batch Resize.

! /**
! * Processes current image and saves to disk
! * multiple sizes from single source.
! *
! * @since 3.5.0
! * @access public
! * @abstract
! *
! * @param array $sizes { {'width'=>int, 'height'=>int, 'crop'=>bool}, ... }
! * @return array
! */
! abstract public function multi_resize( $sizes );
Crop.
! /**
! * Crops Image.
! *
! * @since 3.5.0
! * @access public
! * @abstract
! *
! * @param string|int $src The source file or Attachment ID.
! * @param int $src_x The start x position to crop from.
! * @param int $src_y The start y position to crop from.
! * @param int $src_w The width to crop.
! * @param int $src_h The height to crop.
! * @param int $dst_w Optional. The destination width.
! * @param int $dst_h Optional. The destination height.
! * @param boolean $src_abs Optional. If the source crop points are absolute.
! * @return boolean|WP_Error
! */
! abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null,
! !   ! ! ! ! ! ! ! !             $dst_h = null, $src_abs = false );
Rotate.

! /**
! * Rotates current image counter-clockwise by $angle.
! *
! * @since 3.5.0
! * @access public
! * @abstract
! *
! * @param float $angle
! * @return boolean|WP_Error
! */
! abstract public function rotate( $angle );
Flip!

! /**
! * Flips current image.
! *
! * @since 3.5.0
! * @access public
! * @abstract
! *
! * @param boolean $horz Horizontal Flip
! * @param boolean $vert Vertical Flip
! * @return boolean|WP_Error
! */
! abstract public function flip( $horz, $vert );
Stream.

! /**
! * Streams current image to browser.
! *
! * @since 3.5.0
! * @access public
! * @abstract
! *
! * @param string $mime_type
! * @return boolean|WP_Error
! */
! abstract public function stream( $mime_type = null );
Save or Convert.

! /**
! * Saves current image to file.
! *
! * @since 3.5.0
! * @access public
! * @abstract
! *
! * @param string $destfilename
! * @param string $mime_type
! * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int
! *                          'height'=>int, 'mime-type'=>string}
! */
! abstract public function save( $destfilename = null, $mime_type = null );
Time for an Example!
    // Get instance of WP_Image_Editor selected by WordPress
    $image = wp_get_image_editor( '/path/to/cool_image.jpg' );

    // Returns WP_Error on failure, so check.
    if ( ! is_wp_error( $image ) ) {

        // Rotate in 90 degree increments, for now.
        $image->rotate( 90 );

        // Thumbnail, and crop.
        $image->resize( 300, 300, true );

        // Uses extension for type, unless optional mime parameter is used.
        $image->save( 'new_image.gif' );

!       // Types only limited by Editor and what WordPress allows for uploads.
        if ( $image->supports_mime_type( 'application/pdf' ) )
            $image->stream( 'application/pdf' );
    }
To load an attachment...
      No current alternative to load_image_to_edit(), so:
wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
Extend to create your own
image-manip engines or functions.
Let’s extend Imagick for
everyone’s favorite filter:
Sepia!
Sepia makes the world go
        brown.
Extend from WP_Image_Editor.
class GS_Imagick_Sepia_Editor extends WP_Image_Editor_Imagick {

    /**
      * Filters current in-memory image with Sepia
      *
      * @since 1.0
      * @access public
      *
      * @param int $amount
      * @return bool|WP_Error
      */
    public function sepia( $amount = 80 ) {
         try {
             $this->image->sepiaToneImage( $amount );
             return true;
         }
         catch ( Exception $e ) {
             return new WP_Error( 'image_sepia_error', $e->getMessage() );
         }
    }
}
Enqueue your Editor.

/**
 * Add Sepia Editor to beginning of editor search array.
 *
 * The new editor doesn't need to be at beginning if specifically requesting
 * an editor with sepia() method, but it's safer overall.
 */
function gs_add_imagick_sepia( $editors ) {
    if( ! class_exists( 'GS_Imagick_Sepia_Editor' ) )
        include( plugin_dir_path( __FILE__ ) . 'editors/imagick-sepia.php' );

   array_unshift( $editors, 'GS_Imagick_Sepia_Editor' );
   return $editors;
}
add_filter( 'wp_image_editors', 'gs_add_imagick_sepia' );
Require and run the new method.

// Request an Editor with the sepia() method.
$sepia_editor = wp_get_image_editor( "/path/to/cool-image.jpg",
                                     array( 'methods' => array( 'sepia' ) ) );

// Double-check that we have an editor, and the file is open.
if ( ! is_wp_error( $sepia_editor ) ) {

    // Filter with sepia using our new method.
    $sepia_editor->sepia();

    // Send the image to the browser without saving.
    $sepia_editor->stream();
}
And, that’s it!
Image Editor Examples
•   GD: wp-includes/class-wp-image-editor-gd.php

•   Imagick: wp-includes/class-wp-image-editor-imagick.php


•   Gmagick: http://wordpress.org/extend/plugins/gmagick/
More Resources!
  •   http://make.wordpress.org/core/2012/12/06/wp_image_editor-is-incoming/

  •   http://markoheijnen.com/wordpress-new-image-manipulation/

  •   http://xref.wordpress.org/trunk/WordPress/Image_Editor/
      WP_Image_Editor.html

  •   https://github.com/getsource/imagick-sepia/

  •   https://github.com/humanmade/WPThumb/




Mike Schroder (DH-Shredder)
@GetSource - http://www.getsource.net

Contenu connexe

Tendances

Sherlock Markup and Sammy Semantic - drupal theming forensic analysis
Sherlock Markup and Sammy Semantic - drupal theming forensic analysisSherlock Markup and Sammy Semantic - drupal theming forensic analysis
Sherlock Markup and Sammy Semantic - drupal theming forensic analysisAndreas Sahle
 
Responsive Web Design e a Ubiquidade da Web
Responsive Web Design e a Ubiquidade da WebResponsive Web Design e a Ubiquidade da Web
Responsive Web Design e a Ubiquidade da WebEduardo Shiota Yasuda
 
Quality Use Of Plugin
Quality Use Of PluginQuality Use Of Plugin
Quality Use Of PluginYasuo Harada
 
jQuery Performance Rules
jQuery Performance RulesjQuery Performance Rules
jQuery Performance Rulesnagarajhubli
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 
Building Robust jQuery Plugins
Building Robust jQuery PluginsBuilding Robust jQuery Plugins
Building Robust jQuery PluginsJörn Zaefferer
 
Your own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with RubyYour own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with RubyLindsay Holmwood
 
Intro to Pylons / Pyramid
Intro to Pylons / PyramidIntro to Pylons / Pyramid
Intro to Pylons / PyramidEric Paxton
 
Mulberry: A Mobile App Development Toolkit
Mulberry: A Mobile App Development ToolkitMulberry: A Mobile App Development Toolkit
Mulberry: A Mobile App Development ToolkitRebecca Murphey
 
With a Mighty Hammer
With a Mighty HammerWith a Mighty Hammer
With a Mighty HammerBen Scofield
 
Geodaten & Drupal 7
Geodaten & Drupal 7Geodaten & Drupal 7
Geodaten & Drupal 7Michael Milz
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Kris Wallsmith
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)arcware
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 

Tendances (20)

Sherlock Markup and Sammy Semantic - drupal theming forensic analysis
Sherlock Markup and Sammy Semantic - drupal theming forensic analysisSherlock Markup and Sammy Semantic - drupal theming forensic analysis
Sherlock Markup and Sammy Semantic - drupal theming forensic analysis
 
Responsive Web Design e a Ubiquidade da Web
Responsive Web Design e a Ubiquidade da WebResponsive Web Design e a Ubiquidade da Web
Responsive Web Design e a Ubiquidade da Web
 
Quality Use Of Plugin
Quality Use Of PluginQuality Use Of Plugin
Quality Use Of Plugin
 
Workshop 6: Designer tools
Workshop 6: Designer toolsWorkshop 6: Designer tools
Workshop 6: Designer tools
 
jQuery Performance Rules
jQuery Performance RulesjQuery Performance Rules
jQuery Performance Rules
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
Building Robust jQuery Plugins
Building Robust jQuery PluginsBuilding Robust jQuery Plugins
Building Robust jQuery Plugins
 
Your own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with RubyYour own (little) gem: building an online business with Ruby
Your own (little) gem: building an online business with Ruby
 
Capybara
CapybaraCapybara
Capybara
 
Dojo Confessions
Dojo ConfessionsDojo Confessions
Dojo Confessions
 
Intro to Pylons / Pyramid
Intro to Pylons / PyramidIntro to Pylons / Pyramid
Intro to Pylons / Pyramid
 
DrupalCon jQuery
DrupalCon jQueryDrupalCon jQuery
DrupalCon jQuery
 
Mulberry: A Mobile App Development Toolkit
Mulberry: A Mobile App Development ToolkitMulberry: A Mobile App Development Toolkit
Mulberry: A Mobile App Development Toolkit
 
18. images in symfony 4
18. images in symfony 418. images in symfony 4
18. images in symfony 4
 
With a Mighty Hammer
With a Mighty HammerWith a Mighty Hammer
With a Mighty Hammer
 
Geodaten & Drupal 7
Geodaten & Drupal 7Geodaten & Drupal 7
Geodaten & Drupal 7
 
Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3Introducing Assetic: Asset Management for PHP 5.3
Introducing Assetic: Asset Management for PHP 5.3
 
Cucumber
CucumberCucumber
Cucumber
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 

En vedette

Image Manipulation Inspiration
Image Manipulation InspirationImage Manipulation Inspiration
Image Manipulation Inspirationcatherine lewis
 
Image Manipulation powerpoint
Image Manipulation powerpointImage Manipulation powerpoint
Image Manipulation powerpointemilyaldredd
 
Magazine Construction: Image Manipulation
Magazine Construction: Image ManipulationMagazine Construction: Image Manipulation
Magazine Construction: Image Manipulationerinmcginty
 
Lesson 6 optimisation
Lesson 6  optimisationLesson 6  optimisation
Lesson 6 optimisationhwells2101
 
Basic image manipulation tutorial
Basic image manipulation tutorialBasic image manipulation tutorial
Basic image manipulation tutorialHelen Bruce
 
Lesson 3 graphics types
Lesson 3  graphics typesLesson 3  graphics types
Lesson 3 graphics typeshwells2101
 

En vedette (7)

Social Media and Marketing_en
Social Media and Marketing_enSocial Media and Marketing_en
Social Media and Marketing_en
 
Image Manipulation Inspiration
Image Manipulation InspirationImage Manipulation Inspiration
Image Manipulation Inspiration
 
Image Manipulation powerpoint
Image Manipulation powerpointImage Manipulation powerpoint
Image Manipulation powerpoint
 
Magazine Construction: Image Manipulation
Magazine Construction: Image ManipulationMagazine Construction: Image Manipulation
Magazine Construction: Image Manipulation
 
Lesson 6 optimisation
Lesson 6  optimisationLesson 6  optimisation
Lesson 6 optimisation
 
Basic image manipulation tutorial
Basic image manipulation tutorialBasic image manipulation tutorial
Basic image manipulation tutorial
 
Lesson 3 graphics types
Lesson 3  graphics typesLesson 3  graphics types
Lesson 3 graphics types
 

Similaire à Image Manipulation in WordPress 3.5 - WordCamp Phoenix 2013

QConSP 2015 - Dicas de Performance para Aplicações Web
QConSP 2015 - Dicas de Performance para Aplicações WebQConSP 2015 - Dicas de Performance para Aplicações Web
QConSP 2015 - Dicas de Performance para Aplicações WebFabio Akita
 
Simple Photo Processing and Web Display with Perl
Simple Photo Processing and Web Display with PerlSimple Photo Processing and Web Display with Perl
Simple Photo Processing and Web Display with PerlKent Cowgill
 
Laying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentLaying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentTammy Hart
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ EtsyNishan Subedi
 
Drupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to PluginDrupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to PluginAcquia
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmersAlexander Varwijk
 
Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentationBrian Hogg
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowVrann Tulika
 
WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1Yoav Farhi
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)James Titcumb
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)Beau Lebens
 
Flash Widget Tutorial
Flash Widget TutorialFlash Widget Tutorial
Flash Widget Tutorialhussulinux
 
[cssdevconf] Adaptive Images in RWD
[cssdevconf] Adaptive Images in RWD[cssdevconf] Adaptive Images in RWD
[cssdevconf] Adaptive Images in RWDChristopher Schmitt
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)James Titcumb
 

Similaire à Image Manipulation in WordPress 3.5 - WordCamp Phoenix 2013 (20)

Images and PWA in magento
Images and PWA in magentoImages and PWA in magento
Images and PWA in magento
 
QConSP 2015 - Dicas de Performance para Aplicações Web
QConSP 2015 - Dicas de Performance para Aplicações WebQConSP 2015 - Dicas de Performance para Aplicações Web
QConSP 2015 - Dicas de Performance para Aplicações Web
 
Simple Photo Processing and Web Display with Perl
Simple Photo Processing and Web Display with PerlSimple Photo Processing and Web Display with Perl
Simple Photo Processing and Web Display with Perl
 
Laying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentLaying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme development
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ Etsy
 
Seven deadly theming sins
Seven deadly theming sinsSeven deadly theming sins
Seven deadly theming sins
 
Drupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to PluginDrupal 8, Where Did the Code Go? From Info Hook to Plugin
Drupal 8, Where Did the Code Go? From Info Hook to Plugin
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
 
Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentation
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Magento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request FlowMagento Live Australia 2016: Request Flow
Magento Live Australia 2016: Request Flow
 
Extend sdk
Extend sdkExtend sdk
Extend sdk
 
WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
Kicking off with Zend Expressive and Doctrine ORM (Sunshine PHP 2017)
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)
 
Flash Widget Tutorial
Flash Widget TutorialFlash Widget Tutorial
Flash Widget Tutorial
 
[cssdevconf] Adaptive Images in RWD
[cssdevconf] Adaptive Images in RWD[cssdevconf] Adaptive Images in RWD
[cssdevconf] Adaptive Images in RWD
 
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
Kicking off with Zend Expressive and Doctrine ORM (PHP UK 2017)
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 

Dernier

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Dernier (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Image Manipulation in WordPress 3.5 - WordCamp Phoenix 2013

  • 1. Image Manipulation in WordPress 3.5 WordCamp Phoenix 2013 Mike Schroder (DH-Shredder) @GetSource - http://www.getsource.net
  • 2. Who Am I? • Mike Schroder, a.k.a DH-Shredder, a.k.a. @GetSource • Third Culture Kid, enjoy Coffee & Sailing • WordPress 3.5 Recent Rockstar and wp-cli Contributor • Co-Author of WP_Image_Editor • Happy DreamHost Employee
  • 4. GD was used directly.
  • 5. This meant a mess of functions for Developers.
  • 9. What’s WP_Image_Editor? Centralized way to read an image file directly, manipulate it, and output
  • 10. Community Built. Primarily Marko Heijnen and I, with awesome help from Japh Thomson, Kurt Payne, Andrew Nacin and Cristi Burcă
  • 11. 3.5 comes with Editors for GD and Imagick support
  • 12. Imagick 2.2.0+ compiled with Imagemagick 6.2.9+ for full support
  • 13. With Imagick, this means support for Color Profiles
  • 14. GD Imagick
  • 15. GD Imagick
  • 16. Imagick GD Imagick
  • 17. GD Imagick
  • 18. Imagick GD Imagick
  • 19. What’s the catch? Deprecated filters from GD abstraction
  • 20. Deprecated Filters • image_save_pre is now image_editor_save_pre • wp_save_image_file is now wp_save_image_editor_file • image_edit_before_change is now wp_image_editor_before_change
  • 21. What’s it missing? Centralized way to read an image attachment from the database and manage its sizes and properties
  • 22. That’s WP_Image. ...Hopefully
  • 23. Okay. So, what can I do with it?
  • 24. Resize, Batch Resize, Crop, Flip, Rotate, and Stream. wp-includes/class-wp-image-editor.php
  • 25.
  • 26. Check for Support. /** * Tests whether there is an editor that supports a given mime type or methods. * * @since 3.5.0 * @access public * * @param string|array $args Array of requirements. * Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} } * @return boolean true if an eligible editor is found; false otherwise */ function wp_image_editor_supports( $args = array() ) { ... }
  • 27. Instantiate an Editor. /** * Returns a WP_Image_Editor instance and loads file into it. * * @since 3.5.0 * @access public * * @param string $path Path to file to load * @param array $args Additional data. * Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} } * @return WP_Image_Editor|WP_Error */ function wp_get_image_editor( $path, $args = array() ) { ... }
  • 28. Resize. ! /** ! * Resizes current image. ! * ! * @since 3.5.0 ! * @access public ! * @abstract ! * ! * @param int $max_w ! * @param int $max_h ! * @param boolean $crop ! * @return boolean|WP_Error ! */ ! abstract public function resize( $max_w, $max_h, $crop = false );
  • 29. Batch Resize. ! /** ! * Processes current image and saves to disk ! * multiple sizes from single source. ! * ! * @since 3.5.0 ! * @access public ! * @abstract ! * ! * @param array $sizes { {'width'=>int, 'height'=>int, 'crop'=>bool}, ... } ! * @return array ! */ ! abstract public function multi_resize( $sizes );
  • 30. Crop. ! /** ! * Crops Image. ! * ! * @since 3.5.0 ! * @access public ! * @abstract ! * ! * @param string|int $src The source file or Attachment ID. ! * @param int $src_x The start x position to crop from. ! * @param int $src_y The start y position to crop from. ! * @param int $src_w The width to crop. ! * @param int $src_h The height to crop. ! * @param int $dst_w Optional. The destination width. ! * @param int $dst_h Optional. The destination height. ! * @param boolean $src_abs Optional. If the source crop points are absolute. ! * @return boolean|WP_Error ! */ ! abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, ! ! ! ! ! ! ! ! ! ! $dst_h = null, $src_abs = false );
  • 31. Rotate. ! /** ! * Rotates current image counter-clockwise by $angle. ! * ! * @since 3.5.0 ! * @access public ! * @abstract ! * ! * @param float $angle ! * @return boolean|WP_Error ! */ ! abstract public function rotate( $angle );
  • 32. Flip! ! /** ! * Flips current image. ! * ! * @since 3.5.0 ! * @access public ! * @abstract ! * ! * @param boolean $horz Horizontal Flip ! * @param boolean $vert Vertical Flip ! * @return boolean|WP_Error ! */ ! abstract public function flip( $horz, $vert );
  • 33. Stream. ! /** ! * Streams current image to browser. ! * ! * @since 3.5.0 ! * @access public ! * @abstract ! * ! * @param string $mime_type ! * @return boolean|WP_Error ! */ ! abstract public function stream( $mime_type = null );
  • 34. Save or Convert. ! /** ! * Saves current image to file. ! * ! * @since 3.5.0 ! * @access public ! * @abstract ! * ! * @param string $destfilename ! * @param string $mime_type ! * @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int ! * 'height'=>int, 'mime-type'=>string} ! */ ! abstract public function save( $destfilename = null, $mime_type = null );
  • 35. Time for an Example! // Get instance of WP_Image_Editor selected by WordPress $image = wp_get_image_editor( '/path/to/cool_image.jpg' ); // Returns WP_Error on failure, so check. if ( ! is_wp_error( $image ) ) { // Rotate in 90 degree increments, for now. $image->rotate( 90 ); // Thumbnail, and crop. $image->resize( 300, 300, true ); // Uses extension for type, unless optional mime parameter is used. $image->save( 'new_image.gif' ); ! // Types only limited by Editor and what WordPress allows for uploads. if ( $image->supports_mime_type( 'application/pdf' ) ) $image->stream( 'application/pdf' ); }
  • 36. To load an attachment... No current alternative to load_image_to_edit(), so: wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
  • 37. Extend to create your own image-manip engines or functions.
  • 38. Let’s extend Imagick for everyone’s favorite filter:
  • 40. Sepia makes the world go brown.
  • 41. Extend from WP_Image_Editor. class GS_Imagick_Sepia_Editor extends WP_Image_Editor_Imagick { /** * Filters current in-memory image with Sepia * * @since 1.0 * @access public * * @param int $amount * @return bool|WP_Error */ public function sepia( $amount = 80 ) { try { $this->image->sepiaToneImage( $amount ); return true; } catch ( Exception $e ) { return new WP_Error( 'image_sepia_error', $e->getMessage() ); } } }
  • 42. Enqueue your Editor. /** * Add Sepia Editor to beginning of editor search array. * * The new editor doesn't need to be at beginning if specifically requesting * an editor with sepia() method, but it's safer overall. */ function gs_add_imagick_sepia( $editors ) { if( ! class_exists( 'GS_Imagick_Sepia_Editor' ) ) include( plugin_dir_path( __FILE__ ) . 'editors/imagick-sepia.php' ); array_unshift( $editors, 'GS_Imagick_Sepia_Editor' ); return $editors; } add_filter( 'wp_image_editors', 'gs_add_imagick_sepia' );
  • 43. Require and run the new method. // Request an Editor with the sepia() method. $sepia_editor = wp_get_image_editor( "/path/to/cool-image.jpg", array( 'methods' => array( 'sepia' ) ) ); // Double-check that we have an editor, and the file is open. if ( ! is_wp_error( $sepia_editor ) ) { // Filter with sepia using our new method. $sepia_editor->sepia(); // Send the image to the browser without saving. $sepia_editor->stream(); }
  • 45.
  • 46. Image Editor Examples • GD: wp-includes/class-wp-image-editor-gd.php • Imagick: wp-includes/class-wp-image-editor-imagick.php • Gmagick: http://wordpress.org/extend/plugins/gmagick/
  • 47.
  • 48. More Resources! • http://make.wordpress.org/core/2012/12/06/wp_image_editor-is-incoming/ • http://markoheijnen.com/wordpress-new-image-manipulation/ • http://xref.wordpress.org/trunk/WordPress/Image_Editor/ WP_Image_Editor.html • https://github.com/getsource/imagick-sepia/ • https://github.com/humanmade/WPThumb/ Mike Schroder (DH-Shredder) @GetSource - http://www.getsource.net