Resize uploaded image

Discussion in 'PHP' started by levani, Feb 21, 2010.

  1. #1
    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
     
    levani, Feb 21, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,903
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    have a look at php.net at the image functions - particularly to do with "GD". There are loads of examples.
     
    sarahk, Feb 22, 2010 IP
  3. chadsmith

    chadsmith Peon

    Messages:
    82
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    chadsmith, Feb 22, 2010 IP
  4. TheDataPlanet.com

    TheDataPlanet.com Well-Known Member

    Messages:
    503
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #4
    TheDataPlanet.com, Feb 22, 2010 IP