Your IP : 216.73.216.171


Current Path : /home/ereika83/www/wp-content/plugins/genesis-club-pro/classes/
Upload File :
Current File : /home/ereika83/www/wp-content/plugins/genesis-club-pro/classes/class-media-thumbnails.php

<?php
class Genesis_Club_Thumbnails {

   const THUMBNAIL_FOLDER = 'thumbnails';
   const THUMBNAIL_SIZE = 'thumbnail';

   private $defaults = array('size' => '', 'attr' => array(), 'format' => 'html');
   private $thumbnail_quality;
   private $thumbnail_folder;
   private $thumbnail_size;
   private $thumbnail_height;
   private $thumbnail_width;
   private $thumbnail_crop;

   function __construct($quality=0) {
         $this->set_quality($quality); 
         $this->set_folder();
         $this->set_size();      

   }

    function get_featured_thumbnail($content, $args, $post) {
        if (has_post_thumbnail())
            return $content; //use featured image if one exists
        else {
            $args = wp_parse_args ($args, $this->defaults); 
            $this->set_size($args['size']);
            $post_id = is_object($post) ? $post->ID : $post;
            if ($thumb = $this->get_thumbnail($post_id))
                return $this->get_thumb_format($thumb, $post_id, $args);
            else
                return $content;         
        } 
    } 

   function get_thumb_format($thumb,  $post_id, $args) {
      if ('url'==$args['format'] )
         return $thumb['src'] ;
      else    
         return sprintf('<img %1$s src="%2$s" alt="%3$s" %4$s />',
            image_hwstring($thumb['width'], $thumb['height']) ,
            $thumb['src'], 
            $this->get_featured_image_alt($post_id),
            (is_array($args['attr']) && array_key_exists('class',$args['attr'])) ? sprintf('class="%1$s"',$args['attr']['class']) : '');
   }

   function get_thumbnail($post_id) {
      $thumbs = array();
      $thumb_exists = false;
      if ($thumb_values = get_post_meta($post_id, Genesis_Club_Media::FEATURED_THUMBS_KEY, true)) {
         $thumbs = (array) maybe_unserialize($thumb_values);
         if (is_array($thumbs) && array_key_exists($this->thumbnail_size,$thumbs)) {
			   $thumb = $thumbs[$this->thumbnail_size];
			   $thumb_exists = array_key_exists('src',$thumb) && $this->url_exists($thumb['src']);
         }
      }
      if ($thumb_exists) 
         return $thumb; 
      elseif ($url = $this->get_featured_image_url($post_id)) {
         $thumb = $this->resize_image($url, $this->thumbnail_quality);
         if (!is_wp_error($thumb)) {
            $thumbs[$this->thumbnail_size] = $thumb;
            update_post_meta($post_id, Genesis_Club_Media::FEATURED_THUMBS_KEY, $thumbs);	
            return $thumb;
         }	
      }
      return false;
   }

   function get_facebook_image($image, $the_id=0, $args= array('size' => 'full')) {
        global $post;
        if (empty($the_id) && is_singular() ) $the_id = $post->ID;
        
        if ($url = $this->get_featured_image_url($the_id)) {
            if ($args['size'] == 'full') {
                $image = $url;
            } else {
                $args = wp_parse_args ($args, $this->defaults); 
                $this->set_size($args['size']);
                if ($thumb = $this->get_thumbnail($the_id))
                    $image = $thumb['src'];
            }
        }
        return $image;
   }

    function get_featured_image_alt($post_id) {
        return get_post_meta($post_id, Genesis_Club_Media::FEATURED_IMAGE_ALT_KEY, true);
    }

    function get_featured_image_url($post_id) {
      $url = get_post_meta($post_id, Genesis_Club_Media::FEATURED_IMAGE_KEY, true);
      if (empty($url)) {
         $content = get_post_field('post_content',$post_id);
         $url = $this->get_first_url_from_content($content);
         if ($url) update_post_meta($post_id, Genesis_Club_Media::FEATURED_IMAGE_KEY, $url);	
      }	
      return $url;
   }

   function get_first_url_from_content($html) {
      $images = array();
      preg_match_all('/(imgsrc|src)=(\"|\')[^\"\'>]+/i', $html, $images);
      $urls=preg_replace('/(imgsrc|src)(\"|\'|=\"|=\')(.*)/i',"$3",$images[0]);
      foreach($urls as $url) {
         $ext = substr(strrchr($url, '.'), 1);
         if (isset($ext) 
         && ( ($ext == 'jpg') || ($ext == 'jpeg') || ($ext == 'gif') || ($ext == 'png')) 
         && $this->url_exists($url)) 
            return $url;
      }
      return false;
   }

