Jump to content

Need help with PHP duplicate image detector


helloworld001

Recommended Posts

I have a script that resizes and uploads an image.  It works great.  Now I would like to add a new function to it that will detect if there is a duplicate image already posted.  If there is, it will not upload the image. 

 

I have found a script that has that function.  I am just a little lost on how to incorporate that into my php code.

 

Here is my image upload/resize code.

if(Input::exists()) {
	if(Token::check(Input::get('token'))) {

		// Setting up image folders based on user id
		$userDir= 'images/'.$userid.'/';

		if(!is_dir($userDir)){
			
			mkdir('images/'.$userid.'/', 0775);
		
		} else {
		
			// Uploading image with resize and crop.	
			if(isset($_FILES['image'])) {
			
				if (empty($_FILES['image']['name'])) {
				
						$error = 'Please choose an image!';
						
				} else {
				
					function swapHW() {
						global $height, $width, $newheight, $newwidth;
						
						$tempHeight = $height;
						$tempWidth = $width;
						$height = $tempWidth;
						$width = $tempHeight;
						
						$tempHeight = $newheight;
						$tempWidth = $newwidth;
						$newheight = $tempWidth;
						$newwidth = $tempHeight;
					}
					
					function getOrientedImage($imagePath){
						$image = imagecreatefromstring(file_get_contents($imagePath));
						$exif = exif_read_data($imagePath);
						if(!empty($exif['Orientation'])) {
							switch($exif['Orientation']) {
								case 8:
									$image = imagerotate($image,90,0);
									swapHW();
									break;
								case 3:
									$image = imagerotate($image,180,0);
									break;
								case 6:
									$image = imagerotate($image,-90,0);
									swapHW();
									break;
							}
						}
						
						return $image;
					}
				
					$name 		=	$_FILES['image']['name'];
					$temp 		=	$_FILES['image']['tmp_name'];
					$type		=	$_FILES['image']['type'];
					$size		=	$_FILES['image']['size'];
					$ext 		=	strtolower(end(explode('.', $name)));
					$size2		=	getimagesize($temp);
					$width 		=	$size2[0];
					$height 	=	$size2[1];
					$upload 	=	md5(rand(0, 1000) . rand(0, 1000) . rand(0, 1000) . rand(0, 1000));
					
					// Restrictions for uploading
					$maxwidth	=	6000;
					$maxheight	=	6000;
					$allowed	=	array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');
					
					// Recognizing the extension
					switch($type){
								
						// Image/Jpeg
						case 'image/jpeg':
								$ext= '.jpeg';
						break;
						
						// Image/Jpg
						case 'image/jpg':
								$ext= '.jpg';
						break;
						
						// Image/png
						case 'image/png':
								$ext= '.png';
						break;
						
						// Image/gif
						case 'image/gif':
								$ext= '.gif';
						break;
					}

					// upload variables
					$path			=   $userDir . $upload . $ext;
					$thumb_path		=   $userDir . 'thumb_' . $upload . $ext;
						
					
					// check if extension is allowed.
					if(in_array($type, $allowed)) {
						
						// Checking if the resolution is FULLHD or under this resolution.
						if($width <= $maxwidth && $height <= $maxheight) {
						
							if($size <= 5242880) {
								
								// check the shape of the image
								if ($width == $height) {$shape = 1;}
								if ($width > $height) {$shape = 2;}
								if ($width < $height) {$shape = 3;}
								
								//Adjusting the resize script on shape.
								switch($shape) {
									
									// Code to resize a square image.
									case 1:
										$newwidth =		300;
										$newheight =	225;
									break;
									
									// Code to resize a tall image
									case 2:
										$newwidth 	=	300;
										$ratio 		=	$newwidth / $width;
										$newheight	=	round($height * $ratio);
										
									break;
									
									// Code to resize a wide image.
									case 3:
										$newheight  =	225;
										$ratio		=	$newheight / $height;
										$newidth 	=	round($width * $ratio);
									break;
								}
								
								// Resizing according to extension.
								switch($type) {
								
									// Image/Jpeg	
									case 'image/jpeg';
										$img =		imagecreatefromjpeg($temp);
										$thumb =	imagecreatetruecolor($newwidth, $newheight);
													imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
													imagejpeg($thumb, $thumb_path);
													
									break;
									
									// Image/Jpg	
									case 'image/jpg';
										$img =		imagecreatefromjpeg($temp);
										$thumb =	imagecreatetruecolor($newwidth, $newheight);
													imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
													imagejpeg($thumb, $thumb_path);
													
									break;
									
									// Image/png	
									case 'image/png';
										$img =		imagecreatefrompng($temp);
										$thumb =	imagecreatetruecolor($newwidth, $newheight);
													imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
													imagepng($thumb, $thumb_path);
													
									break;
									
									// Image/gif	
									case 'image/gif';
										$img =		imagecreatefromgif($temp);
										$thumb =	imagecreatetruecolor($newwidth, $newheight);
													imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
													imagegif($thumb, $thumb_path);
									break;
								}
									
									try {
				
										$stmt = $db->prepare("INSERT INTO images(path, thumb_path) 
										VALUES(:path, :thumb_path)");
										
										$stmt->bindParam('path', $path);
										$stmt->bindParam('thumb_path', $thumb_path);
										$stmt->execute();
										
										if($stmt == false){
										
											$error = 'There was a problem uploading your image.';
											
										} else {
										
											$success = 'Your image has been uploaded.';
											move_uploaded_file($temp, $path);
											
										}
										
									} catch(Exception $e) {
										die($e->getMessage());
									}
								
							} else {
								$error = 'Your image size is too big.';
							}
						} else {
							$error = 'Your image resolution exceeds the limit.';
						}

					} else {
						$error = 'Your have uploaded a forbidden extension.'; 
					}
				}
			}
		}	
	}
}	
		

This guy has found a way to find duplicate images.  Here is his script.  http://www.catpa.ws/php-duplicate-image-finder/

 

I could really use some help on integrating his method into mine.

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.