'GIF',2=>'JPG',3=>'PNG',4=>'SWF',5=>'PSD',6=>'BMP',13=>'SWF'); private $file_extensions = array(); // constructor function __construct($MaxWidth=1024, $MaxHeight=768) { $this->MaxWidth = $MaxWidth; $this->MaxHeight = $MaxHeight; // delete any previous session unset($_SESSION['fileupload']); } public function allowed_filetypes($file_extensions="", $type="A") { // default allowed filetypes $this->file_extensions = array("jpg","jpeg","png","gif"); // a list of all allowed file extensions. MUST be an array if(is_array($file_extensions)) { if($type == "A") $this->file_extensions = array_merge($this->file_extensions, $file_extensions); else $this->file_extensions = $file_extensions; } } protected function clean_filename($source_name) { $source_name = strtolower($source_name); # clean filename for spaces, and danish letters # (might substitute any char not being \w\d_-. with a _ to match all unwanted chars) $source_name = addslashes(str_replace(" ", "_", $source_name)); $source_name = addslashes(str_replace("æ", "ae", $source_name)); $source_name = addslashes(str_replace("Æ", "Ae", $source_name)); $source_name = addslashes(str_replace("ø", "oe", $source_name)); $source_name = addslashes(str_replace("Ø", "Oe", $source_name)); $source_name = addslashes(str_replace("å", "aa", $source_name)); $source_name = addslashes(str_replace("Å", "Aa", $source_name)); return $source_name; } public function upload($INPUT_FILE_NAME) { # syntax upload(date("YmdHis")."_$picturename", "picture", "../billeder/"); $MaxWidth = $this->MaxWidth; $MaxHeight = $this->MaxHeight; $picture = $this->clean_filename($_FILES[$INPUT_FILE_NAME]['name']); $file = $_FILES[$INPUT_FILE_NAME]['tmp_name']; if($file) { list($ImageWidth, $ImageWidth, $type) = GetImageSize($file); // $type is a number, we need to compare the chars instead $file_ext = strtolower($this->file_types[$type]); // if we didn't get a file_ext from above, get it from the uploaded file instead if(!$file_ext) { #FIXME eregi(".*(\.[a-z0-9]+)$", $file, $regs); $file_ext = $regs[1]; } // is it an allowed file extension? if(!in_array($file_ext, $this->file_extensions)) { $_SESSION['fileupload']['error'] = "Forkert filnavn!: $picture
Filetypen skal være " . join(", ", $this->file_extensions) . ", den uploadede fil var en $file_ext ($type)"; return 0; } /* // 14/10-2006: en tanke... burde man ikke bare resize til den max der nu er sat? if($ImageWidth > $MaxWidth && $ImageHeight > $MaxHeight) { $_SESSION['fileupload']['error'] = "\nBilledet er for stort i bredden (Max. $MaxWidth pixels) og i højden (Max. $MaxHeight pixels)"; return 0; } elseif($ImageWidth > $MaxWidth) { $_SESSION['fileupload']['error'] = "\nBilledet er for stort i bredden (Max. $MaxWidth pixels)"; return 0; } elseif($ImageHeight > $MaxHeight) { $_SESSION['fileupload']['error'] = "\nBilledet er for stort i højden (Max. $MaxHeight pixels)"; return 0; } */ else { # check that the fileext isn´t allready there #if(!ereg($fileext . "$", $picture)) # $picture = "$new_filename." . $fileext; $this->filename = $picture; // check if the file exists and rename uploaded file if so if(file_exists($this->upload_dir . basename($picture))) { $picture = date("Ymdhis") . "_$picture"; $_SESSION['fileupload']['message'] = ' ' . $this->filename . ' findes allerede. Billedet er omdøbt til ' . $picture . '

