login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Upload systemen > Image Upload Class + Thumbnail

Image Upload Class + Thumbnail

Auteur: ArieMedia - 11 juni 2009 - 09:02 - Gekeurd door: Koen - Hits: 5952 - Aantal punten: 1.50 (3 stemmen)



Voorafgaand een aantal mededelingen
@ Voorbeeld: Wachtwoord: 1234hoedjevan
Ik heb gekozen om alleen nog maar afbeeldingen te ondersteunen,
andere bestanden is goed mogelijk, maar let goed op dat dit fouten kan veroorzaken

De class includen en vervolgens een aantal instellingen doen.
(Voorbeeld instellingen)

  1. include 'upload_image.php';
  2. $img = new upload_image();
  3. $img->imageDir = './images/';
  4. $img->thumbDir = './thumb/';
  5. $img->extensions = array('jpg', 'gif', 'jpeg', 'png');
  6. $img->size = 120450;
  7. $img->thumb_x = 200;


$this->imageDir : Hier worden de geuploade bestanden opgeslagen
$this->thumbDir : Als thumbnails aan staan komen hier je thumbnails (JPG/GIF/PNG)
$this->extensions = array(); : In een array staan de toegestane extensies
$this->size: Maximum filesize
$this->thumb_x: De breedte van de thumbnails, hoogte word uitgerekend!
$this->blackBar = boolean : Zwarte balk bij de thumbnail met naam van bestand?
Voorbeeld hoe ik het gebruik:

  1. include 'upload_image.php';
  2. $img = new upload_image();
  3. $img->imageDir = './images/';
  4. $img->thumbDir = './thumb/';
  5. $img->extensions = array('jpg', 'gif', 'jpeg', 'png'); // werkt dit niet, gebruik dan mimetype na de /
  6. $img->size = 800000;
  7. $img->thumb_x = 200;
  8. if(isset($_POST['upload'])) {
  9. $img->upload();
  10. echo $img->alert();
  11. } else {
  12. // formulier
  13. }


Als je thumbnails gebruikt, upload dan ook een font (is het anders dan ARIAL.TTF UPPERCASE!!) Pas dit dan aan in het script

Code:
  1. <?php
  2. /**
  3.  * CLASS UPLOAD_IMAGE
  4.  * With this class you can upload an image and a thumbnail will be created.
  5.  * AUTHOR : ARJAN VELDKAMP (ARIE2ZERO.NL)
  6.  * Released 4 june 2009
  7. **/
  8.  
  9. class upload_image {
  10.  
  11. /**
  12. * Before you use the function upload, make sure you defined:
  13. - extensions wich is an array
  14. - imageDir, the directory must have the write rights
  15. - thumbDir, the directory must have the write rights
  16. - thumb_x, the width of the created thumbnail
  17. - size, the maximum filesize wich is allowed
  18. - blackbar, set this on true for thumnails with black bar
  19. **/
  20. public $extensions= array();
  21. public $imageDir;
  22. public $thumbDir;
  23. private $alert= array();
  24. public $thumb_x;
  25. private $thumb_y;
  26. public $blackBar=true;
  27. public $size;
  28.  
  29. /**
  30. * Input: File
  31. * Output: true/false
  32. * Descrption: When the upload is succesfull this function will return true.
  33. * When mkthum is true, thumbnails will be created,
  34. * When overWrite is true, If there allready exists a file with this name the file will be overwritten
  35. **/
  36. public function upload($mkthumb=true, $overWrite=true) {
  37. if(is_uploaded_file($_FILES['bestand']['tmp_name'])) {
  38. $this->alert[] = 'Uploading in process';
  39. $this->alert[] = 'Type: '.$_FILES['bestand']['type'];
  40. if(is_dir($this->imageDir)) {
  41. $this->alert[] = 'Directory found!';
  42. $ext = strtolower(array_pop(explode('.', $_FILES['bestand']['name'])));
  43. if(in_array($ext, $this->extensions)) {
  44. $file = $this->convert_file($_FILES['bestand']['name']);
  45. if($_FILES['bestand']['size'] < $this->size) {
  46. if($this->write($file, $overWrite) == true) {
  47. if(move_uploaded_file($_FILES['bestand']['tmp_name'], $file)) {
  48. chmod($file, 0777);
  49. if($mkthumb == true) { $this->thumbnail($file, $ext, $_FILES['bestand']['name']); }
  50. $this->alert[] = 'Your file has been uploaded succesfully';
  51. $this->alert[] = 'URL: '.$file;
  52. return true;
  53. } else { $this->alert[] = 'Cannot move '.$_FILES['bestand']['name'].' to '. $file; return false; }
  54. }
  55. } else { $this->alert[] = 'Overwriting maximum MB'; return false; }
  56. } else { $this->alert[] = 'This extension is not allowed!'; }
  57. } else { $this->alert[] = 'The given location doesnt exist'; return false; }
  58. } else { $this->alert[] = 'No file given'; return false; }
  59. }
  60.  
  61. /**
  62. * Input: filename, boolean, boolean
  63. * Output: New Filename
  64. * Description: Converts the filename to an valid filename
  65. * When forThumb is true, the filename will get an other name.
  66. * When meld is true, errormessages will be produced.
  67. **/
  68.  
  69. private function convert_file($name, $forThumb=false, $meld=true) {
  70.  
  71. $sFor = array(' ', "'", '\\', '"');
  72. $nname = str_replace($sFor, '_', $name);
  73. $nname = explode('.', $nname);
  74. $ext = array_pop($nname);
  75. $nname = implode('_', $nname) .'.'. $ext;
  76.  
  77. if($forThumb == true) {
  78. $nname = explode('.', $nname);
  79. $file = strtolower($this->thumbDir .'thumb_'. $nname[0].'.png');
  80. if($meld == true) { $this->alert[] = 'Thumbnail renamed From: '.$name .' to '. $nname[0]; }
  81. } else {
  82. $file = strtolower($this->imageDir .$nname);
  83. if($meld == true) { $this->alert[] = 'File renamed From: '.$name .' to '. $nname; }
  84. }
  85. return $file;
  86. }
  87.  
  88. /**
  89. * Input: Uploaded_file, extension, filename
  90. * Output: Resized file
  91. * Description: This function will create thumbnails with a black bar on bottom
  92. * The thumnail will be .png and resize the height automaticly,
  93. * This function will not create a thumbnail if the given file is no image.
  94. **/
  95.  
  96. private function thumbnail($file, $ext, $name) {
  97. $blBar = $this->blackBar;
  98. $this->alert[] = 'Trying to create Thumbnail';
  99. $exten = array('jpg', 'jpeg', 'gif', 'png');
  100. if(in_array($ext, $exten)) {
  101. if(is_dir($this->thumbDir)) {
  102. $this->alert[] = 'Thumb Directory found!';
  103.  
  104. list($width, $height) = getimagesize($file);
  105. if($blBar == true) {
  106. $this->thumb_y = round(($height/$width) * $this->thumb_x, 0) + 16;
  107. } else $this->thumb_y = round(($height/$width) * $this->thumb_x, 0);
  108.  
  109. $dest = imagecreatetruecolor($this->thumb_x, $this->thumb_y);
  110. if($ext == 'jpg' || $ext == 'jpeg') {
  111. $file = imagecreatefromjpeg($file);
  112. } elseif($ext == 'gif') {
  113. $file = imagecreatefromgif($file);
  114. } elseif($ext == 'png') {
  115. $file = imagecreatefrompng($file);
  116. } else $this->alert[] = 'Cant create thumbnail from this extension';
  117. if($blBar == true) {
  118. $fill = $this->thumb_y - 16;
  119. } else $fill = $this->thumb_y;
  120.  
  121. $wit = imagecolorallocate($dest, 255,255,255);
  122. imagecopyresampled($dest, $file, 0, 0, 0, 0, $this->thumb_x, $fill, $width, $height);
  123. if($blBar == true) {
  124. imagefilledrectangle($dest, 0, $fill, $this->thumb_x, $this->thumb_y, imagecolorallocate($dest, 0,0,0));
  125. $fill = $fill + 10;
  126. imagettftext($dest, 8, 0, 2, $fill, $wit, 'ARIAL.TTF', $name);
  127. }
  128. if(imagepng($dest, $this->convert_file($name, true))) {
  129. $this->alert[] = 'Thumbnail created!';
  130. $this->alert[] = 'Result: <br> <img src="'.$this->convert_file($name, true, false).'" />';
  131. imagedestroy($dest);
  132. } else $this->alert[] = 'No Thumnail created';
  133. } else $this->alert[] = 'The Thumb Directory has not been found!';
  134. } else $this->alert[] = 'Wrong extension to create Thumbnail';
  135. }
  136.  
  137. /**
  138. * Input: Filename, boolean
  139. * Output: true/false
  140. * Description: checks if the file allready exists
  141. **/
  142.  
  143. private function write($file, $overWrite) {
  144. if(is_file($file)) {
  145. $this->alert[] = 'Warning, this file allready exsist!';
  146. if($overWrite == true) {
  147. $this->alert[] = 'File has been overwritten';
  148. return true;
  149. } else {
  150. $this->alert[] = 'File has not been uploaded, file may not be overwritten';
  151. return false;
  152. }
  153. } else return true;
  154. }
  155.  
  156. /**
  157. * Input: None
  158. * Output: All alerts
  159. * Description: This function will echo all created alerts while uploading.
  160. **/
  161.  
  162. public function alert() {
  163. $txt = '<div style="width: 500px; background-color: #ccc; color: #f00; padding: 3px; text-align: center; border: 1px solid #000;">
  164. <b>THESE MESSAGES HAS BEEN RECEIVED:</b><br>';
  165. foreach($this->alert as $alert) {
  166. $txt .= $alert .'<br>';
  167. }
  168. $txt .= '</div>';
  169. return $txt;
  170. }
  171. }
  172. ?>

Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (7)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.061s