I have created a function that I use to upload my images in PHP
<?php
function addImage($imageWidthRequired, $imageHeightRequired) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
$imageSize = @getimagesize($_FILES['image']["tmp_name"]);
$imageWidth = $imageSize['0'];
$imageHeight = $imageSize['1'];
if ($imageWidth == $imageWidthRequired && $imageHeight == $imageHeightRequired) {
//Perform an action
}
}
}
?>
Everything works fine but I am now facing a problem. I want the user to be able to upload an image without specifying the required dimensions $imageWidthRequired
and $imageHeightRequired
. That means that the user can upload any dimension of image.
Kindly help me get some ideas to make this functions flexible and allow the user to uploas a file without specifying the required measurements. Thanks
You could set some default parameters for $imageWidthRequired
and $imageHeightRequired
(like NULL) in the function definition and check if the values provided are NULL or something else before proceeding.
<?php
function addImage($imageWidthRequired = null, $imageHeightRequired = null) {
if ($imageWidthRequired == null && $imageHeightRequired == null) {
// image dimensions are not provided
} else {
// image dimensions are provided.
}
}