Linux, Apache, PHP, MySQL Administrators Forums

Go Back   Linux, Apache, PHP, MySQL Administrators Forums > LAMP Forums > PHP - Hypertext Preprocessor > PHP Tutorials

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-05-2009, 09:45 AM
zoticai+c's Avatar
Ambiguos Variable
 
Join Date: Jan 2009
Posts: 2,241
Lightbulb Image Resize with PHP/ImageMagick Maintaining Proportions

There are many examples found on the web on resizing images using PHP while maintaining aspect ratio. However most of them are based on a single maximum dimension i.e. should not exceed a certain size with on either height or width.

I have written the code below that will accept a desired height and width and downsize the source image without exceeding either $maxheight or $maxwidth but still maintain proportions.

Adjust your path to ImageMagick accordingly (/usr/bin/convert)


PHP Code:
function resizeimage($source,$dest,
    
$maxwidth 200,$maxheight 160)
{
    list(
$width,$height) = getimagesize($source);
    
    
/**
     * We need to get both ratios so we can 
     * find which reduced height and width 
     * will fix the max allowed dimensions.
     */
    
$hRatio $maxheight $height;
    
$wRatio $maxwidth $width;
    
    
/**
     * Test Dimensions based on height reduction ratio.
     */
    
$tHeightHR $maxheight;
    
$tWidthHR ceil($hRatio $width);
    
    
/**
     * Test dimenstions based on width reduction ratio.
     */
    
$tWidthWR $maxwidth;
    
$tHeightWR ceil($wRatio $height);
    
    if(
$width $maxwidth AND $height $maxheight)
    {
        echo 
'Source already below maximum dimensions: ' 
            
$source " {$width}x{$height}\n";
        return 
false;
    }
    
    if(
$tWidthHR <= $maxwidth) { 
        
$height $tHeightHR$width $tWidthHR
    }
    if(
$tHeightWR <= $maxheight) { 
        
$height $tHeightWR$width $tWidthWR
    }
    
    
$cmd "/usr/bin/convert -resize {$width}x{$height} "
        
"\"{$source}\" \"{$dest}\" 2>&1";
    @
exec($cmd,$output,$retvar);

    if(
$retvar != 0
    {
        echo 
implode(" -- ",$output);
        return 
false;
    }
    
    return 
true;

This tutorial is also available from my blog.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-18-2009, 04:00 PM
Junior Member
 
Join Date: Feb 2009
Posts: 9
Default thanks

Thanks zoticai+c I was not actually looking for this, but I got to improve what I already have.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
image downsize, image resize, imagemagick, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:20 PM.


Community hosted with DotManila Consulting
Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2