· 7 years ago · Sep 26, 2018, 10:16 PM
1$extension = pathinfo($filename, PATHINFO_EXTENSION);
2
3 $image_old = $filename;
4 $image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;
5
6 if (!is_file(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $image_old) > filemtime(DIR_IMAGE . $image_new))) {
7 list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old);
8
9 if (!in_array($image_type, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) {
10 return DIR_IMAGE . $image_old;
11 }
12
13 $path = '';
14
15 $directories = explode('/', dirname($image_new));
16
17 foreach ($directories as $directory) {
18 $path = $path . '/' . $directory;
19
20 if (!is_dir(DIR_IMAGE . $path)) {
21 @mkdir(DIR_IMAGE . $path, 0777);
22 }
23 }
24
25 if ($width_orig != $width || $height_orig != $height) {
26 $image = new Image(DIR_IMAGE . $image_old);
27 $image->resize($width, $height);
28 $image->save(DIR_IMAGE . $image_new);
29 } else {
30 copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
31 }
32 }
33
34 $image_new = str_replace(' ', '%20', $image_new); // fix bug when attach image on email (gmail.com). it is automatic changing space " " to +
35
36 if ($this->request->server['HTTPS']) {
37 return $this->config->get('config_ssl') . 'image/' . $image_new;
38 } else {
39 return $this->config->get('config_url') . 'image/' . $image_new;
40 }
41}