Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug around SVG dimensions #43

Merged
merged 4 commits into from
Mar 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions safe-svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ function metadata_error_fix( $data, $post_id ) {
/**
* Get SVG size from the width/height or viewport.
*
* @param $svg
* @param string|false $svg The file path to where the SVG file should be, false otherwise.
*
* @return array|bool
*/
Expand All @@ -464,16 +464,43 @@ protected function svg_dimensions( $svg ) {
$height = 0;
if ( $svg ) {
$attributes = $svg->attributes();
if ( isset( $attributes->width, $attributes->height ) && is_numeric( (float)$attributes->width ) && is_numeric( (float)$attributes->height ) ) {
$width = floatval( $attributes->width );
$height = floatval( $attributes->height );
} elseif ( isset( $attributes->viewBox ) ) {

if ( isset( $attributes->viewBox ) ) {
$sizes = explode( ' ', $attributes->viewBox );
if ( isset( $sizes[2], $sizes[3] ) ) {
$width = floatval( $sizes[2] );
$height = floatval( $sizes[3] );
$viewbox_width = floatval( $sizes[2] );
$viewbox_height = floatval( $sizes[3] );
}
}

if ( isset( $attributes->width, $attributes->height ) && is_numeric( (float) $attributes->width ) && is_numeric( (float) $attributes->height ) && ! $this->str_ends_with( (string) $attributes->width, '%' ) && ! $this->str_ends_with( (string) $attributes->height, '%' ) ) {
$attr_width = floatval( $attributes->width );
$attr_height = floatval( $attributes->height );
}

/**
* Decide which attributes of the SVG we use first for image tag dimensions.
*
* We default to using the parameters in the viewbox attribute but
* that can be overridden using this filter if you'd prefer to use
* the width and height attributes.
*
* @hook safe_svg_use_width_height_attributes
*
* @param {bool} $false If the width & height attributes should be used first. Default false.
* @param {string} $svg The file path to the SVG.
*
* @return {bool} If we should use the width & height attributes first or not.
*/
if ( (bool) apply_filters( 'safe_svg_use_width_height_attributes', false, $svg ) ) {
$width = $attr_width;
$height = $attr_height;
} else {
$width = $viewbox_width;
$height = $viewbox_height;
}

if ( ! $width && ! $height ) {
return false;
}
}
Expand Down Expand Up @@ -519,6 +546,30 @@ public function fix_direct_image_output( $attr, $attachment, $size = 'thumbnail'

return $attr;
}

/**
* Polyfill for `str_ends_with()` function added in PHP 8.0.
*
* Performs a case-sensitive check indicating if
* the haystack ends with needle.
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for in the `$haystack`.
* @return bool True if `$haystack` ends with `$needle`, otherwise false.
*/
protected function str_ends_with( $haystack, $needle ) {
if ( function_exists( 'str_ends_with' ) ) {
return str_ends_with( $haystack, $needle );
}

if ( '' === $haystack && '' !== $needle ) {
return false;
}

$len = strlen( $needle );
return 0 === substr_compare( $haystack, $needle, -$len, $len );
}

}
}

Expand Down