Skip to content

Instantly share code, notes, and snippets.

@abponcio
Created January 10, 2019 07:25
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 abponcio/23c863911bd0c9d4c77fb6a1a34b41eb to your computer and use it in GitHub Desktop.
Save abponcio/23c863911bd0c9d4c77fb6a1a34b41eb to your computer and use it in GitHub Desktop.
Image Configuration
if (defined('CACHE_LOCAL') && CACHE_LOCAL) {
if (!$filename) {
return;
}
if(!is_file(DIR_IMAGE . $filename) || filesize(DIR_IMAGE . $filename) <= 0) {
$filename = 'no_image.jpg';
}
$info = pathinfo($filename);
list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $filename);
$width = (!$width) ? $width_orig : $width;
$height = (!$height) ? $height_orig : $height;
$extension = $info['extension'];
$old_image = $filename;
$new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type .'.' . $extension;
if (file_exists(DIR_IMAGE . $new_image)) {
list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image);
if ($width_orig != $width || $height_orig != $height) {
$image = new Image(DIR_IMAGE . $old_image);
$image->resize($width, $height, $type);
$image->save(DIR_IMAGE . $new_image);
} else {
copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image);
}
return '/image/'. $new_image;
} else {
$path = '';
$directories = explode('/', dirname(str_replace('../', '', $new_image)));
foreach ($directories as $directory) {
$path = $path . '/' . $directory;
if (!file_exists(DIR_IMAGE . $path)) {
@mkdir(DIR_IMAGE . $path, 0777);
}
}
list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image);
if ($width_orig != $width || $height_orig != $height) {
$image = new Image(DIR_IMAGE . $old_image);
$image->resize($width, $height, $type);
$image->save(DIR_IMAGE . $new_image);
} else {
copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image);
}
return '/image/'. $new_image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment