Advertisement:

Author Topic: Image Resize with Smart Offset and No White Space.  (Read 11173 times)

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Image Resize with Smart Offset and No White Space.
« on: June 12, 2013, 01:36:56 pm »
It will remove whitespace and crop 1/2 on width or 1/4 top & 3/4 bottom on height.

In ImageResizer.php use this calculation.

Code: [Select]
                $w = imagesx($this->im);
                $h = imagesy($this->im);
                $newW = ($w > $width)?  $width  : $w;
                $newH = ($h > $height)? $height : $h;
               
                if(($w/$h)<($width/$height)) {
                  //$newW = $width;
                  $cropedW = $w;
                  $cropedH = ($w > $width)? ($w * $height / $width) : $newH;
                  $offsetx = 0;
                  $offsety = ($h - $cropedH) / 4; 
                } else {
                  //$newH = $height;
                  $cropedW = ($h > $height)? ($h * $width / $height) : $newW;
                  $cropedH = $h;
                  $offsetx = ($w - $cropedW) /2;
                  $offsety = 0;
                }

                $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), $offsetx, $offsety, $newW, $newH, $cropedW, $cropedH);
                imagedestroy($this->im);

                $this->im = $newIm;

In ItemActions.php

Change 
Code: [Select]
     ImageResizer::fromFile($normal_path)->resizeTo($size[0], $size[1])->saveToFile($path);
TO
Code: [Select]
     ImageResizer::fromFile($tmpName)->resizeTo($size[0], $size[1])->saveToFile($path);
twice.

And you can adjust your image/thumbnail size in oc-admin page.
« Last Edit: June 12, 2013, 01:47:43 pm by byteGator »

thedon

  • Sr. Member
  • ****
  • Posts: 341
Re: Image Resize with Smart Offset and No White Space.
« Reply #1 on: June 20, 2013, 10:28:54 pm »
is this the replacement or what we have to replace??

Code: [Select]
$w = imagesx($this->im);
                $h = imagesy($this->im);
                $newW = ($w > $width)?  $width  : $w;
                $newH = ($h > $height)? $height : $h;
               
                if(($w/$h)<($width/$height)) {
                  //$newW = $width;
                  $cropedW = $w;
                  $cropedH = ($w > $width)? ($w * $height / $width) : $newH;
                  $offsetx = 0;
                  $offsety = ($h - $cropedH) / 4; 
                } else {
                  //$newH = $height;
                  $cropedW = ($h > $height)? ($h * $width / $height) : $newW;
                  $cropedH = $h;
                  $offsetx = ($w - $cropedW) /2;
                  $offsety = 0;
                }

                $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), $offsetx, $offsety, $newW, $newH, $cropedW, $cropedH);
                imagedestroy($this->im);

                $this->im = $newIm;

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #2 on: June 21, 2013, 04:02:53 am »
Look at resizeTo function, then you will know which code to replace.

thedon

  • Sr. Member
  • ****
  • Posts: 341
Re: Image Resize with Smart Offset and No White Space.
« Reply #3 on: June 21, 2013, 05:27:38 pm »
i still don't get it

skr678

  • Jr. Member
  • **
  • Posts: 52
Re: Image Resize with Smart Offset and No White Space.
« Reply #4 on: June 21, 2013, 07:21:01 pm »
i still don't get it

On my imageresizer.php it was lines 70 to 91.

Just a question. This fixed the thumbnail for ad listings which is big improvement! Thank you!

The other two image sizes still have the white border around them.  Is this supposed to do anything with those images?

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #5 on: June 22, 2013, 05:16:37 am »
Hi skr678

This is full resizeTo code.

$fill is to remove whitespace.
$upscale is to allow to resize if image source is smaller.

But I don't think you want to use this whole code. Just cut the code that you don't want to use.

