Last active
December 14, 2015 09:39
-
-
Save jesgs/5066730 to your computer and use it in GitHub Desktop.
Image size class for WordPress
This file contains hidden or 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 | |
/** | |
* ImageSizes | |
* Define and create image sizes | |
* | |
* @todo Add setter method for adding image sizes | |
* | |
* @package ImageSizes | |
* @author Jess Green <[email protected]> | |
* @version $Id$ | |
*/ | |
namespace JesGs; | |
/** | |
* Class for image sizes | |
* | |
* @package ImageSizes | |
* @author Jess Green <[email protected]> | |
*/ | |
class ImageSizes | |
{ | |
const MEDSQUARE = 'thumbnail-medium-square'; // square 300x300 | |
const LONG = 'thumbnail-long'; // long 16:9 640x360 | |
/** | |
* Array of image sizes, defined above | |
* | |
* @var array | |
*/ | |
protected $_sizes = array( | |
self::MEDSQUARE => array( | |
'height' => 300, | |
'width' => 300, | |
'crop' => false, | |
), | |
self::LONG => array( | |
'height' => 640, | |
'width' => 360, | |
'crop' => false, | |
), | |
); | |
/** | |
* Instance of class | |
* | |
* @var \JesGs\ImageSizes | |
*/ | |
protected static $_instance; | |
/** | |
* PHP5 Constructor function | |
* | |
* @return \JesGs\ImageSizes | |
*/ | |
protected function __construct() | |
{ | |
return $this; | |
} | |
/** | |
* Get instance of class | |
* | |
* @return \JesGs\ImageSizes | |
*/ | |
public static function get_instance() | |
{ | |
if (self::$_instance === null) { | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
/** | |
* Get sizes | |
* | |
* @return array | |
*/ | |
public function get_sizes() | |
{ | |
return $this->_sizes; | |
} | |
/** | |
* Get an image size | |
* | |
* @return array | |
*/ | |
public function get_size($size) | |
{ | |
if (!isset($this->_sizes[$size])) { | |
return false; | |
} | |
return $this->_sizes[$size]; | |
} | |
/** | |
* Create image sizes | |
* | |
* @return void | |
*/ | |
public function create_image_sizes() | |
{ | |
foreach ($this->_sizes as $size_name => $size_data) { | |
add_image_size( | |
$size_name, | |
$size_data['width'], | |
$size_data['height'], | |
$size_data['crop'] | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment