All pastes #553989 Raw Edit

Something

public text v1 · immutable
·published 2007-06-09 23:30 UTC
rendered paste body
<?php

// Catch errors

error_reporting(E_ALL);

// Get image location

$image = $_POST["image_file"];

// Get image extension

$ft = explode(".", $image);

$ext = $ft[count($ft) - 1];

// Create unique file name

$uniq = uniqid("img_").".".$ext;

// If the filetype is correct

$continue = true;

$reason = "unknown";

if($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "png") {

	// Copy to $dir

	if(!copy($image, $dir."uploaded/".$uniq)) {
	
		echo "File did not copy";
		
		$continue = false;
	
	}
	
} else {

	echo "Incorrect file type";
	
	$reason = "incorrect";
	
	$continue = false;
	
}

if($continue == true) {

	$error = false;
		
	$src_img = $dir."uploaded/".$uniq;
	
	// Create image out of image based on imgae type (extension)
	
	switch($ext) {
	
		case "gif":
			
			$true_img = @imagecreatefromgif($src_img);
			
			break;
			
		case "jpg":
		
			$true_img = @imagecreatefromjpeg($src_img);
		
			break;
			
		case "jpeg";
		
			$true_img = @imagecreatefromjpeg($src_img);
		
			break;
	
		case "png";
		
			$true_img = @imagecreatefrompng($src_img);
			
			break;
			
	}
	
	// Generate dimensions
	
	$orig_w = imagesx($true_img);
			
	$orig_h = imagesy($true_img);
	
	if($orig_w > $thumb_width || $orig_h > $thumb_height) {
		
		if($orig_w >= $orig_h) {
					
			$new_w = $thumb_width;
						
			$diff = $new_w / $orig_w;
						
			$new_h = $orig_h * $diff;
				
		}
			
		if($orig_h > $orig_w) {
					
			$new_h = $thumb_height;
						
			$diff = $new_h / $orig_h;
						
			$new_w = $orig_w * $diff;
				
		}
		
	} else {
	
		$new_h = $orig_h;
	
		$new_w = $orig_w;
	
	}
	
	// Generate image object
				
	$dst_img = imagecreatetruecolor($new_w, $new_h);
	
	// Copy image and resize
				
	imagecopyresized($dst_img, $true_img, 0, 0, 0, 0, $new_w, $new_h, $orig_w, $orig_h);
		
	// Copy thumbnail to location based on extension
	
	$thumb_path = $dir."thumbnails/".$uniq;
	
	switch($ext) {
	
		case "gif":
			
			imagegif($dst_img, $thumb_path);
			
			break;
			
		case "jpg":
		
			imagejpeg($dst_img, $thumb_path);
		
			break;
			
		case "jpeg";
		
			imagejpeg($dst_img, $thumb_path);
			
			break;
	
		case "png";
		
			imagepng($dst_img, $thumb_path);
			
			break;
			
	}
	
	$r = @move_uploaded_file($src, $thumb_path);
	
	echo $r;
	
	$thumb = $thumb_path;
	
	$image = $src_img;
	
} else {

	$error = true;

	echo "The image could not be copied";
	
	if($reason == "incorrect") {
	
		echo ", because the image's filetype is not supported";
		
	}
	
}

?>