AW: Bildgröße mit PHP
PHP Code:
001<?php
002//this class is not part of Resize and upload image class.
003//this class is property of some one else.
004//I get this from phpclasses.org but forget the name of author.
005//sorry for that.
006
007class Image {
008 var $FileName;
009 var $FileSize;
010 var $FileType;
011 var $AllowedExtentions = array ("image/png", "image/gif", "image/jpeg", "image/pjpeg", "image/jpg");
012 var $newWidth = 100; // new width
013 var $newHeight = 100; //new height
014 var $TmpName;
015 var $PicDir; //store uploaded images
016 var $MaxFileSize = 2000000; //kbytes
017 var $ImageQuality = 100; // image compression (max value 100)
018
019 function Image($FileName) {
020 $this->FileName=$FileName;
021 }
022
023 function GetInfo() {
024 $out=' <br><br>Upload success!<br>
025 Name: '.$this->FileName.'<br>
026 file size: '.$this->FileSize.' byte<br>
027 file type: '.$this->FileType.'<br>
028 <img src=' . dirname($_SERVER['PHP_SELF']) . '/' . $this->PicDir . $this->FileName . '><br><br>';
029
030 return $out;
031 }
032
033
034 function GetFileExtention ($FileName) {
035 if (in_array ($this->FileType, $this -> AllowedExtentions)) {
036 return true;
037 } else {
038 return false;
039 }
040
041 }
042
043 function ExistFile () {
044 $fileexist = $_SERVER['DOCUMENT_ROOT'] .
045 dirname($_SERVER['PHP_SELF']) .
046 '/' . $this->PicDir .
047 $this->FileName;
048 if (file_exists($fileexist)) { return true; }
049 }
050
051 function GetError ($error) {
052
053 switch ($error) {
054 case 0 :
055 return "Error: Invalid file type <b>$this->FileType</b>! Allow type: .jpg, .jpeg, .gif, .png <b>$this->FileName</b><br>";
056 break;
057
058 case 1 :
059 return "Error: File <b>$this->FileSize</b> is too large! You must upload 1000 MB file<br>";
060 break;
061
062 case 2 :
063 return "Error: Please, select a file for uploading!<br>";
064 break;
065
066 case 3 :
067 return "Error: File <b>$this->FileName</b> already exist!<br>";
068 break;
069 }
070
071 }
072
073
074 function Resize () {
075 // header('Content-Type: image/gif');
076 if (empty ($this->TmpName)) {return $this -> GetError (2);}
077 else if ($this->FileSize > $this->MaxFileSize) {return $this -> GetError (1);}
078 else if ($this -> GetFileExtention ($this->FileName) == false) {return $this -> GetError (0);}
079 else if ($this -> ExistFile()) {return $this -> GetError (3);}
080
081 else {
082
083 $ext = explode(".",$this->FileName);
084 $ext = end($ext);
085 $ext = strtolower($ext);
086
087 // Get new sizes
088 list($width_orig, $height_orig) = getimagesize($this->TmpName);
089
090 $ratio_orig = $width_orig/$height_orig;
091
092 if ($this->newWidth/$this->newHeight > $ratio_orig) {
093 $this->newWidth = $this->newHeight*$ratio_orig;
094 } else {
095 $this->newHeight = $this->newWidth/$ratio_orig;
096 }
097
098 $normal = imagecreatetruecolor($this->newWidth, $this->newHeight);
099
100 if ($ext == "jpg") { $source = imagecreatefromjpeg($this->TmpName); }
101 else if ($ext == "gif") { $source = imagecreatefromgif ($this->TmpName); }
102 else if ($ext == "png")
103 {
104 $this->ImageQuality = 9;
105 $source = imagecreatefrompng ($this->TmpName);
106 }
107
108 imagecopyresampled($normal, $source, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $width_orig, $height_orig);
109
110
111 if ($ext == "jpg") {
112 //ob_start();
113 imagejpeg($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality");
114 //$binaryThumbnail = ob_get_contents();
115 //ob_end_clean();
116 }
117 else if ($ext == "gif") { imagegif ($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality"); }
118 else if ($ext == "png") { imagepng ($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality"); }
119
120 imagedestroy($source);
121
122 //echo $this -> GetInfo();
123
124 }
125
126 }
127
128 function Save() {
129 if (empty ($this->TmpName)) {return $this -> GetError (2);}
130 else if ($this->FileSize > $this->MaxFileSize) {return $this -> GetError (1);}
131 else if ($this -> GetFileExtention ($this->FileName) == false) {return $this -> GetError (0);}
132 else if ($this -> ExistFile ()) {return $this -> GetError (3);}
133
134 else {
135
136 copy($this->TmpName,$this->PicDir.$this->FileName);
137
138 //echo $this -> GetInfo();
139
140 }
141 }
142 }
143
144?>
Bitte kostenlos registrieren, damit du die Links siehst.
Einige
Bitte kostenlos registrieren, damit du die Links siehst.
Bitte kostenlos registrieren, damit du die Links siehst.
Skripte im auf der Seite aufgelistet