Welcome to WebmasterWorld Guest from 54.146.201.80

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

scaling images using PHP

     
4:21 am on Apr 4, 2010 (gmt 0)

New User

5+ Year Member

joined:Apr 4, 2010
posts: 1
votes: 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.


<?php
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) {
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['image']);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['image']);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['image']);
break;
}

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.

Truth
7:56 am on Apr 7, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 15, 2010
posts:57
votes: 0


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members