GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-image.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Generic class for generating image tag
4  */
6 
7  var $alt;
8  var $src;
9  var $title;
10  var $width;
11  var $height;
12 
13  /**
14  * HTML class attribute.
15  * @var string
16  */
17  var $class = NULL;
18 
19  /**
20  * HTML style attribute
21  * @var string
22  */
23  var $style = NULL;
24 
25  /**
26  * String representing size of image - Choose from "full", "medium", "thumb", "tiny"
27  * @var string
28  */
29  var $size = NULL;
30 
31  /**
32  * Use PHP getimagesize function to auto-calculate image size?
33  * @var boolean
34  */
35  var $getimagesize = false;
36 
37  /**
38  * Check the src to make sure it looks like a valid image URL
39  * @var boolean
40  */
41  var $validate_src = true;
42 
43  /**
44  * Handle being treated as a string by returning the HTML
45  * @return string HTML of image
46  */
47  function __toString() {
48  return $this->html();
49  }
50 
51 
52  function __construct( $atts = array() ) {
53 
54  $defaults = array(
55  'width' => $this->width,
56  'height' => $this->height,
57  'alt' => $this->alt,
58  'title' => $this->title,
59  'size' => $this->size,
60  'src' => $this->src,
61  'class' => $this->class,
62  'getimagesize' => false,
63  'validate_src' => true
64  );
65 
66  $atts = wp_parse_args( $atts, $defaults );
67 
68  foreach( $atts as $key => $val ) {
69  $this->{$key} = $val;
70  }
71 
72  $this->class = !empty( $this->class ) ? esc_attr( implode( ' ', (array)$this->class ) ) : $this->class;
73 
74  $this->set_image_size();
75 
76  }
77 
78  /**
79  * Verify that the src URL matches image patterns.
80  *
81  * Yes, images can not have extensions, but this is a basic check. To disable this,
82  * set `validate_src` to false when instantiating the object.
83  *
84  * @return boolean True: matches pattern; False: does not match pattern.
85  */
86  function validate_image_src() {
87 
88  if ( !$this->validate_src ) { return true; }
89 
90  $info = pathinfo( $this->src );
91 
92  $image_exts = self::get_image_extensions();
93 
94  return isset( $info['extension'] ) && in_array( strtolower( $info['extension'] ), $image_exts);
95  }
96 
97  /**
98  * Returns an array of file extensions recognized by GravityView as images.
99  *
100  * @since 2.14.3
101  *
102  * @return array
103  */
104  public static function get_image_extensions() {
105 
106  /**
107  * @filter `gravityview_image_extensions` Extensions that GravityView recognizes as valid images to be shown in an `img` tag
108  * @param array $image_exts Default: `['jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'webp']`
109  */
110  $image_exts = apply_filters( 'gravityview_image_extensions', array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'tif', 'tiff', 'ico', 'webp' ) );
111 
112  return (array) $image_exts;
113  }
114 
115  /**
116  * Get default widths and heights for image size.
117  *
118  * @return void
119  */
120  public function set_image_size( $string = NULL, $width = NULL, $height = NULL ) {
121 
122 
123  // If there is no width or height passed
124  if ( empty( $width ) || empty( $height ) ) {
125 
126  // And there is no string size passed
127  // And we want to get the image size using PHP
128  if ( empty( $string ) && !empty( $this->getimagesize ) ) {
129 
130  $image_size = @getimagesize( $this->src );
131 
132  // If it didn't return a response, it may be a HTTPS/SSL error
133  if ( empty( $image_size[0] ) ) {
134  $image_size = @getimagesize( set_url_scheme( $this->src, 'http' ) );
135  }
136 
137  if ( !empty( $image_size ) ) {
138  list( $width, $height ) = $image_size;
139  }
140 
141  }
142  // Otherwise, we calculate based on the string size value
143  else {
144 
145  /**
146  * @filter `gravityview_image_sizes` Modify the image size presets used by GravityView_Image class
147  * @param array $image_sizes Array of image sizes with the key being the size slug, and the value being an array with `width` and `height` defined, in pixels
148  */
149  $image_sizes = apply_filters( 'gravityview_image_sizes', array(
150  'tiny' => array('width' => 40, 'height' => 30),
151  'small' => array('width' => 100, 'height' => 75),
152  'medium' => array('width' => 250, 'height' => 188),
153  'large' => array('width' => 448, 'height' => 336),
154  ) );
155 
156  switch( $this->size ) {
157  case 'tiny':
158  extract($image_sizes['tiny']);
159  break;
160  case 'small':
161  case 's':
162  case 'thumb':
163  extract($image_sizes['small']);
164  break;
165  case 'm':
166  case 'medium':
167  extract($image_sizes['medium']);
168  break;
169  case 'large':
170  case 'l':
171  extract($image_sizes['large']);
172  break;
173  default:
174  // Verify that the passed sizes are integers.
175  $width = !empty( $width ) ? intval( $width ) : intval( $this->width );
176  $height = !empty( $height ) ? intval( $height ) : intval( $this->height );
177  }
178 
179  }
180 
181  }
182 
183  $this->width = $width;
184  $this->height = $height;
185  }
186 
187  /**
188  * Return the HTML tag for the image
189  * @return string HTML of the image
190  */
191  public function html() {
192 
193  $html = '';
194 
195  if ( $this->validate_image_src() && ! empty( $this->src ) ) {
196  $atts = '';
197  foreach ( array( 'width', 'height', 'alt', 'title', 'class' ) as $attr ) {
198 
199  if ( empty( $this->{$attr} ) ) { continue; }
200 
201  $atts .= sprintf( ' %s="%s"', $attr, esc_attr( $this->{$attr} ) );
202  }
203 
204  $html = sprintf( '<img src="%s"%s />', esc_url_raw( $this->src ), $atts );
205  }
206 
207  /**
208  * @filter `gravityview_image_html` Filter the HTML image output
209  * @param string $html the generated image html
210  * @param GravityView_Image $this The current image object
211  */
212  return apply_filters( 'gravityview_image_html', $html, $this );
213  }
214 }
Generic class for generating image tag.
html()
Return the HTML tag for the image.
__toString()
Handle being treated as a string by returning the HTML.
validate_image_src()
Verify that the src URL matches image patterns.
set_image_size( $string=NULL, $width=NULL, $height=NULL)
Get default widths and heights for image size.
static get_image_extensions()
Returns an array of file extensions recognized by GravityView as images.