0
votes

I am trying to upload an image from customer product page (upload file option), the image is uploaded to system/upload folder and renamed with: filename + md5 hash string. So, for example I upload image.jpg, the file will be uploaded to system/upload/image.jpg.392da1267fbfa4be65f7859bb0b974d9.

Now I want to see that image uploaded by customer from admin page (I created a new module page), how can I resize and display the image file? I can get the file name from table oc_upload in database, but the phisical file is using modified name which has no extension, I cannot resize this file because it is located in system/upload folder and its extension is changed. The image resize tool can only resize images in image folder with some allowed extensions configured in Settings page.

Any idea how to display images uploaded by customer? Do I need to copy the renamed uploaded file to image folder using its original name and then resize and display it? I appreciate any of your ideas. Thank you.

1
I don't understand why you want to resize the file itself. You could just use CSS properties width and height on an <img> tag, couldn't you ?Lily Bergonzat
Resize to generate the thumbnails of the images.Muhammad Kholid B

1 Answers

0
votes

Just got the solution by copying the resize() method in admin/model/tool/image.php to my controller and modify it to resize image from system/upload folder.

    private function resizeImageUpload($old_filename, $width, $height, $new_filename = '') {

            $extension_arr = array('jpeg', 'jpg', 'png', 'gif');

            $old_image = $old_filename;

            if (!empty($new_filename)) {
                $extension = pathinfo($new_filename, PATHINFO_EXTENSION);
                $new_image = 'cache/system/upload/' . utf8_substr($new_filename, 0, utf8_strrpos($new_filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
            } else {
                $extension = pathinfo($old_filename, PATHINFO_EXTENSION);
                $new_image = 'cache/system/upload/' . utf8_substr($old_filename, 0, utf8_strrpos($old_filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
            }

            if (!in_array($extension, $extension_arr)) return '';

            if (!is_file(DIR_IMAGE . $new_image) || (filectime(DIR_UPLOAD . $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_UPLOAD . $old_image);

                if ($width_orig != $width || $height_orig != $height) {
                        $image = new Image(DIR_UPLOAD . $old_image);
                        $image->resize($width, $height);
                        $image->save(DIR_IMAGE . $new_image);
                } else {
                        $success = copy(DIR_UPLOAD . $old_image, DIR_IMAGE . $new_image);
                }
            }

        if ($this->request->server['HTTPS']) {
            return HTTPS_CATALOG . 'image/' . $new_image;
        } else {
            return HTTP_CATALOG . 'image/' . $new_image;
        }
    }