'; $this->message = $_SESSION['fileupload']['message']; } $_SESSION['fileupload']['filename'] = $this->filename; // now copy the file to the right location if($ImageWidth > $MaxWidth || $ImageHeight > $MaxHeight) { $this->resize($file, $this->upload_dir . basename($this->filename)); return 1; } elseif(copy($file, $this->upload_dir . basename($this->filename))) { return 1; } else { #$_SESSION['fileupload']['error'] = "Error - Upload Failed!"; $_SESSION['fileupload']['error'] = "Fejl - Upload fejlede! " . $_FILES[$INPUT_FILE_NAME]['error']. ""; return 0; } } } else { // do nothing, no file might be okay #$_SESSION['fileupload']['error'] = "no file?"; return -1; } } public function get_error() { if(isset($_SESSION['fileupload']['error'])) return $_SESSION['fileupload']['error']; else return 0; } public function delete_error() { unset($_SESSION['fileupload']['error']); } function resize($source, $dest) { # copy the source file to new file copy($source, $dest); list($source_width, $source_height, $type) = getimagesize($source); $filetype = strtolower($this->file_types[$type]); $percent_w = intval($this->MaxWidth / $source_width * 100) / 100; $percent_h = intval($this->MaxHeight / $source_height * 100) / 100; # use lowest percentage $percent_h > $percent_w ? $percent = $percent_w : $percent = $percent_h; $new_width = intval($source_width*$percent); $new_height = intval($source_height*$percent); # resize the image, IE set thumb size for the $new_image $image_p = imagecreatetruecolor($new_width, $new_height); // preserve transparency if($type == "gif" or $type == "png"){ imagecolortransparent($image_p, imagecolorallocatealpha($image_p, 0, 0, 0, 127)); imagealphablending($image_p, false); imagesavealpha($image_p, true); } # Identifiers for the pictures if($filetype == 'jpg') $image = ImageCreateFromJPEG($source); elseif($filetype == 'png') $image = imagecreatefrompng($source); # resize the image, IE set new size for the $new_image # We get a better result with ImageCopyResampled() than ImageCopyResized() imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $source_width, $source_height); # create the new image if($filetype == 'jpg') Imagejpeg($image_p, $dest, 100); elseif($filetype == 'png') { imagepng($image_p, $dest, 9); } // give info when image has been resized $_SESSION['fileupload']['message'] .= '  Billedet er blevet nedskaleret.

'; $this->message = $_SESSION['fileupload']['message']; // Free up memory imagedestroy($image_p); } function make_thumb($source, $dest, $thumbnail_size=100) { # usage make_thumb("dir/dummy.jpg"); # $FU->make_thumb($_FILES['picture']['tmp_name'], "thumbs/".$_SESSION['fileupload']['filename'], 150); $im_size = GetImageSize($source); $source_width = $im_size[0]; $source_height = $im_size[1]; # addslashes and str replace to ensure that there are no spaces or wierd chars $source_name = addslashes(str_replace(" ", "_", $source)); # create name for thumbnail # PHP5 oddie, \- does NOT work for - in a regex, it MUST be in the end of the [] brackets #$tmp = eregi("^([a-z0-9_\-\./ ]+)\.(jpg|jpeg)$", $source_name, $regs); $tmp = eregi("^([a-z0-9_\./ -]+)\.(jpg|jpeg)$", $source_name, $regs); #$new_file_name = $regs[1]."-thumb.jpg"; $new_file_name = $dest; # copy the source file to new file copy($source, $dest); # check if width is larger than height if ($source_width >= $source_height) { $thumb_width = $thumbnail_size; $thumb_height = intval(($thumb_width/$source_width)*$source_height); } else { $thumb_height = $thumbnail_size; $thumb_width = intval(($thumb_height/$source_height)*$source_width); } # resize the image, IE set thumb size for the $thumb_image $image_p = imagecreatetruecolor($thumb_width, $thumb_height); # Identifiers for the pictures $image = ImageCreateFromJPEG($dest); # Identifiers for the new image #$new_image = ImageCreate($new_width, $new_height); # resize the image, IE set new size for the $new_image # We get a better result with ImageCopyResampled() than ImageCopyResized() ImageCopyResampled($image_p, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $source_width, $source_height); #ImageCopyResized($new_image, $source_image, 0, 0, 0, 0, $new_width, $new_height, $source_width, $source_height); # create the new image Imagejpeg($image_p, $dest, 100); // Free up memory imagedestroy($image_p); } } ?>