Skip to content

Instantly share code, notes, and snippets.

@arminrosu
Last active December 15, 2015 10:19
Show Gist options
  • Save arminrosu/5245226 to your computer and use it in GitHub Desktop.
Save arminrosu/5245226 to your computer and use it in GitHub Desktop.
Parse css for orphan images (i.e. on disk but not in css)
<pre><?php
class OrphanFinder {
private $imagesDir = "";
private $imagesUrl = ""; // base path
private $stylesheetsDir = "";
function __construct($imagesDir, $imagesUrl, $stylesheetsDir) {
$this->imagesDir = $imagesDir;
$this->imagesUrl = $imagesUrl;
$this->stylesheetsDir = $stylesheetsDir;
}
public function getOrphans() {
$css = $this->getCSS();
$assetUrls = $this->getAssetUrls( $css );
$images = $this->getImages();
$cleanedUrls = array();
$missingDiskAssets = array();
// convert url to dir path
foreach( $assetUrls as $assetUrl) :
// we don't handle relative urls (for now)
if ( strpos($assetUrl, $this->imagesUrl) !== false ) :
$cleanedUrls[] = str_replace($this->imagesUrl, $this->imagesDir, $assetUrl);
else :
$missingDiskAssets[] = $assetUrl;
endif;
endforeach;
$missingCSSImages = array_diff($images, $cleanedUrls);
sort($missingCSSImages);
return array(
// images on disk but not in css
'css' => $missingCSSImages,
// assets in css but not on disk
'disk' => $missingDiskAssets
);
}
// CSS
private function getCSS() {
echo "Found these stylesheets:\n";
$css = "";
$stylesheets = $this->getStylesheets();
foreach( $stylesheets as $stylesheet ) :
echo "$stylesheet\n";
$css .= file_get_contents($stylesheet);
endforeach;
return $css;
}
// gets all assets referenced in the css via url()
function getAssetUrls( $css ) {
$matched = preg_match_all('#url\([^)]+\)#i', $css, $matches);
if ( $matched ) :
$assets = array_unique($matches[0]);
// do some cleanup
foreach( $assets as $key => $asset) :
$asset = preg_replace('~\?[^)]+|#[^)]+|\'|"~', "", $asset);
$asset = preg_replace('#url\(|\)#', "", $asset);
$assets[$key] = $asset;
endforeach;
$assets = array_unique($assets);
sort($assets);
return $assets;
else :
echo "ERROR: no assets found in stylesheets \n";
die();
endif;
}
private function getStylesheets() {
// get all directories
$directories = $this->getDirectories($this->stylesheetsDir);
$stylesheets = array();
foreach( $directories as $directory) :
$assets = glob( $directory . "/*.*");
$stylesheets = array_merge($stylesheets, $assets);
endforeach;
return $stylesheets;
}
// IMAGES
private function getImages() {
$directories = $this->getDirectories($this->imagesDir);
$images = array();
foreach( $directories as $directory ) :
$assets = glob( $directory . "/*.*");
$images = array_merge($images, $assets);
endforeach;
return $images;
}
// HELPERS
private function getDirectories( $rootDir ) {
$directories = glob( $rootDir . "/*", GLOB_ONLYDIR);
if ( count($directories) > 0 ) :
foreach( $directories as $directory ) :
$directories = array_merge($directories, $this->getDirectories( $directory));
endforeach;
endif;
$directories[] = $rootDir;
return array_unique($directories);
}
}
// example usage
$images_dir = "~/Sites/example/images";
$images_url = "http://example.com/images";
$stylesheets_dir = "~/Sites/example/css";
$orphanFinder = new OrphanFinder($images_dir, $images_url, $stylesheets_dir);
$orphans = $orphanFinder->getOrphans();
var_dump($orphans);
?></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment