1: | <?php
2: | namespace Opencart\Catalog\Model\Tool;
3: | /**
4: | * Class Image
5: | *
6: | * @package Opencart\Catalog\Model\Tool
7: | */
8: | class Image extends \Opencart\System\Engine\Model {
9: | /**
10: | * Resize
11: | *
12: | * @param string $filename
13: | * @param int $width
14: | * @param int $height
15: | * @param string $default
16: | *
17: | * @throws \Exception
18: | *
19: | * @return string
20: | */
21: | public function resize(string $filename, int $width, int $height, string $default = ''): string {
22: | $filename = html_entity_decode($filename, ENT_QUOTES, 'UTF-8');
23: |
24: | if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != DIR_IMAGE) {
25: | return '';
26: | }
27: |
28: | $extension = pathinfo($filename, PATHINFO_EXTENSION);
29: |
30: | $image_old = $filename;
31: | $image_new = 'cache/' . oc_substr($filename, 0, oc_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;
32: |
33: | if (!is_file(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $image_old) > filemtime(DIR_IMAGE . $image_new))) {
34: | [$width_orig, $height_orig, $image_type] = getimagesize(DIR_IMAGE . $image_old);
35: |
36: | if (!in_array($image_type, [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_WEBP])) {
37: | return $this->config->get('config_url') . 'image/' . $image_old;
38: | }
39: |
40: | $path = '';
41: |
42: | $directories = explode('/', dirname($image_new));
43: |
44: | foreach ($directories as $directory) {
45: | if (!$path) {
46: | $path = $directory;
47: | } else {
48: | $path = $path . '/' . $directory;
49: | }
50: |
51: | if (!is_dir(DIR_IMAGE . $path)) {
52: | @mkdir(DIR_IMAGE . $path, 0777);
53: | }
54: | }
55: |
56: | if ($width_orig != $width || $height_orig != $height) {
57: | $image = new \Opencart\System\Library\Image(DIR_IMAGE . $image_old);
58: | $image->resize($width, $height, $default);
59: | $image->save(DIR_IMAGE . $image_new);
60: | } else {
61: | copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
62: | }
63: | }
64: |
65: | $image_new = str_replace(' ', '%20', $image_new); // fix bug when attach image on email (gmail.com). it is automatically changing space from " " to +
66: |
67: | return $this->config->get('config_url') . 'image/' . $image_new;
68: | }
69: | }
70: |

OpenCart API API documentation generated by ApiGen dev-master