Advertisement
Guest User

Additional Image Size

a guest
Oct 23rd, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.60 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author : Jegtheme
  4.  */
  5. namespace JNews\Image;
  6.  
  7. /**
  8.  * Class JNews Image
  9.  */
  10. Class Image
  11. {
  12.     /**
  13.      * @var Image
  14.      */
  15.     private static $instance;
  16.  
  17.     private $image_size = array();
  18.  
  19.     private $prefix = "jnews-";
  20.  
  21.     /**
  22.      * @return Image
  23.      */
  24.     public static function getInstance()
  25.     {
  26.         if (null === static::$instance)
  27.         {
  28.             static::$instance = new static();
  29.         }
  30.         return static::$instance;
  31.     }
  32.  
  33.     private function __construct()
  34.     {
  35.         $this->setup_image_size();
  36.  
  37.         add_action( 'wp_loaded', array($this, 'image_hook'));
  38.  
  39.         add_action('after_setup_theme', array($this, 'generate_image'), 99);
  40.     }
  41.  
  42.     public function generate_image()
  43.     {
  44.         $generate_image = get_theme_mod('jnews_image_generator', 'normal');
  45.  
  46.         if($generate_image === 'dynamic')
  47.         {
  48.             add_filter('image_downsize', array($this, 'image_down_size'), 99, 3);
  49.         } else {
  50.             add_action( 'init', array($this, 'add_image_size'));
  51.         }
  52.     }
  53.  
  54.     public function image_hook()
  55.     {
  56.         $mechanism = get_theme_mod('jnews_image_load', 'lazyload');
  57.  
  58.         if($mechanism === 'lazyload')
  59.         {
  60.             $image = ImageLazyLoad::getInstance();
  61.         } else if ($mechanism === 'background') {
  62.             $image = ImageBackgroundLoad::getInstance();
  63.         } else {
  64.             $image = ImageNormalLoad::getInstance();
  65.         }
  66.  
  67.         add_action('init', array($this, 'remove_image_sizes'));
  68.         add_action('intermediate_image_sizes_advanced', array($this, 'unset_image_size'));
  69.  
  70.         add_filter( 'jnews_image_lazy_owl' , array($image, 'owl_lazy_image'), null, 2);
  71.         add_filter( 'jnews_single_image_lazy_owl' , array($image, 'owl_lazy_single_image'), null, 2);
  72.         add_filter( 'jnews_image_thumbnail' , array($image, 'image_thumbnail'), null, 2);
  73.         add_filter( 'jnews_image_thumbnail_unwrap' , array($image, 'image_thumbnail_unwrap'), null, 2);
  74.         add_filter( 'jnews_single_image_unwrap' , array($image, 'single_image_unwrap'), null, 2);
  75.         add_filter( 'jnews_single_image_owl' , array($image, 'owl_single_image'), null, 2);
  76.  
  77.         add_filter( 'jnews_single_image', array($image, 'single_image'), null, 3 );
  78.         add_filter( 'image_size_names_choose', array($this, 'custom_size') );
  79.     }
  80.  
  81.     public function custom_size($sizes)
  82.     {
  83.         $this->setup_image_size();
  84.         foreach($this->image_size as $key => $size) {
  85.             $sizes[$key] = esc_html__('Custom Size', 'jnews');
  86.         }
  87.         return $sizes;
  88.     }
  89.  
  90.     public function remove_image_sizes()
  91.     {
  92.         remove_image_size('medium');
  93.         remove_image_size('medium_large');
  94.         remove_image_size('large');
  95.     }
  96.  
  97.     public function unset_image_size($sizes)
  98.     {
  99.         unset( $sizes['medium']);
  100.         unset( $sizes['medium_large']);
  101.         unset( $sizes['large']);
  102.  
  103.         return $sizes;
  104.     }
  105.  
  106.     public function get_image_size($size)
  107.     {
  108.         return $this->image_size[$size];
  109.     }
  110.  
  111.     public function setup_image_size()
  112.     {
  113.         $this->image_size = array
  114.         (
  115.             // dimension : 0.5
  116.             $this->prefix . '360x180'           => array('width' => 360,  'height' => 180, 'crop' => true , 'dimension' => 500),
  117.             $this->prefix . '750x375'           => array('width' => 750,  'height' => 375, 'crop' => true , 'dimension' => 500),
  118.             $this->prefix . '1140x570'          => array('width' => 1140, 'height' => 570, 'crop' => true , 'dimension' => 500),
  119.  
  120.             // dimension : 0.715
  121.             $this->prefix . '120x86'            => array('width' => 120,  'height' => 86,  'crop' => true , 'dimension' => 715),
  122.             $this->prefix . '350x250'           => array('width' => 350,  'height' => 250, 'crop' => true , 'dimension' => 715),
  123.             $this->prefix . '750x536'           => array('width' => 750,  'height' => 536, 'crop' => true , 'dimension' => 715),
  124.             $this->prefix . '1140x815'          => array('width' => 1140, 'height' => 815, 'crop' => true , 'dimension' => 715),
  125.  
  126.             // dimension
  127.             $this->prefix . '360x504'           => array('width' => 360, 'height' => 504, 'crop' => true , 'dimension' => 1400),
  128.  
  129.             // dimension 1
  130.             $this->prefix . '75x75'             => array('width' => 75, 'height' => 75, 'crop' => true , 'dimension' => 1000),
  131.  
  132.             // featured post
  133.             $this->prefix . 'featured-750'      => array('width' => 750,  'height' => 0,  'crop' => true , 'dimension' => 1000),
  134.             $this->prefix . 'featured-1140'     => array('width' => 1140, 'height' => 0,  'crop' => true , 'dimension' => 1000),
  135.         );
  136.     }
  137.  
  138.     public function add_image_size()
  139.     {
  140.         foreach($this->image_size as $id => $image)
  141.         {
  142.             add_image_size( $id, $image['width'], $image['height'], $image['crop'] );
  143.         }
  144.     }
  145.  
  146.     public static function generate_image_retina($image, $imageretina, $alt, $echo)
  147.     {
  148.         $srcset = '';
  149.  
  150.         if(!empty($imageretina))
  151.         {
  152.             $srcset = "srcset=\"" . esc_url($image) . " 1x, " . esc_url($imageretina) . " 2x\"";
  153.         }
  154.  
  155.         $header_logo = "<img src=\"" . esc_url($image) . "\" " . $srcset . " alt=\"" . esc_attr($alt) . "\">";
  156.  
  157.         if($echo) {
  158.             echo jnews_sanitize_output($header_logo);
  159.         } else {
  160.             return $header_logo;
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * The image downside filter
  166.  
  167.      * @param  boolean $ignore
  168.      * @param  integer $id
  169.      * @param  string  $size
  170.      * @return mixed
  171.      */
  172.     public function image_down_size($ignore = false, $id = null, $size = 'medium')
  173.     {
  174.         global $_wp_additional_image_sizes;
  175.  
  176.         $image   = wp_get_attachment_url($id);
  177.         $meta    = wp_get_attachment_metadata($id);
  178.         $width   = 0;
  179.         $height  = 0;
  180.         $crop    = false;
  181.         $dynamic = false;
  182.  
  183.         // return immediately if the size is "thumbnail".
  184.         if ($size == 'thumbnail') {
  185.             return false;
  186.         }
  187.  
  188.         // return immediately if intermediate image size found.
  189.         if (image_get_intermediate_size($id, $size) || is_array($size)) {
  190.             return false;
  191.         }
  192.  
  193.         // check if the image size is defined by 'add_image_size()' but not created yet.
  194.         if (isset($_wp_additional_image_sizes[$size])) {
  195.             $width  = $_wp_additional_image_sizes[$size]['width'];
  196.             $height = $_wp_additional_image_sizes[$size]['height'];
  197.             $crop   = $_wp_additional_image_sizes[$size]['crop'];
  198.         }
  199.  
  200.         // here we assume that the size is dynamic e.g. '200x200'.
  201.         elseif ($sizeArr = $this->parse_size($size)) {
  202.             $width   = isset($sizeArr['width']) ? $sizeArr['width'] : 0;
  203.             $height  = isset($sizeArr['height']) ? $sizeArr['height'] : 999999;
  204.             $crop    = isset($sizeArr['height']) ? true : false;
  205.             $dynamic = true;
  206.         }
  207.  
  208.         // let's continue if original image found, also if width & height are specified.
  209.         if ($image && $width && $height)
  210.         {
  211.             if (! $img = $this->make_image($id, $width, $height, $crop)) {
  212.                 return false;
  213.             }
  214.  
  215.             if ($dynamic) {
  216.                 $img['jnews_dynamic_images'] = true;
  217.             }
  218.  
  219.             unset($img['path']);
  220.  
  221.             $meta['sizes'][$size] = $img;
  222.  
  223.             // update attachment metadata.
  224.             wp_update_attachment_metadata($id, $meta);
  225.  
  226.             // replace original image url with newly created one.
  227.             $image = str_replace(wp_basename($image), wp_basename($img['file']), $image);
  228.  
  229.             // we might need to further constrain it if content_width is narrower
  230.             list($width, $height) = image_constrain_size_for_editor($width, $height, $size);
  231.  
  232.             // finally return the result.
  233.             return array($image, $width, $height, false);
  234.         }
  235.  
  236.         return false;
  237.     }
  238.  
  239.  
  240.     /**
  241.      * Parse image size.
  242.  
  243.      * @param  string $string
  244.      * @return array
  245.      */
  246.     public function parse_size($string)
  247.     {
  248.         $size  = array();
  249.  
  250.         if(!is_array($string) && substr( $string, 0, strlen($this->prefix) ) === $this->prefix)
  251.         {
  252.             $string = substr( $string, strlen($this->prefix) );
  253.             $array = explode('x', $string);
  254.  
  255.             foreach ($array as $key => $value) {
  256.                 $value = absint(trim($value));
  257.  
  258.                 if (! $value) {
  259.                     continue;
  260.                 }
  261.  
  262.                 if ($key === 0) {
  263.                     $size['width'] = $value;
  264.                 }
  265.  
  266.                 if ($key === 1) {
  267.                     $size['height'] = $value;
  268.                 }
  269.             }
  270.         } else {
  271.             return $string;
  272.         }
  273.  
  274.         return $size;
  275.     }
  276.  
  277.  
  278.     /**
  279.      * Create a new image by cropping the original image based on given size.
  280.      *
  281.      * @since  1.0.0
  282.      * @access public
  283.      * @param  integer $id
  284.      * @param  integer $width
  285.      * @param  integer $height
  286.      * @param  boolean $crop
  287.      * @return array
  288.      */
  289.     public function make_image($id, $width, $height = 999999, $crop = false)
  290.     {
  291.         $image  = get_attached_file($id);
  292.         $editor = wp_get_image_editor($image);
  293.  
  294.         if (! is_wp_error($editor)) {
  295.             $editor->resize($width, $height, $crop);
  296.             $editor->set_quality(100);
  297.  
  298.             $filename = $editor->generate_filename();
  299.  
  300.             return $editor->save($filename);
  301.         }
  302.     }
  303.  
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement