Skip to content

Commit

Permalink
refactor svg_dimensions function to calculate dimensions using existi…
Browse files Browse the repository at this point in the history
…ng metadata
  • Loading branch information
sksaju committed Sep 25, 2023
1 parent 62c2be8 commit ebc0531
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions safe-svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ protected function is_gzipped( $contents ) {
public function fix_admin_preview( $response, $attachment, $meta ) {

if ( 'image/svg+xml' === $response['mime'] ) {
$dimensions = $this->svg_dimensions( get_attached_file( $attachment->ID ) );
$dimensions = $this->svg_dimensions( $attachment->ID );

if ( $dimensions ) {
$response = array_merge( $response, $dimensions );
Expand Down Expand Up @@ -384,7 +384,7 @@ public function fix_admin_preview( $response, $attachment, $meta ) {
*/
public function one_pixel_fix( $image, $attachment_id, $size, $icon ) {
if ( get_post_mime_type( $attachment_id ) === 'image/svg+xml' ) {
$dimensions = $this->svg_dimensions( get_attached_file( $attachment_id ) );
$dimensions = $this->svg_dimensions( $attachment_id );

if ( $dimensions ) {
$image[1] = $dimensions['width'];
Expand Down Expand Up @@ -445,7 +445,7 @@ public function get_image_tag_override( $html, $id, $alt, $title, $align, $size
$width = $size[0];
$height = $size[1];
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
} elseif ( 'full' === $size && $dimensions = $this->svg_dimensions( get_attached_file( $id ) ) ) {
} elseif ( 'full' === $size && $dimensions = $this->svg_dimensions( $id ) ) {
$width = $dimensions['width'];
$height = $dimensions['height'];
} else {
Expand Down Expand Up @@ -485,7 +485,7 @@ public function skip_svg_regeneration( $metadata, $attachment_id ) {
$relative_path = str_replace( trailingslashit( $upload_dir['basedir'] ), '', $svg_path );
$filename = basename( $svg_path );

$dimensions = $this->svg_dimensions( $svg_path );
$dimensions = $this->svg_dimensions( $attachment_id );

if ( ! $dimensions ) {
return $metadata;
Expand Down Expand Up @@ -564,15 +564,22 @@ public function metadata_error_fix( $data, $post_id ) {
*
* @return array|bool
*/
protected function svg_dimensions( $svg ) {
protected function svg_dimensions( $attachment_id ) {
if ( ! function_exists( 'simplexml_load_file' ) ) {
return false;
}

$svg = @simplexml_load_file( $svg ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$width = 0;
$height = 0;
if ( $svg ) {
$svg = get_attached_file( $attachment_id );
$metadata = wp_get_attachment_metadata( $attachment_id );
$width = 0;
$height = 0;

if ( $svg && ! empty( $metadata['width'] ) && empty( $metadata['height'] ) ) {
$width = floatval( $metadata['width'] );
$height = floatval( $metadata['height'] );
} else if ( $svg ) {
$svg = @simplexml_load_file( $svg ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged

$attributes = $svg->attributes();

if ( isset( $attributes->viewBox ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
Expand Down Expand Up @@ -627,11 +634,21 @@ protected function svg_dimensions( $svg ) {
}
}

return array(
$dimensions = array(
'width' => $width,
'height' => $height,
'orientation' => ( $width > $height ) ? 'landscape' : 'portrait',
);

/**
* Calculate SVG dimensions and orientation.
*
* @param array $dimensions An array containing width, height, and orientation.
* @param string $svg The file path to the SVG.
*
* @return array An array of SVG dimensions and orientation.
*/
return apply_filters( 'safe_svg_dimensions', $dimensions, $svg );
}

/**
Expand Down

0 comments on commit ebc0531

Please sign in to comment.