Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-27-2004, 03:24 AM   PM User | #1
vern
New Coder

 
Join Date: Sep 2004
Location: Minnesota, USA
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
vern is an unknown quantity at this point
Resizing images (Problem with the math)

What I want to do is resize images. I could easily just make a function that resizes it 50% ... but then the output would be 50% no matter how big the image is. I want to set a value for max width and height, but I have trouble doing the math/code to make sure the image doesn't grow bigger than the specified width and height, but still be as big, or close to the size of maximum. Anyone have any sample code? ideas? Thanks.
vern is offline   Reply With Quote
Old 09-27-2004, 08:15 AM   PM User | #2
Celtboy
Regular Coder

 
Join Date: May 2002
Location: Virginia, USA
Posts: 620
Thanks: 0
Thanked 6 Times in 6 Posts
Celtboy is an unknown quantity at this point
I whipped this function up really quickly. It should do what you want

PHP Code:
<?php
/*
 * Name: resizeImage.php
 * Version: 1.0
 * Author: John Collins                   celtboy[at]gmail.com
 *
 * Purpose:
 *       Function takes an image and maximum width and height,
 * and returns new width and height based on maximum values.
 * It returns an associative array with the new width and height
 */
function resizeImage($max_width$max_height$image_file) {

    
/* image, width & height variables */
    
$image_info getImageSize($image_file);
    
$width  $image_info[0];
    
$height $image_info[1];
    
    
/* our return values, default to current image dimensions */
    
$ar_return = array();
    
$ar_return["width"]  = $width;
    
$ar_return["height"] = $height;
    

    
/* Only perform if current dimensions are outside max */
    
if (($width $max_width) || ($height $max_height)) {

        if (
$width >= $height) {
           
$ar_return["width"]  = (int)($max_width);
           
$ar_return["height"] = (int)($height * ($max_width $width));
           
        } else {
           
$ar_return["height"] = (int)($max_height);
           
$ar_return["width"]  = (int)($width * ($max_height $height));
        }
        
    }
    
    return 
$ar_return;
    
// resizeImage()
?>

Sample usage:

PHP Code:
<?php
include("resizeImage.php");

/* Image Variables */
$img_source "my_image.jpg";
$max_width 200;
$max_height 400;

$new_image_data resizeImage($max_width$max_height$img_source);

/* Store results in simple vars */
$width  $new_image_data["width"];
$height $new_image_data["height"];

/* Print resized image */
print "<img src=\"" $img_source "\" width=\"" $width "\" height=\"" $height "\" alt=\"Image of: " $img_source "\" />";

?>

hth,
-Celt
Celtboy is offline   Reply With Quote
Old 09-28-2004, 05:43 AM   PM User | #3
vern
New Coder

 
Join Date: Sep 2004
Location: Minnesota, USA
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
vern is an unknown quantity at this point
Much thanks. I'll hack it up when I get the time. Thanks for taking the time.
vern is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:39 AM.


Advertisement
Log in to turn off these ads.