Resize uploaded image

Discussion in 'PHP' started by lammspillning, Aug 14, 2007.

  1. #1
    Does anyone know the code to resize an image that has been uploaded?

    I have the following code to upload the image:

    <?php
    //create the directory if doesn't exists (should have write permissons)
    if(!is_dir("./files")) mkdir("./files", 0755);
    //move the uploaded file
    move_uploaded_file($_FILES['Filedata']['tmp_name'], "./files/myImage.jpg");
    ?>

    I now need to resize the image to say 360*290

    //thnx
     
    lammspillning, Aug 14, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
  3. Wildhoney

    Wildhoney Active Member

    Messages:
    192
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Here is my function that I use to resize images:

    
    public function doResize($szSource, $szDestination, $szExtension, $iResizeWidth, $iResizeHeight)
    {
    	$szExtension = strtolower($szExtension);
    	
    		switch($szExtension)
    		{
    			case('jpg'): 	$pSource = @imagecreatefromjpeg($szSource); break;
    			case('jpeg'): 	$pSource = @imagecreatefromjpeg($szSource); break;
    			case('gif'): 	$pSource = @imagecreatefromgif($szSource); break;
    			case('wbmp'): 	$pSource = @imagecreatefromwbmp($szSource); break;
    			case('png'): 	$pSource = @imagecreatefrompng($szSource); break;
    		}
    		
    		$pResized = @imagecreatetruecolor($iResizeWidth, $iResizeHeight);
    		list($iWidth, $iHeight) = getimagesize($szSource);
    	
    		if(!@imagecopyresampled($pResized, $pSource, 0, 0, 0, 0, $iResizeWidth, $iResizeHeight, $iWidth, $iHeight))
    		{
    			return false;	
    		}
    
    	switch($szExtension)
    	{
    			case('jpg'): 	$pResult = imagejpeg($pResized, $szDestination, 100); break;
    			case('jpeg'): 	$pResult = imagejpeg($pResized, $szDestination, 100); break;
    			case('gif'): 	$pResult = imagegif($pResized, $szDestination); break;
    			case('wbmp'): 	$pResult = imagewbmp($pResized, $szDestination); break;
    			case('png'): 	$pResult = imagepng($pResized, $szDestination); break;
    		}
    		
        if($pResult)
        {
        	return true;
        }
        	
        return false;
    }
    
    PHP:
     
    Wildhoney, Aug 14, 2007 IP
    adsblog and bpb2221 like this.
  4. adsblog

    adsblog Active Member

    Messages:
    659
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    70
    #4
    wow ! good function ! reps added ! :D
     
    adsblog, Aug 14, 2007 IP
  5. Wildhoney

    Wildhoney Active Member

    Messages:
    192
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Thanks! Just to point out though, that function is from inside one of my classes, so if use the modular approach to PHP then simply remove public from the beginning of the function deceleration.
     
    Wildhoney, Aug 14, 2007 IP