Last active
August 29, 2015 14:23
-
-
Save markoheijnen/abe4f4ed0f52178218a3 to your computer and use it in GitHub Desktop.
Favicon generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WP_Site_Icon_Creator { | |
private $file; | |
public function __construct() { | |
$this->file = dirname( __FILE__ ) . '/512px_Icon.png'; | |
$this->generate(); | |
} | |
public function generate() { | |
$editor = wp_get_image_editor( $this->file ); | |
if ( is_wp_error( $editor ) ) { | |
return false; | |
} | |
$upload_dir = wp_upload_dir(); | |
$sizes = get_site_icon_sizes(); | |
uasort( $sizes, array( $this, 'order_by_size' ) ); | |
foreach ( $sizes as $id => $args ) { | |
$editor->resize( $args['size'], $args['size'], true ); | |
$resized_file = $editor->save( $upload_dir['basedir'] . '/icons/' . $id ); | |
} | |
if ( $editor->supports_mime_type('image/x-icon') ) { | |
$editor->resize( 16, 16, true ); | |
$resized_file = $editor->save( $upload_dir['basedir'] . '/icons/favicon.ico' ); | |
} | |
exit; | |
} | |
public function order_by_size( $a, $b ) { | |
return ( $a['size'] > $b['size'] ) ? -1 : 1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
<?php | |
class WP_Site_Icon_Sizes { | |
protected $sizes = array(); | |
public function __construct() { | |
foreach ( array( 16, 32, 96, 192 ) as $size ) { | |
$this->add( "favicon-{$size}", array( | |
'label' => __( 'Favicon %s' ), | |
'size' => $size, | |
'scalable' => true, | |
'display_callback' => array( $this, 'display_favicon' ), | |
) ); | |
} | |
foreach ( array( 120, 152 ) as $size ) { | |
$this->add( "touch-icon-{$size}", array( | |
'label' => __( 'Touch Icon %s' ), | |
'size' => $size, | |
'scalable' => true, | |
'display_callback' => array( $this, 'display_touch_icon' ), | |
) ); | |
} | |
} | |
public function add( $id, array $args ) { | |
$this->sizes[ $id ] = $args; | |
return true; | |
} | |
public function remove( $id ) { | |
if ( isset( $this->sizes[ $id ] ) ) { | |
unset( $this->sizes[ $id ] ); | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function get_all() { | |
return $this->sizes; | |
} | |
public function display_favicon( $size, $src, WP_Post $attachment ) { | |
$mime_type = explode( '/', get_post_mime_type( $attachment ) ); | |
$icon = sprintf( '<link rel="icon" sizes="%1$dx%1$d" href="$2%s" type="image/%3$d">', | |
absint( $size ), | |
esc_url( $src ), | |
esc_attr( $mime_type[1] ) | |
); | |
return $icon; | |
} | |
public function display_touch_icon( $size, $src, WP_Post $attachment ) { | |
$icon1x = sprintf( '<link rel="apple-touch-icon-precomposed" sizes="%1$dx%1$d" href="$2%s">', | |
absint( $size ) / 2, | |
esc_url( $src ) | |
); | |
$icon2x = sprintf( '<link rel="apple-touch-icon-precomposed" sizes="%1$dx%1$d" href="$2%s">', | |
absint( $size ), | |
esc_url( $src ) | |
); | |
return $icon1x . $icon2x; | |
} | |
/** | |
* Singleton. | |
* | |
* @return WP_Site_Icon_Sizes Site Icon Sizes instance. | |
*/ | |
public static function get_instance() { | |
static $instance; | |
if ( ! isset( $instance ) ) { | |
$instance = new WP_Site_Icon_Sizes; | |
} | |
return $instance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment