Created
September 24, 2018 11:31
-
-
Save NicholasTurner23/4cc279dfafb74a2bb9cf75b447562316 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(isset($_FILES['files']) && !empty($_FILES['files'])){ | |
$fileName = $_FILES['files']['name']; | |
$fileTemp = $_FILES['files']['tmp_name']; | |
$fileType = $_FILES['files']['type']; | |
$allowedext = array('image/png', 'image/gif', 'image/jpeg', 'image/jpg'); | |
$destination = "img/"; | |
$width = 400; | |
$height = 350; | |
for ($i = 0; $i< count($_FILES['files']['name']); $i++){ | |
if(in_array($fileType[$i], $allowedext)){ | |
list($orignal_width, $original_height) = getimagesize($fileTemp[$i]); | |
$scale_ratio = $orignal_width/$original_height; | |
if(($width/$height)>$scale_ratio){ | |
$width = $height*$scale_ratio; | |
}else{ | |
$height = $width/$scale_ratio; | |
} | |
$img = null; | |
switch ($fileType[$i]){ | |
case "image/jpg": | |
$img = imagecreatefromjpeg($fileTemp[$i]); | |
break; | |
case "image/png": | |
$img = imagecreatefrompng($fileTemp[$i]); | |
break; | |
case "image/gif": | |
$img = imagecreatefromgif($fileTemp[$i]); | |
break; | |
case "image/jpeg": | |
$img = imagecreatefromjpeg($fileTemp[$i]); | |
} | |
$canvas = imagecreatetruecolor($width, $height); | |
imagecopyresampled($canvas, $img, 0, 0, 0, 0, $width, $height, $orignal_width, $original_height); | |
imagejpeg($canvas, $destination.$fileName[$i], 80); | |
}else{ | |
echo "Not in array"; | |
} | |
} | |
}else{ | |
die("Error occurred"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment