Skip to content

Commit

Permalink
https://github.com/opencart/opencart/issues/3119
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkerr committed Aug 8, 2015
1 parent 5e04236 commit 8afa9b6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 55 deletions.
2 changes: 1 addition & 1 deletion upload/catalog/model/tool/image.php
Expand Up @@ -40,4 +40,4 @@ public function resize($filename, $width, $height) {
return $this->config->get('config_url') . 'image/' . $new_image;
}
}
}
}
121 changes: 67 additions & 54 deletions upload/system/library/image.php
Expand Up @@ -2,37 +2,56 @@
class Image {
private $file;
private $image;
private $info;
private $width;
private $height;
private $bits;
private $mime;

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);
$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 . '!');
}
}

private function create($image) {
$mime = $this->info['mime'];
public function getFile() {
return $this->file;
}

if ($mime == 'image/gif') {
return imagecreatefromgif ($image);
} elseif ($mime == 'image/png') {
return imagecreatefrompng($image);
} elseif ($mime == 'image/jpeg') {
return imagecreatefromjpeg($image);
}
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;
}

public function save($file, $quality = 90) {
Expand All @@ -46,24 +65,24 @@ public function save($file, $quality = 90) {
} elseif ($extension == 'png') {
imagepng($this->image, $file);
} elseif ($extension == 'gif') {
imagegif ($this->image, $file);
imagegif($this->image, $file);
}

imagedestroy($this->image);
}
}

public function resize($width = 0, $height = 0, $default = '') {
if (!$this->info['width'] || !$this->info['height']) {
if (!$this->width || !$this->height) {
return;
}

$xpos = 0;
$ypos = 0;
$scale = 1;

$scale_w = $width / $this->info['width'];
$scale_h = $height / $this->info['height'];
$scale_w = $width / $this->width;
$scale_h = $height / $this->height;

if ($default == 'w') {
$scale = $scale_w;
Expand All @@ -73,19 +92,19 @@ public function resize($width = 0, $height = 0, $default = '') {
$scale = min($scale_w, $scale_h);
}

if ($scale == 1 && $scale_h == $scale_w && $this->info['mime'] != 'image/png') {
if ($scale == 1 && $scale_h == $scale_w && $this->mime != 'image/png') {
return;
}

$new_width = (int)($this->info['width'] * $scale);
$new_height = (int)($this->info['height'] * $scale);
$new_width = (int)($this->width * $scale);
$new_height = (int)($this->height * $scale);
$xpos = (int)(($width - $new_width) / 2);
$ypos = (int)(($height - $new_height) / 2);

$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);

if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') {
if ($this->mime == 'image/png') {
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
Expand All @@ -96,61 +115,56 @@ public function resize($width = 0, $height = 0, $default = '') {

imagefilledrectangle($this->image, 0, 0, $width, $height, $background);

imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height);
imagedestroy($image_old);

$this->info['width'] = $width;
$this->info['height'] = $height;
$this->width = $width;
$this->height = $height;
}

public function watermark($file, $position = 'bottomright') {
$watermark = $this->create($file);

$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);

public function watermark($watermark, $position = 'bottomright') {
switch($position) {
case 'topleft':
$watermark_pos_x = 0;
$watermark_pos_y = 0;
break;
case 'topright':
$watermark_pos_x = $this->info['width'] - $watermark_width;
$watermark_pos_x = $this->width - $watermark->getWidth();
$watermark_pos_y = 0;
break;
case 'bottomleft':
$watermark_pos_x = 0;
$watermark_pos_y = $this->info['height'] - $watermark_height;
$watermark_pos_y = $this->height - $watermark->getHeight();
break;
case 'bottomright':
$watermark_pos_x = $this->info['width'] - $watermark_width;
$watermark_pos_y = $this->info['height'] - $watermark_height;
$watermark_pos_x = $this->width - $watermark->getWidth();
$watermark_pos_y = $this->height - $watermark->getHeight();
break;
}

imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, 120, 40);
imagecopy($this->image, $watermark->getImage(), $watermark_pos_x, $watermark_pos_y, 0, 0, $watermark->getWidth(), $watermark->getHeight());

imagedestroy($watermark);
imagedestroy($watermark->getImage());
}

public function crop($top_x, $top_y, $bottom_x, $bottom_y) {
$image_old = $this->image;
$this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);

imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->info['width'], $this->info['height']);
imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->width, $this->height);
imagedestroy($image_old);

$this->info['width'] = $bottom_x - $top_x;
$this->info['height'] = $bottom_y - $top_y;
$this->width = $bottom_x - $top_x;
$this->height = $bottom_y - $top_y;
}

public function rotate($degree, $color = 'FFFFFF') {
$rgb = $this->html2rgb($color);

$this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));

$this->info['width'] = imagesx($this->image);
$this->info['height'] = imagesy($this->image);
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
}

private function filter($filter) {
Expand All @@ -163,13 +177,8 @@ private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000') {
imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
}

private function merge($file, $x = 0, $y = 0, $opacity = 100) {
$merge = $this->create($file);

$merge_width = imagesx($merge);
$merge_height = imagesy($merge);

imagecopymerge($this->image, $merge, $x, $y, 0, 0, $merge_width, $merge_height, $opacity);
private function merge($merge, $x = 0, $y = 0, $opacity = 100) {
imagecopymerge($this->image, $merge->getImage(), $x, $y, 0, 0, $merge->getWidth(), $merge->getHeight(), $opacity);
}

private function html2rgb($color) {
Expand All @@ -191,4 +200,8 @@ private function html2rgb($color) {

return array($r, $g, $b);
}
}

function __destruct() {

}
}

1 comment on commit 8afa9b6

@danijelGombac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In watermark function is something wrong.

Fatal error: Call to a member function getImage() on a non-object on line 145

Please sign in to comment.