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

Images should be shrunk to 600px or less #152

Open
boonebgorges opened this issue Aug 16, 2018 · 2 comments
Open

Images should be shrunk to 600px or less #152

boonebgorges opened this issue Aug 16, 2018 · 2 comments

Comments

@boonebgorges
Copy link
Owner

The standard width for BP email templates - and email templates in general - is 600px. Due to technical limitations in email clients, this is the widest width that's practical for cross-platform messages.

When an activity item included in a digest contains an image, and image dimensions (either explicit with width attribute or implicit via the original image width) are greater than 600px wide, it can expand the viewport in some clients - notwithstanding the max-width: 600px declaration on the email container.

I think we can avoid this by doing a bit of preprocessing magic when building the email markup. Namely:

  • Look for <img> elements
  • If an element doesn't have a width, or has width wider than 600, shrink to 600 (and remove height to avoid distortion)

I can work on a patch for this but first I wanted to get a sanity check from @r-a-y that this is a sensible way forward.

@boonebgorges
Copy link
Owner Author

I opened a BP ticket on the chance that we want to handle this upstream https://buddypress.trac.wordpress.org/ticket/7948#ticket

@boonebgorges
Copy link
Owner Author

Here's what I'm using on the client site for the time being:

/**
 * Prevent inline images wider than 600px in digests.
 */
function bbg_shrink_ges_images( $content ) {
	$dom = new domDocument;
	$dom->loadHTML(
		$content,
		LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
	);

	$imgs = $dom->getElementsByTagName( 'img' );
	foreach ( $imgs as $img ) {
		$width = null;

		$width_el = $img->attributes->getNamedItem( 'width' );
		if ( $width_el ) {
			$width = (int) $width_el->value;
		}

		if ( $width && $width <= 600 ) {
			continue;
		}

		$img->setAttribute( 'width', '600' );
	}


	$content = $dom->saveHTML();

	return $content;
}
add_filter( 'ass_digest_content', 'bbg_shrink_ges_images' );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant