| 1 |
<?php |
| 2 |
|
| 3 |
class WPUPG_Template_Post_Image extends WPUPG_Template_Block { |
| 4 |
|
| 5 |
public $editorField = 'postImage'; |
| 6 |
public $thumbnail; |
| 7 |
public $crop = false; |
| 8 |
|
| 9 |
public function __construct( $type = 'post-image' ) |
| 10 |
{ |
| 11 |
parent::__construct( $type ); |
| 12 |
} |
| 13 |
|
| 14 |
public function thumbnail( $thumbnail ) |
| 15 |
{ |
| 16 |
$this->thumbnail = $thumbnail; |
| 17 |
return $this; |
| 18 |
} |
| 19 |
|
| 20 |
public function crop( $crop ) |
| 21 |
{ |
| 22 |
$this->crop = $crop; |
| 23 |
return $this; |
| 24 |
} |
| 25 |
|
| 26 |
public function output( $post, $args = array() ) |
| 27 |
{ |
| 28 |
if( !$this->output_block( $post, $args ) ) return ''; |
| 29 |
if( !isset( $this->thumbnail ) ) $this->thumbnail = 'full'; |
| 30 |
|
| 31 |
$post_image_id = get_post_thumbnail_id( $post->ID ); |
| 32 |
$thumb = wp_get_attachment_image_src( $post_image_id, $this->thumbnail ); |
| 33 |
|
| 34 |
if(!$thumb) return ''; // No recipe image found |
| 35 |
|
| 36 |
$image_url = $thumb[0]; |
| 37 |
if( is_null( $image_url ) ) return ''; |
| 38 |
|
| 39 |
// Check image size unless a specific thumbnail was specified |
| 40 |
if( is_array( $this->thumbnail ) ) { |
| 41 |
|
| 42 |
$new_width = $this->thumbnail[0]; |
| 43 |
$new_height = $this->thumbnail[1]; |
| 44 |
|
| 45 |
if( $thumb[1] && $thumb[2] ) { // Use image size if passed along |
| 46 |
$width = $thumb[1]; |
| 47 |
$height = $thumb[2]; |
| 48 |
} else { // Or look it up for ourselves otherwise |
| 49 |
$size = getimagesize( $image_url ); |
| 50 |
$width = $size[0]; |
| 51 |
$height = $size[1]; |
| 52 |
} |
| 53 |
|
| 54 |
// Don't distort the image |
| 55 |
$undistored_height = floor( $new_width * ( $height / $width ) ); |
| 56 |
$this->add_style( 'height', $undistored_height.'px' ); |
| 57 |
|
| 58 |
// Get correct thumbnail size |
| 59 |
$correct_thumb = array( |
| 60 |
$new_width, |
| 61 |
$undistored_height |
| 62 |
); |
| 63 |
|
| 64 |
$thumb = wp_get_attachment_image_src( $post_image_id, $correct_thumb ); |
| 65 |
$image_url = $thumb[0]; |
| 66 |
|
| 67 |
// Cropping the image |
| 68 |
if( $this->crop ) { |
| 69 |
$this->add_style( 'overflow', 'hidden', 'outer' ); |
| 70 |
$this->add_style( 'max-width', $new_width.'px', 'outer' ); |
| 71 |
$this->add_style( 'max-height', $new_height.'px', 'outer' ); |
| 72 |
|
| 73 |
if( $new_height < $undistored_height ) { |
| 74 |
$margin = -1 * ( 1 - $new_height / $undistored_height ) * 100/2; |
| 75 |
$this->add_style( 'margin-top', $margin.'%' ); |
| 76 |
$this->add_style( 'margin-bottom', $margin.'%' ); |
| 77 |
|
| 78 |
$this->add_style( 'width', '100%' ); |
| 79 |
$this->add_style( 'height', 'auto' ); |
| 80 |
} elseif ( $new_height > $undistored_height ) { |
| 81 |
// We need a larger image |
| 82 |
$larger_width = $new_height * ($new_width / $undistored_height); |
| 83 |
$larger_thumb = array( |
| 84 |
$larger_width, |
| 85 |
$new_height |
| 86 |
); |
| 87 |
|
| 88 |
$thumb = wp_get_attachment_image_src( $post_image_id, $larger_thumb ); |
| 89 |
$image_url = $thumb[0]; |
| 90 |
|
| 91 |
$margin = ( $new_width - $larger_width ) / 2; |
| 92 |
$this->add_style( 'margin-left', $margin.'px' ); |
| 93 |
$this->add_style( 'margin-right', $margin.'px' ); |
| 94 |
$this->add_style( 'width', $larger_width.'px' ); |
| 95 |
$this->add_style( 'max-width', $larger_width.'px' ); |
| 96 |
$this->add_style( 'height', $new_height.'px' ); |
| 97 |
|
| 98 |
} |
| 99 |
} |
| 100 |
} else if( $this->thumbnail == 'full' ) { |
| 101 |
// Get better thumbnail size based on max possible block size |
| 102 |
$correct_thumb = array( |
| 103 |
$args['max_width'], |
| 104 |
$args['max_height'] |
| 105 |
); |
| 106 |
|
| 107 |
$thumb = wp_get_attachment_image_src( $post_image_id, $correct_thumb ); |
| 108 |
$image_url = $thumb[0]; |
| 109 |
} |
| 110 |
|
| 111 |
$args['desktop'] = $args['desktop'] && $this->show_on_desktop; |
| 112 |
|
| 113 |
$output = $this->before_output(); |
| 114 |
|
| 115 |
ob_start(); |
| 116 |
?> |
| 117 |
<div<?php echo $this->style( 'outer' ); ?>> |
| 118 |
<img src="<?php echo $image_url; ?>" alt="<?php echo esc_attr( get_post_meta( $post_image_id, '_wp_attachment_image_alt', true) ); ?>"<?php echo $this->style(); ?> /> |
| 119 |
</div> |
| 120 |
<?php |
| 121 |
$output .= ob_get_contents(); |
| 122 |
ob_end_clean(); |
| 123 |
|
| 124 |
return $this->after_output( $output, $post ); |
| 125 |
} |
| 126 |
} |