Code: [Select]
public function resizeTo($width, $height, $upscale = true, $fill = true) {
        if (osc_use_imagick()) {
            if ($fill) {
                $geometry = $this->im->getImageGeometry();
                $w = $geometry['width'];
                $h = $geometry['height'];
                if($upscale or (($w > $width) and ($h > $height))) {
                    if (($w / $width) < ($h / $height)) {
                        $this->im->cropImage($w, floor($height * $w / $width), 0, (($h - ($height * $w / $width)) / 4));
                    } else {
                        $this->im->cropImage(ceil($width * $h / $height), $h, (($w - ($width * $h / $height)) / 2), 0);
                    }
                    $this->im->ThumbnailImage($width, $height, true);
                } else {
                    $newW = ($w > $width) ? $width : $w;
                    $newH = ($h > $height) ? $height : $h;
                    if (($w / $width) < ($h / $height)) {
                        $cropedW = $w;
                        $cropedH = ($w > $width) ? ceil($w * $height / $width) : $newH;
                    } else {
                        $cropedH = $h;
                        $cropedW = ($h > $height) ? ceil($h * $width / $height) : $newW;
                    }
                    $offsetx = ($w - $cropedW) / 2;
                    $offsety = ($h - $cropedH) / 4;
                    $this->im->cropImage($cropedW, $cropedH, $offsetx, $offsety);
                    $bg = new Imagick();
                    $bg->newImage($width, $height, 'white');
                    $x = ceil($width  - $newW) / 2;
                    $y = ceil($height - $newH) / 2;
                    $bg->compositeImage($this->im, imagick::COMPOSITE_OVER, $x, $y);
                    $this->im = $bg;
                }
            } else {
                $bg = new Imagick();
                $bg->newImage($width, $height, 'white');
                $geometry = $this->im->getImageGeometry();
                if ($upscale or ($geometry['width'] > $width) or ($geometry['height'] > $height)) {
                    $this->im->thumbnailImage($width, $height, true);
                    $geometry = $this->im->getImageGeometry();
                }
                $x = ceil($width - $geometry['width']) / 2;
                $y = ceil($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 ($fill) {
                if($upscale) {
                    $newW = $width;
                    $newH = $height;
                } else {   
                    $newW = ($w > $width) ? $width : $w;
                    $newH = ($h > $height) ? $height : $h;
                }   
                if (($w / $h) < ($width / $height)) {
                    $cropedW = $w;
                    if($upscale){
                        $cropedH = ceil($w * $height / $width);
                    } else {
                        $cropedH = ($w > $width) ? ceil($w * $height / $width) : $newH;
                    }
                    $offsetx = 0;
                    $offsety = ($h - $cropedH) / 4;
                } else {
                    $cropedH = $h;
                    if($upscale){
                        $cropedW = ceil($h * $width / $height);
                    } else {
                        $cropedW = ($h > $height) ? ceil($h * $width / $height) : $newW;
                    }
                    $offsetx = ($w - $cropedW) / 2;
                    $offsety = 0;
                }
            } else {
                if(($w/$h)>=($width/$height)) {
                    if($upscale){ $newW = $width; } else { $newW = ($w > $width)? $width : $w; };
                    $newH = ceil($h * ($newW / $w));
                } else {
                    if($upscale) { $newH = $height; } else { $newH = ($h > $height)? $height : $h; };
                    $newW = ceil($w * ($newH / $h));
                }
                $cropedW = $w;
                $cropedH = $h;
                $offsetx = 0;
                $offsety = 0;
            }   
            $newIm = imagecreatetruecolor($width, $height);
            imagealphablending($newIm, false);
            $colorTransparent = imagecolorallocatealpha($newIm, 255, 255, 255, 127);
            imagefill($newIm, 0, 0, $colorTransparent);
            imagesavealpha($newIm, true);
            imagecopyresampled($newIm, $this->im, floor(($width - $newW) / 2), floor(($height - $newH) / 2), $offsetx, $offsety, $newW, $newH, $cropedW, $cropedH);
            imagedestroy($this->im);
            $this->im = $newIm;
        }
        return $this;
    }
« Last Edit: June 22, 2013, 05:22:59 am by byteGator »

thedon

  • Sr. Member
  • ****
  • Posts: 341
Re: Image Resize with Smart Offset and No White Space.
« Reply #6 on: June 22, 2013, 02:24:28 pm »
ok but where is imageresizer.php i cannot find it

skr678

  • Jr. Member
  • **
  • Posts: 52
Re: Image Resize with Smart Offset and No White Space.
« Reply #7 on: June 23, 2013, 06:30:21 am »
ok but where is imageresizer.php i cannot find it

\oc-includes\osclass\classes

If you have osclass on your computer, you can just browse to it and do a search of osclass directory. Helps to find files quickly for me.

skr678

  • Jr. Member
  • **
  • Posts: 52
Re: Image Resize with Smart Offset and No White Space.
« Reply #8 on: June 23, 2013, 06:32:23 am »
To clarify, the part that is in the OP is just to correct the thumbnail white space? 

And the other parts of below allows resizing of other image sizes?

Thank you again. Really like the way the thumbnail looks with this mod.



Hi skr678

This is full resizeTo code.

$fill is to remove whitespace.
$upscale is to allow to resize if image source is smaller.

But I don't think you want to use this whole code. Just cut the code that you don't want to use.

Code: [Select]
public function resizeTo($width, $height, $upscale = true, $fill = true) {
        if (osc_use_imagick()) {
            if ($fill) {
                $geometry = $this->im->getImageGeometry();
                $w = $geometry['width'];
                $h = $geometry['height'];
                if($upscale or (($w > $width) and ($h > $height))) {
                    if (($w / $width) < ($h / $height)) {
                        $this->im->cropImage($w, floor($height * $w / $width), 0, (($h - ($height * $w / $width)) / 4));
                    } else {
                        $this->im->cropImage(ceil($width * $h / $height), $h, (($w - ($width * $h / $height)) / 2), 0);
                    }
                    $this->im->ThumbnailImage($width, $height, true);
                } else {
                    $newW = ($w > $width) ? $width : $w;
                    $newH = ($h > $height) ? $height : $h;
                    if (($w / $width) < ($h / $height)) {
                        $cropedW = $w;
                        $cropedH = ($w > $width) ? ceil($w * $height / $width) : $newH;
                    } else {
                        $cropedH = $h;
                        $cropedW = ($h > $height) ? ceil($h * $width / $height) : $newW;
                    }
                    $offsetx = ($w - $cropedW) / 2;
                    $offsety = ($h - $cropedH) / 4;
                    $this->im->cropImage($cropedW, $cropedH, $offsetx, $offsety);
                    $bg = new Imagick();
                    $bg->newImage($width, $height, 'white');
                    $x = ceil($width  - $newW) / 2;
                    $y = ceil($height - $newH) / 2;
                    $bg->compositeImage($this->im, imagick::COMPOSITE_OVER, $x, $y);
                    $this->im = $bg;
                }
            } else {
                $bg = new Imagick();
                $bg->newImage($width, $height, 'white');
                $geometry = $this->im->getImageGeometry();
                if ($upscale or ($geometry['width'] > $width) or ($geometry['height'] > $height)) {
                    $this->im->thumbnailImage($width, $height, true);
                    $geometry = $this->im->getImageGeometry();
                }
                $x = ceil($width - $geometry['width']) / 2;
                $y = ceil($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 ($fill) {
                if($upscale) {
                    $newW = $width;
                    $newH = $height;
                } else {   
                    $newW = ($w > $width) ? $width : $w;
                    $newH = ($h > $height) ? $height : $h;
                }   
                if (($w / $h) < ($width / $height)) {
                    $cropedW = $w;
                    if($upscale){
                        $cropedH = ceil($w * $height / $width);
                    } else {
                        $cropedH = ($w > $width) ? ceil($w * $height / $width) : $newH;
                    }
                    $offsetx = 0;
                    $offsety = ($h - $cropedH) / 4;
                } else {
                    $cropedH = $h;
                    if($upscale){
                        $cropedW = ceil($h * $width / $height);
                    } else {
                        $cropedW = ($h > $height) ? ceil($h * $width / $height) : $newW;
                    }
                    $offsetx = ($w - $cropedW) / 2;
                    $offsety = 0;
                }
            } else {
                if(($w/$h)>=($width/$height)) {
                    if($upscale){ $newW = $width; } else { $newW = ($w > $width)? $width : $w; };
                    $newH = ceil($h * ($newW / $w));
                } else {
                    if($upscale) { $newH = $height; } else { $newH = ($h > $height)? $height : $h; };
                    $newW = ceil($w * ($newH / $h));
                }
                $cropedW = $w;
                $cropedH = $h;
                $offsetx = 0;
                $offsety = 0;
            }   
            $newIm = imagecreatetruecolor($width, $height);
            imagealphablending($newIm, false);
            $colorTransparent = imagecolorallocatealpha($newIm, 255, 255, 255, 127);
            imagefill($newIm, 0, 0, $colorTransparent);
            imagesavealpha($newIm, true);
            imagecopyresampled($newIm, $this->im, floor(($width - $newW) / 2), floor(($height - $newH) / 2), $offsetx, $offsety, $newW, $newH, $cropedW, $cropedH);
            imagedestroy($this->im);
            $this->im = $newIm;
        }
        return $this;
    }

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Image Resize with Smart Offset and No White Space.
« Reply #9 on: June 23, 2013, 02:26:44 pm »
Thanks for the code. Works fine in firefox,and other browsers, but in IE does not work. Can anyone fix it?
PS: I hate IE, it's never compactivel in anything :o

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #10 on: June 26, 2013, 05:47:09 am »
Thanks for the code. Works fine in firefox,and other browsers, but in IE does not work. Can anyone fix it?
PS: I hate IE, it's never compactivel in anything :o

it is about your theme css.

fog

  • Hero Member
  • *****
  • Posts: 1062
Re: Image Resize with Smart Offset and No White Space.
« Reply #11 on: June 26, 2013, 06:49:41 am »
Hello byteGator, I forgot to edit my post. It was the internet history, after cleaned temporary files, internet explorer already recognized the dimensions. It works very well. Thanks for sharing this wonderful feature!
greetings

Maximus

  • Newbie
  • *
  • Posts: 41
Re: Image Resize with Smart Offset and No White Space.
« Reply #12 on: January 28, 2014, 11:12:12 pm »
Please, tell me how to apply this solution only for thumbs - not for previews, and not for normal photos.

dev101

  • Osclass Hero
  • Hero Member
  • *
  • Posts: 2155
  • osclass.work
Re: Image Resize with Smart Offset and No White Space.
« Reply #13 on: January 29, 2014, 03:10:12 am »
Hi byteGator,

I was also using this mod for a long time (set it and forget to revert back, even forgot where read it and who posted it, too lazy I guess :D). Thing is it does crop full-size images, which is kind of bad. Explanation: for example, if you use 4:3 ratio, then 16:10, 3:2, 2:1 or 16:9 images and other non-ideal ones will loose considerable amount of information. It is a good thing for thumbs and preview, but not so good for full-size images. For those who do not wish to crop images and loose image information, forced aspect ratio with added white or black color is still the best way.

Regards
dev101
« Last Edit: January 29, 2014, 03:17:21 am by dev101 »

Maximus

  • Newbie
  • *
  • Posts: 41
Re: Image Resize with Smart Offset and No White Space.
« Reply #14 on: January 29, 2014, 03:58:26 am »
It is a good thing for thumbs and preview, but not so good for full-size images.
Yes, exactly!
So I beg you, if anyone knows, please tell how to apply this solution only for thumbs.