title: Django - How to validate uploaded image is of valid extension type and not corrupted
body: <p>So I have this code below for validating and image file being uploaded. The main issues with the way of validating is that the user can simply change the extension and upload an invalid image, which would cause issues on the front-end, because if it tries to render it, it won't be able to. <strong>Is there a way to validate if a valid image file is being uploaded in Django?</strong> Or maybe this is just fine and it not rendering on the front-end is expected behavior when the user uploads an improper image file.</p>
  <pre><code>VALID_IMAGE_EXTENSIONS = ('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')
  upper_image_extensions = tuple([string.upper() for string in VALID_IMAGE_EXTENSIONS])
  VALID_IMAGE_EXTENSIONS += upper_image_extensions
  
  def validate_image_upload(file):
      if not isinstance(file, InMemoryUploadedFile):
          raise ValidationError(&quot;Is not InMemoryUploadedFile&quot;)
  
      if not is_valid_file_of_format(file._name, VALID_IMAGE_EXTENSIONS):
          raise ValidationError(&quot;Image file format is invalid&quot;)
  </code></pre>