Advertisement:

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

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #15 on: February 01, 2014, 12:31:50 am »
Hi....

To use it for thumbnail and normal with different crop you can edit your ItemAction

this is my code, but maybe it already has to much modification:

Code: [Select]
        public function uploadItemResources($aResources,$itemId)
        {
            if($aResources != '') {
                $itemResourceManager = ItemResource::newInstance();
                $folder = osc_images_path().floor($itemId/1000000)."/".sprintf("%02d", floor(($itemId%1000000)/10000))."/".sprintf("%02d", floor(($itemId%10000)/100))."/";
                $numImagesItems = osc_max_images_per_item();
                $numImages = $itemResourceManager->countResources($itemId);
                foreach ($aResources['error'] as $key => $error) {
                    if($numImagesItems==0 || ($numImagesItems>0 && $numImages<$numImagesItems)) {
                        if ($error == UPLOAD_ERR_OK) {
                            $tmpName = $aResources['tmp_name'][$key];
                            $imgres = ImageResizer::fromFile($tmpName);
                            //$extension = osc_apply_filter('upload_image_extension', $imgres->getExt());
                            //$mime = osc_apply_filter('upload_image_mime', $imgres->getMime());
                            $extension ='jpg';
                            $mime = 'image/jpeg';

                            // Create normal size

                            $normal_path = $path = $tmpName."_normal";
                            $size = explode('x', osc_normal_dimensions());
                            $img = ImageResizer::fromFile($tmpName)->autoRotate()->resizeTo($size[0], $size[1], false);
                            if( osc_is_watermark_text() ) {
                                $img->doWatermarkText(osc_watermark_text(), osc_watermark_text_color());
                            } elseif ( osc_is_watermark_image() ){
                                $img->doWatermarkImage();
                            }
                            $img->saveToFile($path, $extension);

                            // Create preview

                            $path = $tmpName."_preview";
                            $size = explode('x', osc_preview_dimensions());
                            ImageResizer::fromFile($tmpName)->autoRotate()->resizeTo($size[0], $size[1], true)->saveToFile($path, $extension);

                            // Create thumbnail

                            $path = $tmpName."_thumbnail";
                            $size = explode('x', osc_thumbnail_dimensions());
                            ImageResizer::fromFile($tmpName)->autoRotate()->resizeTo($size[0], $size[1], true )->saveToFile($path, $extension);


Maximus

  • Newbie
  • *
  • Posts: 41
Re: Image Resize with Smart Offset and No White Space.
« Reply #16 on: February 01, 2014, 03:37:11 am »
Thank you very much, byteGator!

I just changed part of the osclass code

Code: [Select]
ImageResizer::fromFile($tmpName)->resizeTo($size[0], $size[1])->saveToFile($path);
on the part of your code, here it is:

Code: [Select]
ImageResizer::fromFile($tmpName)->resizeTo($size[0], $size[1], true )->saveToFile($path, $extension);
And now everything was excellent! Thumbs without white space, even with portrait images!

Many thanks!

ivv

  • Newbie
  • *
  • Posts: 49
Re: Image Resize with Smart Offset and No White Space.
« Reply #17 on: March 27, 2014, 12:07:13 pm »
I tryed but nothing happend only errors


I am trying to make those two mods

1. All thumbanils that are in Gallery and List mode - to be with fixed width/height, but one size for Gallery and other for List mode

2. All Small preview thumnails that are on a side of the main preview of item.php - to be with square size

 
Could some one tell what exactly I have to do.

10x

ivv

  • Newbie
  • *
  • Posts: 49
Re: Image Resize with Smart Offset and No White Space.
« Reply #18 on: March 28, 2014, 12:05:23 am »
Here is my test, how this mode crop the images:

1. When is uploaded a Landscape image is everything ok into grid thumbnail, and preview item.php


but the main problem is in Portrait images


2. When is uploaded Portrait image, here is what happend:

2.1. Into the grid thumbnail the image is not centeret by height, it's croped from top 80px, croped from bottom 220px, left and right are ok the are on 0px

Question for this 2.1.: How to center the image verticaly for Portrait images, so the thumbnail to show absolut center of image
see attached image named: 1st-portrait-2nd-landscape-grid-thumnbails.png


2.2. Into the preview mode (item.php) the Portrait image is the same croped just as it is in 2.1. (above)

Question for this 2.2.: Hoe to show all of the Portrait image without any cropes, just full portrait size
see attached image named: preview-width-small-thumbnails.png

3. I think that is better the watermark to be added after all images are resized to each one, not only the first one and than to be rsized and to losing quality of the watermark, and to not be visible as is into this case of mod



Please also see the image: portrait-image-croped-by-preview-frame-as-a-landscape.png
This image shows how 600x800px Portrait image is croped, and how many data is lost, should be nice to let the portrait image to be all visible without crops into preview item.php

 
« Last Edit: March 28, 2014, 12:22:06 am by ivv »

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #19 on: March 28, 2014, 11:11:13 am »
Hi ivv

2.1. To change offset, change this
Code: [Select]
       $offsetx = ($w - $cropedW) / 2;
       $offsety = ($h - $cropedH) / 4;
to
Code: [Select]
       $offsetx = ($w - $cropedW) / 2;
       $offsety = ($h - $cropedH) / 2;

2.2. To crop only thumbnail, change item action. See previous post.

3.1 Yes, watermark is added after crop and resize. It ALREADY work this way.

Regards
B

ivv

  • Newbie
  • *
  • Posts: 49
Re: Image Resize with Smart Offset and No White Space.
« Reply #20 on: March 28, 2014, 11:20:15 am »
I changed the code from 2.11

Changed ItemAction but still portrait images are acroped as a landscape into preview item.php -  hereis the image

I tryed both with on/off of

Force image aspect. No white background will be added to keep the size. - from admin

No luck,

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #21 on: March 28, 2014, 11:26:42 am »
All croped image will have standard height / width. So it cannot preserve your mode (portrait / landscape).
FOr me, I choose to use square thumbnail and preview.

ivv

  • Newbie
  • *
  • Posts: 49
Re: Image Resize with Smart Offset and No White Space.
« Reply #22 on: March 28, 2014, 11:27:23 am »
Also I saw potential problem:

You post this:
Quote
2.1. To change offset, change this
Code: [Select]
       $offsetx = ($w - $cropedW) / 2;
       $offsety = ($h - $cropedH) / 4;
to
Code: [Select]
       $offsetx = ($w - $cropedW) / 2;
       $offsety = ($h - $cropedH) / 2;

But there is no change after I made this msetup at all,

BUT

if I change the code few rows below:

from:
Quote
}
                            $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;
                        }

to:
Quote
}
                            $offsetx = 0;
                            $offsety = ($h - $cropedH) / 2;
                        } 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;
                        }

