/catalog/model/tool/image.php
PHP | 52 lines | 42 code | 10 blank | 0 comment | 13 complexity | 2d9296fae2c8ba1136649c736a3b773c MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
- <?php
- class ModelToolImage extends Model {
- public function resize($filename, $width, $height) {
- if (!is_file(DIR_IMAGE . $filename)) {
- if (is_file(DIR_IMAGE . 'no_image.jpg')) {
- $filename = 'no_image.jpg';
- } elseif (is_file(DIR_IMAGE . 'no_image.png')) {
- $filename = 'no_image.png';
- } else {
- return;
- }
- }
- $extension = pathinfo($filename, PATHINFO_EXTENSION);
- $old_image = $filename;
- $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
- if (!is_file(DIR_IMAGE . $new_image) || (filectime(DIR_IMAGE . $old_image) > filectime(DIR_IMAGE . $new_image))) {
- $path = '';
- $directories = explode('/', dirname(str_replace('../', '', $new_image)));
- foreach ($directories as $directory) {
- $path = $path . '/' . $directory;
- if (!is_dir(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);
- $image->save(DIR_IMAGE . $new_image);
- } else {
- copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image);
- }
- }
- $imagepath_parts = explode('/', $new_image);
- $new_image = implode('/', array_map('rawurlencode', $imagepath_parts));
-
- if ($this->request->server['HTTPS']) {
- return $this->config->get('config_ssl') . 'image/' . $new_image;
- } else {
- return $this->config->get('config_url') . 'image/' . $new_image;
- }
- }
- }