Hello, I have a bit of html with images. Using DOMDocument I'd like to add class imgSizeFixed if the image is wider than 730px. The code below works perfect except for adding the class. It goes inside the if statement fine, but the class is never added to the image. if(!empty($article)) { include('php/classes/bbCode.php'); $articleText = bbCode::addCodes($article['article']); $html = '<html><body>' . $articleText . '</body></html>'; $dom = new DOMDocument(); $dom->loadHTML($html); $domNodeList = $dom->getElementsByTagName("img"); foreach ($domNodeList as $domNode) { $attrs = array(); $attrs = $domNode->getAttribute('src'); $imageArray = getimagesize($attrs); if(!empty($imageArray) && $imageArray != false) { if($imageArray[0] > 730) { // image to big add class imgSizeFixed $domNode->setAttribute('class', 'imgSizeFixed'); // end image too big add class imgSizeFixed } } } // remove <html><body></html></body> $html = substr($html, 12); $html = substr($html, 0, -14); echo $html; echo '<div style="margin-bottom:48px;"></div>'; } Code (markup):