Thursday, July 30, 2009

How do I resize pictures in PHP without loosing quality?

I am writing a PHP script where you can upload pictures and they automatically get resized to three different thumbnails. Everything works fine, except that the quality of the generated thumbnails is nothing like I would like it to be. Anyone have suggestions what I can do?





Thank you.

How do I resize pictures in PHP without loosing quality?
It depend how you resize


If you are just changing the dimensions that means you are loosing quality


You should use software attached to your script to modify the pixels, or you can do it with Java


Try to add a java command to reduce the pixels with the dimensions,


Good luck
Reply:%26lt;?php





function imageResize($width, $height, $target) {





//takes the larger size of the width and height and applies the


formula accordingly...this is so this script will work


dynamically with any size image





if ($width %26gt; $height) {


$percentage = ($target / $width);


} else {


$percentage = ($target / $height);


}





//gets the new value and applies the percentage, then rounds the value


$width = round($width * $percentage);


$height = round($height * $percentage);





//returns the new sizes in html image tag format...this is so you


can plug this function inside an image tag and just get the





return "width=\"$width\" height=\"$height\"";





}





?%26gt;
Reply:I think, there has several commands but try this


imagecopyresampled();





Have a look those links





http://www.vipercreations.com/tutorials/...





http://www.webmasterworld.com/forum88/80...





http://uk.php.net/gd





http://blazonry.com/scripting/upload-siz...





http://aspn.activestate.com/ASPN/docs/PH... - Php image function





http://www.spinwave.com/index.php - Image compression program
Reply://This will set a image's dimensions to be within a certain width %26amp; height you want with the width %26amp; height being in proporation.


$widthYouWant = 150; //This can be any size you set


$heightYouWant = 150;//This can be any size you set





//This is the full image name and location


$image = "http://" . $_SERVER['HTTP_HOST'] . "/" . $directoryName . "/" . $imageName;


if(is_file($image)){ //makes sure the file is an actual file





list($width, $height, $type, $attr) = getimagesize("img/flag.jpg"); //gets the size of the image





//This will set a width to a certain width you want


if($width %26gt; $widthYouWant){


$widthDivider = $width/$widthYouWant;


$width = $widthYouWant;


$height = $height/$widthDivider;


}


//Size will already be set in proportion to a width being max 150 pixels


if($height %26gt; $heightYouWant){


$heightDivider = $height /$heightYouWant;


$width = $width/$heightDivider;


$height = heightYouWant;


}





}


else{


$error = "File doesn't exist.";


}





//set the image tag with width="%26lt;?=$width?%26gt;" height="%26lt;?=$height?%26gt;"


No comments:

Post a Comment