Page 1 of 1

Cached Images and Size

Posted: Wed Aug 19, 2009 3:21 am
by hcamelion
I am wondering how cached images work. I set my popup product images to 900x900 then I noticed that it adds white space to the image if it is not 900x900 rather then just resize by width only. For example if a photo is 1000x600 open cart will make it 900x900 if you set that in the admin instead of something like 900x550 if it were to resize by width only while keeping the aspect ratio. Maybe the settings page should have a "resize by" setting. Resize by width, height, both or best fit.

Now I am trying to change the size on the settings page but I guess after the images have been cached you can no longer change them. Is there any way to delete the cache and have it rebuilt other then re-uploading all the photos in the store.

Re: Cached Images and Size

Posted: Wed Aug 19, 2009 3:59 am
by Daniel
what happens is the system checks which file is newer. the cached version or the main image. if the main image is newer it makes a new cache and overwrites the old one.

Re: Cached Images and Size

Posted: Wed Aug 19, 2009 10:06 pm
by Qphoria
hcamelion wrote:I am wondering how cached images work. I set my popup product images to 900x900 then I noticed that it adds white space to the image if it is not 900x900 rather then just resize by width only. For example if a photo is 1000x600 open cart will make it 900x900 if you set that in the admin instead of something like 900x550 if it were to resize by width only while keeping the aspect ratio. Maybe the settings page should have a "resize by" setting. Resize by width, height, both or best fit.
It should already be keeping the best aspect ratio. It you upload a 300x800 image and then a 800x300 image
It will pad whitespace on the sides of the first one to make it square
It will pad whitespace on the top and bottom of the second one to make it square.
The idea here is to make all images look the same on the store front so they aren't all different sizes making your page all janky.
Its just using the normal php resizing features used in "imagecreatefromjpg" built in function. So it's a standard function.
Now I am trying to change the size on the settings page but I guess after the images have been cached you can no longer change them. Is there any way to delete the cache and have it rebuilt other then re-uploading all the photos in the store.
As Daniel said, if the image is newer it will regenerate. But if you want to regenerate them without uploading new files, simply goto your ftp and delete all the image files from the image/cache folder

Re: Cached Images and Size

Posted: Thu Aug 20, 2009 3:16 am
by kdmp
Just to add to this...

If I delete what is in my cache - it never comes back.

Does OC generate those cache files when they are requested? I just tested your instructions out in 1.3.2 and when I deleted everything (mind you this is a fresh install of OC 1.3.2 with the image folder restored from previous version). I called the 'frontpage' and no product images appear. I log in to my server through SSH and there are no files present in the cache directory. The cache directory has the proper permissions.

Re: Cached Images and Size

Posted: Thu Aug 20, 2009 3:48 am
by Qphoria
They are generated the first time they are loaded, yes.

Re: Cached Images and Size

Posted: Thu Aug 20, 2009 6:46 am
by kdmp
Qphoria,

Thanks, my case it was my fault. I didn't set my log file. After I set a name for my log file it started to work. I didn't realize that it was required at the time for testing.

Thanks!

Re: Cached Images and Size

Posted: Thu Aug 20, 2009 7:00 am
by hcamelion
Yes, I did realize that the cache rebuilds itself and it only was not working because I tried a sized of "900xnothing". I took a look at the image_resize function in the image helper. I understand why uniform sizes on thumbnails is important but on the larger pictures it ends up making them quite smaller if they are not exactly that size. For example I have an artist who wants to show their artwork which has writing on it. All the artwork are canvas prints roughly 900x600. Some other products are not the same size.

When I upload a 900x600 photo the photo will be padded with white space to make it 900x900. Then when the user clicks on the photo thickbox makes it quite smaller because it is so tall when really it should have been only 600px tall or smaller. This makes the artwork unreadable because when padded the artwork is 400x400 in thick box. I did change the size to 900x600 on the settings page and on my resolution monitor the photos are now a nice 800px wide. If there were a resize by drop down on the image settings users can choose the behavior of the resizing at least on the large photos.

Re: Cached Images and Size

Posted: Tue Jan 19, 2010 9:01 pm
by Bryan76
I"m not sure if this is what you're looking for or not, but I rewrote the resize function of the Image class. It will now create an image with the aspect ratio of the original with no added "letterboxing."

As others noted however, this may screw up the alignment of your catalog. You'll need to fix this in your theme. I did it because I wanted to put a drop shadow on my images.

system/library/image.php

Code: Select all

    public function resize($width = 0, $height = 0) {
    	if (!$this->info['width'] || !$this->info['height']) {
			return;
		}

		$xpos = 0;
		$ypos = 0;

		$scale = min($width / $this->info['width'], $height / $this->info['height']);
		
		if ($scale == 1) {
			return;
		}
		
		$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($new_width, $new_height);
		
		//$background = imagecolorallocate($this->image, 255, 255, 255);
		//imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
	
		imagecopyresized($this->image, $image_old, 0, 0, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);
		imagedestroy($image_old);

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

Re: Cached Images and Size

Posted: Sun Oct 31, 2010 10:03 pm
by dantheman50_98
Hi,

I've used this code in system/library/image.php and it has worked, so I now have no added white space which is great. However, the images appear rather jagged now. Have you got any idea why this could be?
I'm using v.1.4.91 by the way. I'm also using the watermark mod, so I had to modify the image.php file in that if itmakes any difference.

Thanks,
Dan

Re: Cached Images and Size

Posted: Sun Apr 10, 2011 4:25 am
by dragonxeon
To fix the jagged issue change this line

imagecopyresized($this->image, $image_old, 0, 0, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);

with this line

imagecopyresampled($this->image, $image_old, 0, 0, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']);


Basically you want it to resample the image not use resized

Re: Cached Images and Size

Posted: Sun Apr 10, 2011 8:05 pm
by dantheman50_98
Oh great, thanks. I'll bear that in mind if I use opencart again.

Re: Cached Images and Size

Posted: Thu Jan 26, 2012 7:08 pm
by merrydukaan
Hi I am on latest version of OC and got the same problem. dragonxeon's solution of replacing with 'imagecopyresampled' is already part of default code, tried deleting image cache too! Any ideas?
Someone?

Re: Cached Images and Size

Posted: Thu Feb 02, 2012 10:35 pm
by kathylene
I am using v1.5.1.3 I changed the code as Bryan76 and dragonxeon suggested and it worked perfectly. I did go through and deleted all the cached image thumbnails that had already been made (/image/cache/data).

I have been trying for quite sometime to get the white letterbox effect to not display since I have a black background. Thanks to the both of you!

Re: Cached Images and Size

Posted: Sun Feb 05, 2012 10:58 am
by ijmilton
Hello,

New to OpenCart and this forum.

First, thank you very much for making this. It works great!

Second, I converted it to vqmod and here it is :)

Re: Cached Images and Size

Posted: Tue Sep 11, 2012 2:33 am
by FCWC
Tx for this.
Using the VQMod - makes it simple!
Now 2 more things. If my image has a transparent background it is turning it black. Fix?

And 2nd - how to auto center images?