need some help figuring out some image resizing logic
Posted: Sun Oct 05, 2008 12:40 pm
Hi,
I have this image size down rule for resizing down images based on their original width and height. There are 6 possible sizes (from extra small to xx large). Im stuck figuring out the logic for resizing according to this rule.
So here is the rule in the class...
and here is the method that should handle the logic...
So I already figured out which possible sizes the image could be sized down to...
But im still missing the logic for figuring out the actual numeric values for those possibilities that passed the test. It seems obvious its a percent value BUT I have not idea no idea how to write that portion of logic.
Any ideas, comments or suggestions are welcome. Just need to pointers to set me in the right path.
Many thanks!
Oh if you're curious here are the other methods referenced which work perfect... just missing that logic to send the right value...
I have this image size down rule for resizing down images based on their original width and height. There are 6 possible sizes (from extra small to xx large). Im stuck figuring out the logic for resizing according to this rule.
So here is the rule in the class...
Code: Select all
private $SIZE_RULE = array
(
array('name' => 'XSmall', 'min' => 250, 'max' => 450),
array('name' => 'Small', 'min' => 451, 'max' => 850),
array('name' => 'Medium', 'min' => 851, 'max' => 1650),
array('name' => 'Large', 'min' => 1651, 'max' => 3250),
array('name' => 'XLarge', 'min' => 3251, 'max' => 4999),
array('name' => 'XXLarge', 'min' => 5000, 'max' => 5000),
);Code: Select all
private function create($img, $path, $name)
{
$i = imagecreatefromjpeg($img);
$w = imagesx($i);
$h = imagesy($i);
foreach ($this->SIZE_RULE as $s)
{
if ($w >= $s['min'] && $w <= $s['max'] || $h >= $s['min'] && $h <= $s['max'])
{
$n = $path.$name.$s['name'].'.jpg';
// this is just to determine the layout (horizontal or vertical) for the width and height
// where $s[???] would be the value I need to send in but I dont know how to figure it out based on the size rule
$d = $this->getDimensions($s[???], $w, $h);
$this->newImage($i, $n, $d->nw, $d->nh, $w, $h); // and this is where image gets sent off to be created
}
}
}But im still missing the logic for figuring out the actual numeric values for those possibilities that passed the test. It seems obvious its a percent value BUT I have not idea no idea how to write that portion of logic.
Any ideas, comments or suggestions are welcome. Just need to pointers to set me in the right path.
Many thanks!
Oh if you're curious here are the other methods referenced which work perfect... just missing that logic to send the right value...
Code: Select all
private function newImage($img, $name, $nw, $nh, $w, $h)
{
$ti = imagecreatetruecolor($nw, $nh);
imagecopyresampled($ti, $img, 0, 0, 0, 0, $nw, $nh, $w, $h);
imagejpeg($ti, $name, 100);
}
private function getDimensions($v, $w, $h)
{
switch (true)
{
case $w > $h: $nw = $v; $nh = floor($h * ($nw / $w)); break;
case $w < $h: $nh = $v; $nw = floor($w * ($nh / $h)); break;
default: $nh = $nw = $v; break;
}
$r->nw = $nw;
$r->nh = $nh;
return $r;
}