Skip to content

Commit

Permalink
Add more helpful functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gchtr committed Feb 21, 2022
1 parent ab65831 commit 8f15e0c
Showing 1 changed file with 94 additions and 21 deletions.
115 changes: 94 additions & 21 deletions lib/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,12 @@ public function width() {
return false;
}
} else {
list( $width ) = Helper::get_dimensions_upscale( $width, $height, [
'upscale' => $this->upscale,
'resize' => $this->size['resize'],
'max_width' => $this->max_width(),
'max_height' => $this->max_height(),
] );
list( $width ) = Helper::get_dimensions_upscale( $width, $height, [
'upscale' => $this->upscale,
'resize' => $this->size['resize'],
'max_width' => $this->max_width(),
'max_height' => $this->max_height(),
] );
}

return $width;
Expand All @@ -506,12 +506,12 @@ public function height() {
}
} else {
$height = Helper::maybe_fix_height( $height, $width, $this->max_width(), $this->max_height() );
list( , $height ) = Helper::get_dimensions_upscale( $width, $height, [
'upscale' => $this->upscale,
'resize' => $this->size['resize'],
'max_width' => $this->max_width(),
'max_height' => $this->max_height(),
] );
list( , $height ) = Helper::get_dimensions_upscale( $width, $height, [
'upscale' => $this->upscale,
'resize' => $this->size['resize'],
'max_width' => $this->max_width(),
'max_height' => $this->max_height(),
] );
}

return $height;
Expand All @@ -533,6 +533,51 @@ public function aspect_ratio() {
return 0;
}

/**
* Checks whether an image is a portrait image.
*
* @return bool
*/
public function is_landscape() {
return $this->aspect_ratio() > 1;
}

/**
* Checks whether an image is a portrait image.
*
* @return bool
*/
public function is_portrait() {
return $this->aspect_ratio() < 1;
}

/**
* Checks whether an image is exactly square.
*
* @return bool
*/
public function is_square() {
return $this->aspect_ratio() === 1;
}

/**
* Checks whether an image is pretty much a square image with an allowed
* deviation of +- 2%.
*
* @return bool
*/
public function is_squarish( $deviation = 0.02 ) {
// Prevent division by 0.
if ( $this->height() === 0 ) {
return false;
}

$aspect = $this->aspect_ratio();

// Check for squareness with a 2% deviation.
return $aspect > ( 1 - $deviation ) && $aspect < ( 1 + $deviation );
}

/**
* Gets value for loading attributes.
*
Expand Down Expand Up @@ -600,12 +645,12 @@ public function responsive_attributes( $args = [] ) {
* @since 0.12.0
*/
$default_args = [
'attr_width' => true,
'attr_height' => true,
'lazy_srcset' => false,
'lazy_src' => false,
'lazy_sizes' => false,
'loading' => 'lazy',
'attr_width' => true,
'attr_height' => true,
'lazy_srcset' => false,
'lazy_src' => false,
'lazy_sizes' => false,
'loading' => 'lazy',
'to_webp' => $this->is_webp(),
'src_default' => true,
];
Expand Down Expand Up @@ -634,10 +679,10 @@ public function responsive_attributes( $args = [] ) {
$attributes['srcset'] = $srcset;

if ( $args['src_default'] ) {
$attributes['src'] = $this->src_default();
$attributes['src'] = $this->src_default();
}

$attributes['sizes'] = $this->sizes();
$attributes['sizes'] = $this->sizes();
} else {
$attributes['src'] = $this->src( [ 'to_webp' => $args['to_webp'] ] );
}
Expand All @@ -652,7 +697,7 @@ public function responsive_attributes( $args = [] ) {

if ( $args['attr_height'] ) {
$attributes['height'] = $this->height();
$attributes['style'] = false;
$attributes['style'] = false;
}

// Lazy-loading.
Expand Down Expand Up @@ -761,14 +806,42 @@ public function mime_type() {
return null;
}

/**
* Checks whether an image will be converted to WebP.
*
* @return bool
*/
public function is_webp() {
return isset( $this->size['webp'] )
&& $this->size['webp']
&& ! $this->is_svg()
&& ! $this->is_pdf();
}

/**
* Checks whether an image is an SVG image.
*
* @return bool
*/
public function is_svg() {
return 'image/svg+xml' === $this->mime_type();
}

/**
* Checks whether an image is a GIF.
*
* @return bool
*/
public function is_gif() {
return 'image/gif' === $this->mime_type();
}

/**
* Checks whether an image is a PDF.
*
* @return bool
*/
public function is_pdf() {
return 'application/pdf' === $this->mime_type();
}
}

0 comments on commit 8f15e0c

Please sign in to comment.