...

View Full Version : Help with image resize script



waps2
01-04-2012, 05:00 PM
Hi guys

Im trying to modify a part of a script in an open source shopping cart (opencart)
but not having much joy so need some help.

The functions below are what seem to recreate an image on the fly based on width and height setttings which are stored in the backend database.

My problem is this:

At the moment if the image loaded from image folder is not the same size as the preset values in the database, then it will recreate the image. It recreates the image to the specified size but will include a whitebackground with the picture sat in the middle.

This is fine normally. But I want to modify it to allow the following.

If a third parameter of $stretch =1 is passed into the function resize(), I want it to simply pull/push/reshape the image to the required size with no white space/background like it does now.

But Im stuck on that part ?

Hope I have explained it clearly enough :)

Awaiting your help on this one



final class Image {
private $file;
private $image;
private $info;

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

$info = getimagesize($file);

$this->info = array(
'width' => $info[0],
'height' => $info[1],
'bits' => $info['bits'],
'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);
}
}

public function save($file, $quality = 90) {
$info = pathinfo($file);

$extension = strtolower($info['extension']);

if ($extension == 'jpeg' || $extension == 'jpg') {
imagejpeg($this->image, $file, $quality);
} elseif($extension == 'png') {
imagepng($this->image, $file, 0);
} elseif($extension == 'gif') {
imagegif($this->image, $file);
}

imagedestroy($this->image);
}

public function resize($width = 0, $height = 0) {

/////looks into database and checks for a required width and height setting. if none it exits
if (!$this->info['width'] || !$this->info['height']) {
return;
}

$xpos = 0;
$ypos = 0;

///check to see if the image is the same as the required width/height setting in database
$scale = min($width / $this->info['width'], $height / $this->info['height']);

//if the image is the right size. nothing needs to be done so exit
if ($scale == 1) {
return;
}

///// however if the image is not the right size start to rescale and create a new one

$new_width = (int)($this->info['width'] * $scale);
$new_height = (int)($this->info['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') {
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
imagecolortransparent($this->image, $background);
} else {
$background = imagecolorallocate($this->image, 255, 255, 255);
}

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']);
imagedestroy($image_old);

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum