0

I have been using the function below to resize image, but realized it does not work on images that are longer then the width. Current Image I'm testing is 1200 x 1800 and is 248kb. With this image the function does nothing to it. When loaded into the directory it not only is the same size 1200 x 1800, but is also still 248kb. I have simplfied the code and tested the code below and sure enough it's giving the same results. Any ideas?

function resize_image($file, $max_resolution){
    if(file_exists($file)){
     
        $original_image = imagecreatefromjpeg($file);
        
        
        //resolution
        $original_width = imagesx($original_image);
        $original_height = imagesy($original_image);
            
        //try width first
        $ratio = $max_resolution / $original_width;
        $new_width = $max_resolution;
        $new_height = $original_height * $ratio;
        
        //If that didnt work
        if($new_height > $max_resolution){
            $ratio = $max_resolution / $original_height;
            $new_height = $max_resolution;
            $new_width = $original_width * ratio;
        }
        
        if($original_image){
            $new_image = imagecreatetruecolor($new_width,$new_height); 
            imagecopyresampled($new_image,$original_image,0,0,0,0,$new_width,$new_height,$original_width,$original_height);
            imagejpeg($new_image,$file,90);
            
        }
        
    }
}


if($_SERVER['REQUEST_METHOD'] == "POST"){
 
    if(isset($_FILES['image']) && $_FILES['image']['type'] == 'image/jpeg'){
        
         print_r($_FILES);
         move_uploaded_file($_FILES['image']['tmp_name'], $_FILES['image']['name']);
        
           $file = $_FILES['image']['name'];
        
            resize_image($file, "500");
            echo "<img src='$file'/>";        
       
    }
    
}
?>
Dr5056732
  • 29
  • 6

0 Answers0