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);

            $this->im = $newIm;
        return $this;
4 Years
Discussion Span
Last Post by pritaeas

Is there any way to make images and at least the thumb a exact width x height

Basically yes. If the image is larger than the thumbnail, you could resize first and then crop to get an exact WxH size.


well, actually I want to stretch an image to the minimal dimensions. For example:
one uploads an image 600x400px, I need thumbnail 125x125px (either stretched or cropped)
one uploads an image 312x500px, I need thumbnail 125x125px (either stretched or cropped)

either without any white around the thumbnails.

Edited by mjsmitten


600x400: first resize to 187x125, then crop. So instead of calculating the aspect ratio and resizing the largest, resize the smallest of width and height.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.