You are here:  » Optimizing cached images


Optimizing cached images

Submitted by Retro135 on Sun, 2017-02-05 08:17 in

Hi David,
I have about 750k products. As many merchants supply only huge images, even cached, many will be over the 25k size you mentioned in another thread to estimate disk storage size. I began to do this with products and stopped as I saw images 100k and over. Is there any way to optimize these images on the server and replace in the cache?

Submitted by support on Mon, 2017-02-06 12:09

Hi,

I know you have been using imageCache.php for some time so may not be at the latest version which includes re-sizing so that might do the trick. Note that the latest version uses Base 64 encoding to transfer the image URL - if you are currently using urlencode(), to avoid having to modify your html/ files, where the latest version has this code:

  $src = base64_decode($_GET["src"]);

...REPLACE with:

  $src = $_GET["src"];

Cheers,
David.
--
PriceTapestry.com

Submitted by Retro135 on Mon, 2017-02-06 15:38

Actually, this is the version I've been using, but only with featured. I manually optimized to bring down size, so now average 30-32k.

I experimented with products, but deleted after seeing the resized image sizes, which many were over 100k. Certainly can't manually optimize.

I'm probably misunderstanding, but if I use with both search results & product, does that create 2 cached images of same product? If so, is there some way for product images to utilize cached search result images? If this is database intensive, don't even bother w/code :-)

Submitted by support on Mon, 2017-02-06 15:45

Hi,

If you use imageCache.php for both Featured Products and for the main product image, there would only be 1 cached image file created as the URL and therefore the hash used to create the cache filename will be the same.

I'm surprised at the images sizes after resize being so large - what resize dimension do you have configured in imageCache.php e.g.

  $imageCacheResize = 250;

...and could you perhaps let me know an example product image URL that is coming in at over 100K after resizing? (i'll remove link before publishing your reply...)

Thanks,
David.
--
PriceTapestry.com

Submitted by Retro135 on Tue, 2017-02-07 05:58

I regenerated unoptimized images on Featured page. What I see from comparing merchant / cache images is that smaller size jpgs become larger pngs. And larger jpgs don't reduce very well (a 242kb jpg > 125.6kb png). Actually, you can pull any image from feature and product pages to compare sizes as product page images aren't cached (I disabled for product pages). Here is sample info/links:

{code saved}

Submitted by support on Tue, 2017-02-07 09:14

Hi,

I'll check the sources out later but I just wanted to suggest one thing to try quickly - in the imageCache.php script the re-sized image is output as png using this code;

      imagepng($new);

....it might be worth experimenting with JPEG format instead, simply REPLACE with:

      imagejpeg($new);

Cheers,
David.
--
PriceTapestry.com

Submitted by Retro135 on Wed, 2017-02-08 07:37

When I added new items, it worked. File sizes now tiny (most under 11k)!

However, some of the image quality is not good (last 3 rows are new jpegs): {link saved}

1. Next to last row, click image to see product page to compare. The default quality is 75 in GD, found this to increase quality:

      imagejpeg($im, NULL, 90);
      imagedestroy($im);

How can this be included in imageCache.php script?

2. The very last image has black showing top/bottom (but not on product page). How can I make background all white?

Submitted by support on Wed, 2017-02-08 09:56

Hi,

To include the quality parameter, simply use that code directly in imageCache.php at line 79:

      imagejpeg($im, NULL, 90);

Regarding the image with black borders, I'm not quite sure where this is coming from as it doesn't appear to be part of the image (and as you say it is fine on the product page!) However, if it is an artifact of scaling, the first thing I would suggest is to try a slightly different dimension, for example if you are using;

  $imageCacheResize = 250;

...try:

  $imageCacheResize = 256;

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Retro135 on Thu, 2017-02-09 20:57

image quality code never worked, returned "default quality" no matter what setting I used, so deleted it. Changed Resize to Resample, that works _super_. Trying to add a little sharpening, no joy.

I've tried several different things, not getting results. What I'm working with now:

{code saved}

1. Where should the sharpening code be placed?

2. Does it need to be altered to work with imageCache.php?

3. The example code does have _return $img;_ as the last line, but as I saw that further down in your code, left it out. I placed closing brace where it is now and also tried adding after your code. Neither made any difference. Where should the function imagesharpen closing brace be placed?

4. Should I use $img or $new?

+ any & all advice (will sharpening will cause too much load/use too much memory, etc.) is greatly appreciated.

Submitted by support on Fri, 2017-02-10 09:13

Hi,

Position your imagesharpen function at the top of the script, and you will need to add the return value at the end of the function (immediately following call to imageconvolution)...

  return $new;

And then insert the call just before the call to imagejpeg(), so you will have:

  $new = imagesharpen($new);
  imagejpeg($new);

Cheers,
David.
--
PriceTapestry.com

Submitted by Retro135 on Sun, 2017-02-12 19:17

Hi David,
Worked a charm after I revised

$new = imagesharpen($new);
imagejpeg($new,null,90);

Quality now works, and sharpening didn't until I set this. New images avg. low 20s, all good. TY very much!

Submitted by Retro135 on Sun, 2017-02-12 19:28

Regarding black border...it's actually a black background. I happened to find another merchant using a very small image inside a 300x300 png with a large transparency. Example at bottom: {link saved}

I checked imageCache, background is set to white, haven't touched.

  $new = imagecreatetruecolor($imageCacheResample,$imageCacheResample);
  $newBackground = imagecolorallocate($new,255,255,255);
  imagefill($new,1,1,$newBackground);

Does anything need to be changed if merchant supplies png and I'm converting to jpeg?

Submitted by support on Mon, 2017-02-13 10:10

Hi,

Please could you email me your latest imageCache.php and I'll give it a go on my test server - there are various threads around the net regarding transparency resulting in a black background after resizing...

Cheers,
David.
--
PriceTapestry.com