image uploading and validation php

Solutions on MaxInterview for image uploading and validation php by the best coders in the world

showing results for - "image uploading and validation php"
Karl
09 Mar 2020
1
2/***********************************************************************************************/
3    $image = $_FILES['image']['name'];
4    $imageFlag = true;
5    if (empty($image)) {
6      /*  if($gender == "male")
7            $imageName = 'default1.jpeg';
8        else
9            $imageName = 'default2.png';*/
10        $imageFlag = false;
11    } else {
12
13
14        $file_extension = strtolower(pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION));
15
16        $allowed_image_extension = array(
17            "jpeg",
18            "png",
19            "jpg"
20        );
21        // Get Image Dimension
22        $fileinfo = @getimagesize($_FILES["image"]["tmp_name"]); //$fileinfo var contains an array of info about the image
23        $width = $fileinfo[0]; //array at index 0 contains the width of image
24        $height = $fileinfo[1];  // array index 1 contains the height of image
25        /*check image diamentions*/
26        if ($width > 1500 || $height > 1600 || $width > $height) {
27            $output['error']['image'] = "* image dimensions should be less than 400*500 and width should be less than height i.e passport size image";
28            $imageFlag = false;
29
30        }
31
32        /*check image extension*/
33        if (!in_array($file_extension, $allowed_image_extension)) {
34            $imageFlag = false;
35            $output['error']['image'] = "Only jpg , jpeg and png format are allowed";
36        }
37        /*Check image size*/
38        if (($_FILES["image"]["size"] > 20000000)) {
39            $imageFlag = false;
40            $output['error']['image']= "Image size should be less than 2mb";
41        }
42        if ($imageFlag) {
43            /*or uniqueid*/
44            $imageName = time() . 'Teacher.' . $file_extension;
45            $target = ("images/teacher/" . basename($imageName));
46            $m = move_uploaded_file($_FILES['image']['tmp_name'], $target);/*moves the image into the server*/
47
48        }
49
50    }
51    /*****************************************************************************/