homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

scaling images using PHP

Msg#: 4109643 posted 4:21 am on Apr 4, 2010 (gmt 0)

How to scale images using PHP.

Some body asked this question about 7 yearsa ago as you can see here [webmasterworld.com...]

I couldn't find a suitable answer but i think i figured it out.

function scale_image($width, $height, $fit){

echo 'Width: '.$width.' & Height: '.$height.' <br/>To fit into '.$fit.' x '.$fit.'<br/><br/>';

if ($width >= $height){
$denominator = $width/$fit;
$scale_height = $height/$denominator;
$scale_width = $width / $denominator;
echo 'Width is greater than height<br/>';
} else {
$denominator = $height/$fit;
$scale_width = $width/$denominator;
$scale_height = $height / $denominator;
echo 'Hieght is greater than width<br/>';

echo 'Scaled width: '.$scale_width.' & Height: '.$scale_height;

//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($_FILES['image']['tmp_name']);

$fit = 800; //fit into 800x800

//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($_FILES['image']);

switch ($type) {
$image = imagecreatefromgif($_FILES['image']);
$image = imagecreatefromjpeg($_FILES['image']);
$image = imagecreatefrompng($_FILES['image']);

if ($width > $fit || $height > $fit){
//create thumbnail for index image
scale_image($width, $height, $fit/*to fit area $fitx$fit*/);
$image = imagecreatetruecolor($scale_width, $scale_height);
imagecopyresampled($image, $image, 0, 0, 0, 0, $scale_width, $scale_height, $width, $height);

$image_name = 'image';

imagejpeg($image, 'C:/image/'.$image_name.'.jpg');


Hope this helps.




5+ Year Member

Msg#: 4109643 posted 7:56 am on Apr 7, 2010 (gmt 0)

I tried running this script but din't really find any significant difference. Thanks for sharing.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved