Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.9.5

Request #7405 Enhanced fit() function
Submitted: 2006-04-15 20:24 UTC
From: chri1709 Assigned:
Status: Open Package: Image_Transform (version 0.9.0)
PHP Version: Irrelevant OS:
Roadmaps: (Not assigned)    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 44 - 12 = ?

 
 [2006-04-15 20:24 UTC] chri1709 (Christoph Schiessl)
Description: ------------ Enhaned version of fit()... two additional parameters are required: $exactly (bool) - determines wether the image must have the exactly (!) the size as specified by the $width and $height parameters $keepasprect (bool) - determines whether the aspect ratio of the orginal image is mainted or not Test script: --------------- My version of the fit() function: function fit($width, $height, $exactly=false, $keepaspect=true) { if ($width <= 0 || $height <= 0) { return PEAR::raiseError("Invalid arguments.", IMAGE_TRANSFORM_ERROR_ARGUMENT); } $x = $this->img_x / $width; $y = $this->img_y / $height; if ($exactly == false) { if ($x <= 1 && $y <= 1) { return true; } elseif ($x > $y) { return $this->scaleByX($width); } else { return $this->scaleByY($height); } } else { if ($keepaspect == false) { return $this->resize($width, $height); } elseif ($x == $y) { return $this->scaleByX($width); } elseif ($x > $y) { $res = $this->scaleByX($width); if (PEAR::isError($res)) { return $res; } else { return $this->crop($width, $height, 0, round(($this->img_y-$height)/2, 0)); } } elseif ($x < $y) { $res = $this->scaleByY($height); if (PEAR::isError($res)) { return $res; } else { return $this->crop($width, $height, round(($this->img_x-$width)/2, 0), 0); } } } } Expected result: ---------------- With the additial parameter, this function is perfect for creating thumbnails for example! There may be bugs in my version of fit()

Comments

 [2006-04-16 00:01 UTC] jausions (Philippe Jausions)
Doesn't resize() already fit the purpose of the additional parameters? The purpose of the fit() method is really to take an image and make it fit into a given box while preserving aspect ratio. If you want to make thumbnails all of the same size, simply use resize(). I fail to see the advantage of making fit() doing more that what it already does.
 [2006-04-16 09:42 UTC] chri1709
Okay... the difference between resize() and fit(): Imagine that you have to create a thumbnail of an image. The thumbnail must the size of precisely 120x80 pixels (otherwise you would destroy the layout of the page) AND you want to keep the aspect ratio. How would you do that? With my fit() function you would simply do: $im->fit(120, 80, true, true);
 [2006-04-16 21:31 UTC] chri1709
After thinking about the problem I came to conclusion that it would be better modify the resize($new_x = 0, $new_y = 0, $options = null) function and add one additional parameter (keep aspect ratio). It would be easy to use the options array for that purpose...