| scaling images using PHP
|
aformoftruth

msg:4109645 | 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.
<?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
|
bizminder

msg:4111243 | 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.
|
|
|