I'm using this script for multiple image upload, but I want to resize each images uploaded through the form so that the maximum height and width should be 500px, without loosing the proportions. Here is the script: while(list($key,$value) = each($_FILES['images']['name'])) { if(!empty($value)) { $filename = $value; $filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line while(file_exists('upimg/'.$filename)) $filename=rand(1,9999).$filename; $add = "upimg/$filename"; //echo $_FILES['images']['type'][$key]; // echo "<br>"; copy($_FILES['images']['tmp_name'][$key], $add); chmod("$add",0777); } PHP: Can anyone please help? Thanks in advance
have a look at php.net at the image functions - particularly to do with "GD". There are loads of examples.
Is there a certain format you want for the final image? Here's the function I usually use for PNGs: function createpng($src,$dest,$width=800,$height=800){ $data=@getimagesize($src); $img=@imagecreatefromstring(file_get_contents($src)); if(!$img) return false; $sw=imagesx($img); $sh=imagesy($img); if($sw<$width&&$sh<$height){ $w=$sw; $h=$sh; }elseif($sw>$sh){ $w=$width; $h=floor(($sh*($height/$sw))); }elseif($sw<$sh){ $w=floor($sw*($width/$sh)); $h=$height; }elseif($sw==$sh){ $w=$width; $h=$height; } $w=($w<1)?1:$w; $h=($h<1)?1:$h; $thumb=@imagecreatetruecolor($w,$h); if($data['mime']=="image/png"){ imagealphablending($thumb,false); imagefill($thumb,0,0,imagecolorallocatealpha($thumb,0,0,0,127)); imagesavealpha($thumb,true); }elseif($data['mime']=="image/gif"){ $transparent=imagecolortransparent($img); if($transparent>=0){ $tcolor=imagecolorsforindex($img,$transparent); $transparent=imagecolorallocate($thumb,$tcolor['red'],$tcolor['green'],$tcolor['blue']); imagefill($thumb,0,0,$transparent); imagecolortransparent($thumb,$transparent); } } imagecopyresampled($thumb,$img,0,0,0,0,$w,$h,$sw,$sh); imagepng($thumb,$dest); imagedestroy($thumb); imagedestroy($img); return true; } PHP: Call the function using createpng($source,$destination,500,500) where $source is the location of the uploaded image, such as $_FILES['images']['tmp_name'], and $destination is where you want to save it.
Perhaps this class will help you out: http://www.kavoir.com/2009/01/php-resize-image-and-store-to-file.html