Forum Moderators: coopster
<?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');
?>