I have the following function for resizing to (also) thumbnails, and widht and height for big, smaller and thumbs is fixed sizes, but the problem is this:
If an uploaded image is small in dimensions there is a large white space around the thumb to make it a fixed size; If an uploaded image is too big in dimensions it returns an almost good thumb with little white space.
What I can see is that there is some kind of overlay to make up for the required fixed size which give me an ugly look if uploads are different sizes.
Is there any way to make images and at least the thumb a exact width x height, no matter what the upload size/perspective was?
public function resizeTo($width, $height) {
if(osc_use_imagick()) {
$bg = new Imagick();
$bg->newImage($width, $height, 'white');
$this->im->thumbnailImage($width, $height, true);
$geometry = $this->im->getImageGeometry();
$x = ( $width - $geometry['width'] ) / 2;
$y = ( $height - $geometry['height'] ) / 2;
$bg->compositeImage( $this->im, imagick::COMPOSITE_OVER, $x, $y );
$this->im = $bg;
} else {
$w = imagesx($this->im);
$h = imagesy($this->im);
if(($w/$h)>=($width/$height)) {
//$newW = $width;
$newW = ($w > $width)? $width : $w;
$newH = $h * ($newW / $w);
} else {
//$newH = $height;
$newH = ($h > $height)? $height : $h;
$newW = $w * ($newH / $h);
}
$newIm = imagecreatetruecolor($width,$height);//$newW, $newH);
imagealphablending($newIm, false);
$colorTransparent = imagecolorallocatealpha($newIm, 255, 255, 255, 127);
imagefill($newIm, 0, 0, $colorTransparent);
imagesavealpha($newIm, true);
imagecopyresampled($newIm, $this->im, (($width-$newW)/2), (($height-$newH)/2), 0, 0, $newW, $newH, $w, $h);
imagedestroy($this->im);
$this->im = $newIm;
}
return $this;
}