Skip to content

Commit

Permalink
Slugify image cache file
Browse files Browse the repository at this point in the history
Make image cache filename insencible for russian letters, spaces, etc.
Slugify function is taken here: http://stackoverflow.com/questions/2103797/url-friendly-username-in-php/2103815#2103815
  • Loading branch information
toporchillo committed Aug 13, 2013
1 parent a3649ae commit bfdf3d5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions catalog/model/tool/image.php
Expand Up @@ -21,7 +21,7 @@ public function resize($filename, $width, $height, $type = "") {
$extension = $info['extension'];

$old_image = $filename;
$new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type .'.' . $extension;
$new_image = 'cache/' . $this->slugify(utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type .'.' . $extension);

if (!file_exists(DIR_IMAGE . $new_image) || (filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image))) {
$path = '';
Expand Down Expand Up @@ -53,5 +53,13 @@ public function resize($filename, $width, $height, $type = "") {
return $this->config->get('config_url') . 'image/' . $new_image;
}
}

/**
* Slugify string.
* Used to make filename without rusian letters, spaces, etc.
*/
public function slugify($string) {
return strtolower(trim(preg_replace('~[^0-9a-z\.]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}
}
?>
?>

0 comments on commit bfdf3d5

Please sign in to comment.