Skip to content

Instantly share code, notes, and snippets.

@wardpeet
Created November 4, 2013 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wardpeet/7310976 to your computer and use it in GitHub Desktop.
Save wardpeet/7310976 to your computer and use it in GitHub Desktop.
A helper to convert images inside cms to the right width and height
<?php
/**
* A simple helper that converts img tags to it's thumbnails so we don't load to big images. It also does it the way around.
*
* TODO Check if we can reduce number of regexes used.
* @author Ward Peeters <ward@coding-tech.com>
*/
class ContentOptimiser
{
public static function optimise($data)
{
// Check if we have images
if (preg_match_all('/<img([^>]+)>/', $data, $matches) > 0) {
$iconFiles = ThumbnailCreator::getIcons();
foreach ($matches[1] as $match) {
// just check if we have a width or height
if (preg_match('/src="([^"]+)".*(height:\s?\d+px;|width:\s?\d+px;|height="\d+"|width="\d+")/', $match, $source)) {
$info = pathinfo($source[1]);
list($width, $height) = self::getSize($match);
// only do this when we have width or height
if (($width || $height) && !in_array($info['filename'], $iconFiles)) {
list($origWidth, $origHeight) = @getimagesize(BASEPATH . preg_replace('|' . BASEURL . '|', '', $source[1], 1));
// and not the same as default size
if (($width && $origWidth != $width) || ($height && $origHeight != $height)) {
$newSrc = $info['dirname'] . '/' . sprintf('%s-%dx%d.%s', $info['filename'], $width, $height, $info['extension']);
$data = preg_replace('|src="' . $source[1] . '"|', 'src="' . $newSrc . '"', $data, 1);
}
}
}
}
}
return $data;
}
public static function revert($data)
{
if (preg_match_all('/<img.*src=".*-(\d+)x(\d+)([^>]+)">/', $data, $matches) > 0) {
foreach ($matches[0] as $key => $match) {
// split all attributes out
preg_match_all('/(\w+)\s*=\s*("[^"]*"|\'[^\']*\'|[^"\'\s>]*)/', $match, $attributes);
$attrString = '';
$hasStyles = $widthOrHeight = false;
// just check attributes for width and height
foreach ($attributes[1] as $k => $attr) {
if (isset($attributes[2][$k])) {
if ($attr == 'src' && ($matches[1][$key] || $matches[2][$key])) {
$attributes[2][$k] = str_replace('-' . $matches[1][$key] . 'x' . $matches[2][$key], '', $attributes[2][$k]);
} else if ($attr == 'width' && $matches[1][$key]) {
$attributes[2][$k] = $matches[1][$key];
$widthOrHeight = true;
} else if ($attr == 'height' && $matches[1][$key]) {
$attributes[2][$k] = $matches[2][$key];
$widthOrHeight = true;
} else if ($attr == 'style') {
$hasStyles = true;
if (preg_match_all('/(width:\s?(\d+)px;|height:\s?(\d+)px;)/', $attributes[2][$k], $style)) {
$widthOrHeight = true;
// replace width and height in style
foreach ($style[0] as $l => $v) {
if (strpos($v, 'height') !== false && $matches[2][$key]) {
$attributes[2][$k] = str_replace($v, 'height: ' . $matches[2][$key] . 'px;', $attributes[2][$k]);
} else if (strpos($v, 'width') !== false && $matches[1][$key]) {
$attributes[2][$k] = str_replace($v, 'width: ' . $matches[1][$key] . 'px;', $attributes[2][$k]);
}
}
} else { // couldn't find height or width
if ($matches[1][$key]) {
$attributes[2][$k] .= 'width: ' . $matches[2][$key] . 'px;';
}
if ($matches[2][$key]) {
$attributes[2][$k] .= 'height: ' . $matches[2][$key] . 'px;';
}
}
}
$attrString .= ' ' . $attr . '="' . trim($attributes[2][$k], '"') . '"';
}
}
// if no width or height is found in the element than we should add it ourselves
if (!$widthOrHeight && !$hasStyles) {
$styleString = '';
if ($matches[1][$key]) {
$styleString .= 'width: ' . $matches[2][$key] . 'px;';
}
if ($matches[2][$key]) {
$styleString .= 'height: ' . $matches[2][$key] . 'px;';
}
if ($styleString) {
$attrString .= ' style="' . $styleString . '"';
}
}
$newImage = '<img' . $attrString . ' />';
$data = str_replace($match, $newImage, $data);
}
}
return $data;
}
protected static function getSize($line)
{
preg_match('/width="(\d+)"|width:\s?(\d+)px;?/', $line, $widths);
preg_match('/height="(\d+)"|height:\s?(\d+)px;?/', $line, $heights);
return array(
(int) $widths ? (count($widths) > 2 ? $widths[2] : $widths[1]) : 0,
(int) $heights ? (count($heights) > 2 ? $heights[2] : $heights[1]) : 0,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment