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