In my case, for some time now I have begun to consider certain strategic classes for my project. One of them is
jquery form-validator that incorporates javascript validations through jquery for multiple uses in the html / php forms I perform.
For your request, in the case of images you can validate the type of image file, its size in px (minimum and maximum), its frame (high-width ratio), among many other things.
In the case of image validation you should only add the additional variables to the input field: data-validation-allowing="jpg"
and data-validation-dimension="min100"
to activate it. See example:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>
<form>
<fieldset>
<div class="form-group">
<?php // campo IMAGEN NATURAL ?>
<label for="img_natural" class="col-sm-6 control-label">Imagen Natural (N)</label>
<div class="col-sm-10"><input type="file" id="img_natural" name="img_natural" accept="image/jpeg" data-validation-allowing="jpg" data-validation-error-msg="Elija una imagen con formato JPG." data-validation="required" />
<p class="help-block">formato .JPG (requerido)</p>
</div>
</div>
<div class="form-group">
<?php // campo IMAGEN MINIATURA ?>
<label for="img_miniatura" class="col-sm-6 control-label">Imagen Miniatura (M)</label>
<div class="col-sm-10"><input type="file" id="img_miniatura" name="img_miniatura" accept="image/jpeg" data-validation-allowing="jpg" data-validation-error-msg="Elija una imagen con formato JPG." data-validation="required" />
<p class="help-block">formato .JPG, tamaño 100 x 100 px</p>
</div>
</div>
<button>Enviar</button>
</fieldset>
</form>
<script>
$.validate({
modules: 'file'
});
</script>
For the case of file sizes, you can include the variables in the input field:
data-validation="mime size" data-validation-allowing="jpg, png, gif" data-validation-max-size="2M
which will validate maximum size 2MB and format of the files to be imported.
It's simple. Surely you will ask yourself, why include classes? In my case the answer was that I'm only going to add classes that solve several problems at the same time. If it only solves a validation or a refill, it's not worth it. This class is worth it.