Skip to content

Instantly share code, notes, and snippets.

@rossriley
Created June 29, 2015 13:06
Show Gist options
  • Save rossriley/44880d8359c8efbd81e6 to your computer and use it in GitHub Desktop.
Save rossriley/44880d8359c8efbd81e6 to your computer and use it in GitHub Desktop.
Width/Height to bolt thumbs
<?php
public function parseResizedImages($text) {
$doc = new DOMDocument();
@$doc->loadHTML($text);
$tags = $doc->getElementsByTagName('img');
foreach ($tags as $tag) {
if (substr($tag->getAttribute("src"),0,7) == "/files/") {
if ($wid = $tag->getAttribute("width")) {
$pathParts = pathinfo($tag->getAttribute("src"));
$tag->setAttribute("src", "/thumbs/".$wid."x0/".$path_parts['dirname'].'/'.$pathParts["filename"].'.'.$pathParts["extension"]);
$tag->removeAttribute("width");
$tag->removeAttribute("height");
$text = preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $doc->saveHTML());;
}
}
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment