Created
June 29, 2015 13:06
-
-
Save rossriley/44880d8359c8efbd81e6 to your computer and use it in GitHub Desktop.
Width/Height to bolt thumbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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