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

SVG height and width attributes #6

Open
jeremymoore opened this issue Jul 31, 2019 · 1 comment · May be fixed by #216
Open

SVG height and width attributes #6

jeremymoore opened this issue Jul 31, 2019 · 1 comment · May be fixed by #216
Assignees
Labels
help wanted Extra attention is needed type:enhancement New feature or request.
Milestone

Comments

@jeremymoore
Copy link

Hi @darylldoyle

What are your thoughts on trying to assign the default height and width attributes based on the image size used in the wp_get_attachment_image() function?

echo wp_get_attachment_image( $svg_id, [78, 78], true, [
     'class' => 'img-fluid',
     'alt' => 'icon',
] );

For example, when I use the above in my template with your plugin activated my SVG is loaded with the height and width attributes set to the images default size. I, however, would like the height and width to be 78x78.

I ended up using the following filter in my theme to work around the issue... but i think this would be worth considering incorporating into the plugin.

add_filter( 'wp_get_attachment_image_attributes', 'wwc_fix_direct_image_output', 11, 3 );
function wwc_fix_direct_image_output( $attr, $attachment, $size = 'thumbnail' ) {

	// If we're not getting a WP_Post object, bail early.
	// @see https://wordpress.org/support/topic/notice-trying-to-get-property-id/
	if ( ! $attachment instanceof WP_Post ) {
		return $attr;
	}

	$mime = get_post_mime_type( $attachment->ID );
	if ( 'image/svg+xml' === $mime ) {
		$default_height = 100;
		$default_width  = 100;

		$size = image_downsize($attachment->ID, $size);
		
		if (is_array($size) && sizeof($size) >= 3) {
			$attr['height'] = $size[2];
			$attr['width']  = $size[1];
		}
	}

	return $attr;
}```
@jeffpaul jeffpaul added this to the Future Release milestone Aug 31, 2021
@jeffpaul jeffpaul added type:question Further information is requested. type:enhancement New feature or request. labels Aug 31, 2021
@darylldoyle
Copy link
Collaborator

Thanks for the info @jeremymoore.

I think this could be a great enhancement moving forward, especially taking CLS into account now.

@jeffpaul this could work by wrapping lines 471-476 in a conditional that checks for the full size and otherwise returning the size requested as above.

We should also take into account that $size may be an array rather than a string with a named size.

@jeffpaul jeffpaul added help wanted Extra attention is needed and removed type:question Further information is requested. labels Aug 31, 2021
@jayedul jayedul self-assigned this Feb 1, 2023
@gabriel-glo gabriel-glo self-assigned this Jul 22, 2024
@gabriel-glo gabriel-glo linked a pull request Jul 22, 2024 that will close this issue
4 tasks
@gabriel-glo gabriel-glo linked a pull request Jul 22, 2024 that will close this issue
4 tasks
@jeffpaul jeffpaul modified the milestones: Future Release, 2.3.0 Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed type:enhancement New feature or request.
Projects
Status: Code Review
Development

Successfully merging a pull request may close this issue.

5 participants