than both thumbnail and preview images are verticaly middle, is it a bug?

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #23 on: March 28, 2014, 11:31:03 am »
you do right things. It is not a bug.

I just want to tell you that for center you have to use offset /2
if you want to 25% offset at top and 75% offset at bottom, use offset /4.



ivv

  • Newbie
  • *
  • Posts: 49
Re: Image Resize with Smart Offset and No White Space.
« Reply #24 on: March 28, 2014, 11:34:05 am »
And what to use for 0% offset :)

I see what you mean, but the cript should offset only the thumbnails and leave the preview untouched :)


ivv

  • Newbie
  • *
  • Posts: 49
Re: Image Resize with Smart Offset and No White Space.
« Reply #25 on: March 28, 2014, 11:44:24 am »
Hi ivv

...

3.1 Yes, watermark is added after crop and resize. It ALREADY work this way.

Regards
B

If it's working like this why, there is no watermarks?

10x

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #26 on: March 28, 2014, 02:56:27 pm »
1. No watermark at thumbnail and preview.
2. As I said before, if you want to change how osclass work on preview, you have to change ItemAction.php

I think all is clear. Please try your self.

ivv

  • Newbie
  • *
  • Posts: 49
Re: Image Resize with Smart Offset and No White Space.
« Reply #27 on: March 28, 2014, 03:06:04 pm »
Yes I know that, but I'm not a programmer, so I am on my own alone.

Just wanted to tell that there is no sence the portrait image in preview item.php to be as alandscape mode.


10x for the help

Cheers

byteGator

  • Osclass Hero
  • Sr. Member
  • *
  • Posts: 346
  • Simple Mind is a Beautiful Mind
Re: Image Resize with Smart Offset and No White Space.
« Reply #28 on: March 28, 2014, 03:17:43 pm »
Yes I know that, but I'm not a programmer, so I am on my own alone.

Just wanted to tell that there is no sence the portrait image in preview item.php to be as alandscape mode.


10x for the help

Cheers

Yes, i suggest you make it square. I use 200 x 200 pixel for thumbnail. Take a look at my site. :)
BTW, what is your site address?

Regards

amarjeets18

  • Jr. Member
  • **
  • Posts: 92
Re: Image Resize with Smart Offset and No White Space.
« Reply #29 on: April 20, 2014, 06:57:48 am »