Post by prototype0104 » Tue Sep 14, 2021 7:14 pm

Hi guys I checked every topic with that subject but didn;t found a solution.
In my database all my image urls are to an external link (https://mydomain/image/sdsad/sdaas/image.jpg)
I saw that controllers use resize function located in model/tools/image.
What changes I need to make that OpenCart will access https://mydomain/image/sdsad/sdaas/image.jpg and will create locale cached files?
My image.php:

Code: Select all

<?php
class ModelToolImage extends Model {
	public function resize($filename, $width, $height) {

                if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
                    $server = HTTPS_SERVER;
                } else {
                    $server = HTTP_SERVER;
                }

                if ($filename) {
                    $image_info = @getimagesize(DIR_IMAGE . $filename);
                    if (!$image_info) {
                        return $server . 'image/' . $filename;
                    }
                } else {
                    $filename = "no_image.png";
                }
                
		if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != str_replace('\\', '/', DIR_IMAGE)) {
			
		
			if (preg_match('/https?:\/\//', $filename)) return $filename;
			else return;
		
			
		}

		$extension = pathinfo($filename, PATHINFO_EXTENSION);

		$image_old = $filename;
		$image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.' . $extension;

		if (!is_file(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $image_old) > filemtime(DIR_IMAGE . $image_new))) {
			list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old);
				 
			if (!in_array($image_type, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) { 
				return DIR_IMAGE . $image_old;
			}
						
			$path = '';

			$directories = explode('/', dirname($image_new));

			foreach ($directories as $directory) {
				$path = $path . '/' . $directory;

				if (!is_dir(DIR_IMAGE . $path)) {
					@mkdir(DIR_IMAGE . $path, 0777);
				}
			}

			if ($width_orig != $width || $height_orig != $height) {
				$image = new Image(DIR_IMAGE . $image_old);
				$image->resize($width, $height);
				$image->save(DIR_IMAGE . $image_new);
			} else {
				copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new);
			}
		}
		
		$image_new = str_replace(' ', '%20', $image_new);  // fix bug when attach image on email (gmail.com). it is automatic changing space " " to +
		
		if ($this->request->server['HTTPS']) {
			return 'https://atvmotoshop.ro/image/' . $image_new;
		} else {
			return 'https://atvmotoshop.ro/image/' . $image_new;
		}
	}
}

Active Member

Posts

Joined
Sun Mar 24, 2019 6:08 pm
Location - Romania

Post by Gergely » Tue Sep 14, 2021 7:55 pm

Hi prototype0104,

If you can't find another solution, you can take a look at this free extension.

Active Member

Posts

Joined
Wed Sep 30, 2020 7:58 pm

Post by prototype0104 » Tue Sep 14, 2021 8:35 pm

This module still not work for me..
1) it will return the original image url
2) it's not even returning the original image url, is returning: "https://mydomain.com/image/https://imageurl...."

EDIT:
Does anyone now if Image function from system/library/image.php is only for local files?
Because I want to use $image = new Image(imageurl)

Active Member

Posts

Joined
Sun Mar 24, 2019 6:08 pm
Location - Romania

Post by paulfeakins » Wed Sep 15, 2021 6:49 pm

If you can't find an extension, you could pay a developer such as ourselves or post a job in the Commercial Support Forum.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Guru Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom
Who is online

Users browsing this forum: SohBH and 275 guests