Browse files

Add formatting enhancements.

  • Loading branch information...
1 parent 0837abf commit 8f2c53651bae87178d7e13dc85e23bb22a990bae @maxim committed Jan 29, 2009
Showing with 15 additions and 11 deletions.
  1. +1 −0 README.rdoc
  2. +14 −11 smart_resize_image.function.php
View
1 README.rdoc
@@ -1,3 +1,4 @@
+= smart_resize_image
Should be easy to find, right? All you want is a function that resizes an image to constraints (doesn't care if it should scale up or down), with possibility to select if you want to keep it proportional, and possibility to use either width or height as the constraint. Also, you want it to preserve transparency damn it! Surprisingly, I was unable to find a good function that does all that, so I decided to attempt writing it. This should do it.
== Features/Usage:
View
25 smart_resize_image.function.php
@@ -1,8 +1,13 @@
<?
- function smart_resize_image( $file, $width = 0, $height = 0, $proportional = false, $output = 'file', $delete_original = true, $use_linux_commands = false ) {
- if ( $height <= 0 && $width <= 0 ) {
- return false;
- }
+ function smart_resize_image($file,
+ $width = 0,
+ $height = 0,
+ $proportional = false,
+ $output = 'file',
+ $delete_original = true,
+ $use_linux_commands = false ) {
+
+ if ( $height <= 0 && $width <= 0 ) return false;
# Setting defaults and meta
$info = getimagesize($file);
@@ -13,9 +18,9 @@ function smart_resize_image( $file, $width = 0, $height = 0, $proportional = fal
# Calculating proportionality
if ($proportional) {
- if ($width == 0) $factor = $height/$height_old;
- elseif ($height == 0) $factor = $width/$width_old;
- else $factor = min( $width / $width_old, $height / $height_old );
+ if ($width == 0) $factor = $height/$height_old;
+ elseif ($height == 0) $factor = $width/$width_old;
+ else $factor = min( $width / $width_old, $height / $height_old );
$final_width = round( $width_old * $factor );
$final_height = round( $height_old * $factor );
@@ -56,10 +61,8 @@ function smart_resize_image( $file, $width = 0, $height = 0, $proportional = fal
# Taking care of original, if needed
if ( $delete_original ) {
- if ( $use_linux_commands )
- exec('rm '.$file);
- else
- @unlink($file);
+ if ( $use_linux_commands ) exec('rm '.$file);
+ else @unlink($file);
}
# Preparing a method of providing result

0 comments on commit 8f2c536

Please sign in to comment.