   function url_exists($url) {
      $hdrs = @get_headers($url);
      return is_array($hdrs) && (strpos($hdrs[0],'404') === FALSE);
   }

   function resize_image($url, $jpeg_quality, $suffix = null) {
      @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
      if ( ! $original_imagestring = @file_get_contents( $url ))
         return new WP_Error('fetch_image_failed',__('Unable to read image file &#8220;%s&#8221;.'), $url);
      if ( ! function_exists('imagecreatefromstring') )
         return new WP_Error('no_gd_library',__('The GD image library is not installed.'));
      $original_image = @imagecreatefromstring( $original_imagestring );
      if ( !is_resource( $original_image ) )
         return new WP_Error('fetch_image_failed',__('File &#8220;%s&#8221; is not an image.'), $url);	    	
      $orig_w = imagesx( $original_image);
      $orig_h = imagesy( $original_image); 	
      $orig_type = substr(strrchr($url, '.'), 1);
      $max_w = $this->thumbnail_width;
	  $max_h = $this->thumbnail_height;		
	  $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $this->thumbnail_crop);
	  if ( !$dims )
	     return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') );
	  list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
	  $newimage = wp_imagecreatetruecolor( $dst_w, $dst_h );

	   imagecopyresampled( $newimage, $original_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
	
      // convert from full colors to index colors, like original PNG.
	   if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $original_image) )
	     imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );
	
      // we don't need the original in memory anymore
	   imagedestroy( $original_image );
	
      $ext = substr(strrchr($url, '.'), 1);
	   if ( !$suffix ) $suffix = "{$dst_w}x{$dst_h}";	
	   $file = substr(strrchr($url, '/'), 1);
		$name = wp_basename($file, ".$ext")."-{$suffix}.{$ext}";
		$uploads = wp_upload_dir();	  
		$thumbnail_dir =  $uploads['basedir'] .'/'. $this->thumbnail_folder;
		if (! file_exists($thumbnail_dir)) mkdir($thumbnail_dir);     
		$destfilename = $thumbnail_dir . '/'. $name;
		$desturl = $uploads['baseurl'] .'/'. $this->thumbnail_folder . '/'. $name;
	   if ( IMAGETYPE_GIF == $orig_type ) {
         if ( !imagegif( $newimage, $destfilename ) )
	        return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
	   } elseif ( IMAGETYPE_PNG == $orig_type ) {
	     if ( !imagepng( $newimage, $destfilename ) )
	        return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
	   } else {
	     // all other formats are converted to jpg
	     if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
	        return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
	   }
	
      imagedestroy( $newimage );
	
      // Set correct file permissions
	   $stat = stat( dirname( $destfilename ));
	   $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
	   @ chmod( $destfilename, $perms );	
	   return array( 'src' => $desturl, 'width' => $dst_w, 'height' => $dst_h);
	}

   function get_all_image_sizes() {
      global $_wp_additional_image_sizes;
		foreach ( get_intermediate_image_sizes() as $s ) {
			$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
			if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
				$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
			else
				$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
			if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
				$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
			else
				$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
			if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
				$sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
			else
				$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
		}
		return $sizes ;	
	}
	
   private function set_size($preferred_size = '') {
		if (empty($preferred_size)) $preferred_size = self::THUMBNAIL_SIZE;
		if ($preferred_size == $this->thumbnail_size) return true;
		$sizes = $this->get_all_image_sizes();
		$valid_size = ($preferred_size && array_key_exists($preferred_size,$sizes)) ? $preferred_size : 'thumbnail';
		if (!array_key_exists($valid_size, $sizes)) $sizes[$valid_size] = array('width'=> 150, 'height' => 150, 'crop' => true);
		$this->thumbnail_size = $valid_size;
		$this->thumbnail_width = $sizes[$valid_size]['width'];
		$this->thumbnail_height = $sizes[$valid_size]['height'];
		$this->thumbnail_crop = $sizes[$valid_size]['crop'];
	}

   private function set_folder($preferred_folder ='') {
		if (empty($preferred_folder)) $preferred_folder = self::THUMBNAIL_FOLDER;
		if ($preferred_folder != $this->thumbnail_folder) 
			$this->thumbnail_folder = $preferred_folder;
	}

   private function set_quality($quality = 0) {
		if (empty($quality)) $quality = Genesis_Club_Media::THUMBNAIL_QUALITY;
		$this->thumbnail_quality = min($quality,100);
	}
}