Skip to content

Instantly share code, notes, and snippets.

@jesgs jesgs/class.image-size.php

Last active Dec 14, 2015
Embed
What would you like to do?
Image size class for WordPress
<?php
/**
* ImageSizes
* Define and create image sizes
*
* @todo Add setter method for adding image sizes
*
* @package ImageSizes
* @author Jess Green <jgreen@psy-dreamer.com>
* @version $Id$
*/
namespace JesGs;
/**
* Class for image sizes
*
* @package ImageSizes
* @author Jess Green <jgreen@psy-dreamer.com>
*/
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
You can’t perform that action at this time.