Jump to content

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.