Back to Top

Difference Between Opencart Image Library Of Version 2.0.x.x and 2.1.x.x

Updated 17 March 2016

As we love to upload images in both Opencart admin end and front end then we should know about how to use opencart default image library.Opencart has been changed some codes in image library file of version 2.1.x.x and today we are going to learn about the object constructor differences between Opencart version 2.0.x.x and version 2.1.x.x image library file.

Version 2.0.x.x –

when we make an object of image class then this constructor is called

$image= new Image(DIR_IMAGE.'/home/demo/public_html/image/demo_product.png');
public function __construct($file) {
		if (file_exists($file)) {
			$this->file = $file;

			$info = getimagesize($file);

			$this->info = array(
				'width'  => $info[0],
				'height' => $info[1],
				'bits'   => isset($info['bits']) ? $info['bits'] : '',
				'mime'   => isset($info['mime']) ? $info['mime'] : ''
			);

			$this->image = $this->create($file);
		} else {
			exit('Error: Could not load image ' . $file . '!');
		}
	}

private function create($image) {
        $mime = $this->info['mime'];

        if ($mime == 'image/gif') {
            return imagecreatefromgif ($image);
        } elseif ($mime == 'image/png') {
            return imagecreatefrompng($image);
        } elseif ($mime == 'image/jpeg') {
            return imagecreatefromjpeg($image);
        }
    }

Version 2.1.x.x –

public function __construct($file) {
		if (file_exists($file)) {
			$this->file = $file;

			$info = getimagesize($file);

			$this->width  = $info[0];
			$this->height = $info[1];
			$this->bits = isset($info['bits']) ? $info['bits'] : '';
			$this->mime = isset($info['mime']) ? $info['mime'] : '';

			if ($this->mime == 'image/gif') {
				$this->image = imagecreatefromgif($file);
			} elseif ($this->mime == 'image/png') {
				$this->image = imagecreatefrompng($file);
			} elseif ($this->mime == 'image/jpeg') {
				$this->image = imagecreatefromjpeg($file);
			}
		} else {
			exit('Error: Could not load image ' . $file . '!');
		}
	}

Main difference is that when we pass the image path as parameter of the constructor then

Start your headless eCommerce
now.
Find out More

width,height,bits and mime values are stored as indexes of class global variable in Version 2.0.x.x but in Version 2.1.x.x these values are used the class global variable to store the value.and we can get these values by calling these functions

        public function getFile() {
		return $this->file;
	}

	public function getImage() {
		return $this->image;
	}

	public function getWidth() {
		return $this->width;
	}

	public function getHeight() {
		return $this->height;
	}

	public function getBits() {
		return $this->bits;
	}

	public function getMime() {
		return $this->mime;
	}

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home