Jump to content

Checking for Image - Redundant Code?


doubledee

Recommended Posts

Thanks to everyone's earlier help, I pretty much have my "Upload a Photo" module done.

 

However, I am wondering if the following code snippet can be streamlined?

 

Do I really need to check for Width and Height when above I look at if ($imageDetails = getimagesize($tempFile)){??

 

 

// ********************
// Check File-Type.		*
// ********************
if (empty($errors)){
	if ($imageDetails = getimagesize($tempFile)){
		// Image-Array Exists.
		$width = $imageDetails[0];
		$height = $imageDetails[1];
		$imageType = $imageDetails['mime'];

// IS IT REDUNDANT TO CHECK THE DIMENSIONS???
		// ************************
		// Check for Dimensions.	*
		// ************************
		if ($width && $height){
			// Width and Height Exist.
			// File is Image.

			// ************************
			// Determine Image-Type.	*
			// ************************
			if ($imageType !== 'image/gif' &&
					$imageType !== 'image/jpeg' &&
					$imageType !== 'image/png'){
				// Invalid Image-Type.
				$errors['upload'] = 'Image-type must be either GIF, JPG, or PNG.';
			}else{
				// Valid Image-Type.
				// Continue Processing Upload...
			}//End of DETERMINE IMAGE-TYPE
		}else{
			// File Not Image.
			$errors['upload'] = 'Only Images can be uploaded (i.e. GIF, JPG, or PNG).';
		}//End of CHECK FOR DIMENSIONS

	}else{
		// No Image-Array.
		// File Not Image.
		$errors['upload'] = 'Only Images can be uploaded (i.e. GIF, JPG, or PNG).';
	}
}//End of CHECK FILE-TYPE

 

What do you think?

 

I am leaning towards taking the second check out for brevity...

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/257480-checking-for-image-redundant-code/
Share on other sites

You only need to check the width and height if you want to enforce a maximum/minimum size.  If you don't care what size the image is you do not need to check.

 

...because if the File is not an "Image" then this code...

			if ($imageDetails = getimagesize($tempFile)){

 

...would return FALSE.  Is that right??

 

 

Debbie

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.