Image resize function in PHP

if(isset($_FILES['proof']) && $_FILES['proof']['type'] == 'image/jpeg' && ($_FILES['profile']) && $_FILES['profile']['type'] == 'image/jpeg')
    {
       $temp_file1 = $_FILES['proof']['tmp_name'];
        $temp_file2 = $_FILES['profile']['tmp_name'];
        $target_dir = "../main_tmp/dist/vendor/";            
        $file1 = $_FILES['proof']['tmp_name'];
        $file2 = $_FILES['profile']['tmp_name'];
        $name = $vencode.'.jpeg';        
        $width = 433; // Custom Width for Resized Image
        $height = 320;  // Custom Height for Resized Image
        $resized_image1 = resize_image($file1,$width,$height);
        $resized_image2 = resize_image($file2,$width,$height);
        
        $query = mysqli_query($con,"insert into photo values('$name','$name')");
        if($query)
        {
            move_uploaded_file($file1,"../main_tmp/dist/vendor/".$name);
            move_uploaded_file($file2,"../main_tmp/dist/vendor/profile/".$name);
            echo '
                    alert("Details Added Successfully...!");document.location="vendor_view.php";
                    <!-- history.back(); -->
                ';
            

        }else
        {
            echo '
                alert("Failed to Submited...");history.back();
                ';
            

        }
    }else{
        echo '
                alert("Only jpeg file...");history.back();
                ';
    }
}


function resize_image($file,$new_width,$new_height){
    if(file_exists($file)){
        $org_image = imagecreatefromjpeg($file);
        $org_width = imagesx($org_image);
        $org_height = imagesy($org_image);
        if($org_image){   
            $newimg = imagecreatetruecolor($new_width,$new_height);
            imagecopyresampled($newimg,$org_image,0,0,0,0,$new_width,$new_height,$org_width,$org_height);
            imagejpeg($newimg,$file,90);
        }

    }
}

Leave a comment