PDA

View Full Version : A better image resize


moment17
03-15-2006, 03:14 AM
I wasn't happy with the quality of admin_resize even at 100%, so I did a quick and dirty function and added to to the bottom of functions.php:


function maxSize($image_width, $image_height, $max_width, $max_height) {

$orientation = ($image_width >= $image_height) ? "landscape" : "portrait";

if ($orientation == "landscape") {
$factor = $image_height / $image_width;
} else {
$factor = $image_width / $image_height;
}

while ($image_width > $max_width || $image_height > $max_height) {

if ($orientation == "landscape") {
// width;
$image_height = round(($image_width * $factor));
if ($image_width > $max_width || $image_height > $max_height) {
$image_width = round($image_width - 5);
$image_height = round(($image_width * $factor));
}
} else {
// height;
$image_width = round(($image_height / $factor));
if ($image_width > $max_width || $image_height > $max_height) {
$image_height = round($image_height - 5);
$image_width = round(($image_height * $factor));
}
}

}

$img = array($image_width, $image_height);
return $img;

}

and in index.php down about line 253 (just below $image_width = $image_extra['0']; and $image_height = $image_extra['1'];) add:

$max_width = 790;
$max_height = 590;
$arSize = maxSize($image_width, $image_height, $max_width, $max_height);

and replaced $image_width and $image_height with the returned array vars in the following lines:
$tpl = str_replace("<IMAGE_WIDTH>",$image_width,$tpl);
$tpl = str_replace("<IMAGE_HEIGHT>",$image_height,$tpl);

to:
$tpl = str_replace("<IMAGE_WIDTH>",$arSize[0],$tpl);
$tpl = str_replace("<IMAGE_HEIGHT>",$arSize[1],$tpl);

I'm sure there's somepalce better for $max_width and $max_height (like in the database) but I just wanted to "set it and forget it" for the time being.

This works very well for me and takes little cpu on an old PIII/700 server running fedora. It also has the advantage that if you change your template to one with a different size image area you aren't stuck with a bunch of ill-fitting images.

se.nsuo.us
03-15-2006, 03:23 AM
I wasn't happy with the quality of admin_resize even at 100%
What do you mean by this? what was the problem that your hack solves?

moment17
03-15-2006, 05:07 AM
What do you mean by this? what was the problem that your hack solves?

The image quality that the resize addon produces in the scaled images isn't what I had hoped. I haven't looked at the code of it, but I assume it uses GD. Granted jpg to jpg is a lossy process, but I honestly have never found the GD libs to produce a very high quality product, tending to excessively blur images. (I'm using php 4.4.0 linked against gd 2.0.33, so it's not an "old libs" issue.)

The problem it solves is that it sets the image size using the img tag width and height properties, as does PP, but it also limits the maximum width and height - which PP doesn't do. Another advantage, as stated in the original post, is that if you move from a somewhat narrow layout template to a wider one there's no easy way (that I can see anyway) to "re-set" the display size of the images that have already been uploaded and scaled using the resize addon.

se.nsuo.us
03-15-2006, 05:14 AM
So... your hack does not produce sharper images, just fixes a maximum on the dimensions right?

moment17
03-15-2006, 05:59 AM
So... your hack does not produce sharper images, just fixes a maximum on the dimensions right?

Both. Sharpness is somewhat subjective, but I don't think the quality is there with the scaled images produced by the addon. I've been using GD for over six years and it's always been less than spectacular in my opinion. (I've been in graphics for 20 years and spend probably too much time looking at images and their relative quality).

se.nsuo.us
03-15-2006, 06:21 AM
I am lost here - how does your hack produce sharper image? resizing using HTML? What about the horrible ragged edges that causes?

Or may be like you say increased sharpness is subjective.... I guess time to hack something which uses ImageMagicK - I am not happy with GD resizes either

romamor
07-13-2006, 04:04 AM
I think this does not quite work for me: I set the img width="480" height="320" then uploaded a photo in vertical orientation: 427x640 (WxH).


The imaged was resized to the right size, but the wrong way: it compressed the height - the 640 of the vertically oriented photo- to 320, and stretched the width - the 427 - to 480, so the image looks distorted.

What I wanted is the image to keep its vertical orientation and only the proportions to be reduced accordingly.

An yideas what might be wrong here? I set in index.php
$max_width = 480;
$max_height = 480;

and also

$max_width = 480;
$max_height = 320;

and it I got the same distorted image. It seems that it does not detect properly that the image is in vertical orientation because the width and height parameters in the template can only say that the image is width=480 and height 320, unless there is a way to assign the right size for vertical images.

se.nsuo.us
07-13-2006, 04:48 AM
Am not really sure if this is a valid hack at all.... might have worked for the original author but will be unacceptable to most

romamor
07-13-2006, 12:15 PM
There is not much feedback on it either. Anyway, is there a plugn or hack that allows me to properly resize images? In other words, if I have a landscape image of 1024x768 I want it resized to 480x320; if the image is vertical (768x1024), then I want it to be resized proportionally to 320x480, or better yet, constrain both sides but keep the original proportion: e.g. max height=320 and max width = 480. This would allow to display images with different sizes and orientations in a page that does not scroll if I chose the right dimensions.

romamor
07-13-2006, 01:15 PM
Ok, I downloaded the latest version of admin_autoresize and it works as expected; it properly resizes images in either orientation and keeps the proportions.

romamor
07-13-2006, 01:25 PM
Ah... it only works for individual file uploads! It does not work (for me) with the copy folders addon. Is there any way to make it work with copy folders?