Start > Zonder rubriek > Afbeelding Uploadscript

Afbeelding Uploadscript

18 April 2009

Ik had een uploadscript nodig voor afbeeldingen. Dus ik ging op het internet zoeken of er nog een mooie te downloaden was, twee keer het wiel uitvinden is ja niet nodig! Maar elk script dat ik testte had óf messy code, óf deed niet wat ik wou. Daarom ging ik zelf maar eventjes aan de slag. Hieruit kwam een simpel uploadscriptje die jpg, gif en png bestanden kan uploaden en deze ook nog kan schalen naar een andere grootte.

  1. <?php
  2.  
  3. /**
  4. * JaapzUpload
  5. * Een simpel uploadscript met schaal functie gemaakt door:
  6. * Jaap Broekhuizen a.k.a. Jaapz
  7. * Site: www.jaapz.nl
  8. * Mail: jaapz.b[apenstaartje]gmail[punt]com
  9. * Vrijgegeven onder de GNU GPL2 licentie
  10. */
  11.  
  12. // Variabelen
  13. $verzend = $_POST[‘verzend’];
  14. $resize = $_POST[‘resize’];
  15. $width = $_POST[‘width’];
  16. $map = ‘../uploads/’;
  17.  
  18. // Tekst echoen
  19. echo
  20. <h1>Bestand Uploaden</h1>
  21.  
  22. ;
  23.  
  24. // Extensies die mogen upgeload worden
  25. $mag_types = array(‘image/jpeg’,
  26.                                    ‘image/gif’,
  27.                                    ‘image/png’);
  28.  
  29. // Is er al wat upgeload?
  30. if (!isset($verzend)) { // Nee
  31.         ?>
  32. <form method="post" target="_self" enctype="multipart/form-data" action="upload.php">
  33.         Selecteer een bestand:
  34. <input name="bestand" type="file">
  35. <input type="checkbox" name="resize" onclick="if(this.checked){document.getElementById(’resize’).style.display=’block’}else{document.getElementById(’resize’).style.display=’none’}" /> Afbeelding schalen?
  36. <div id="resize" style="display:none">
  37.                 Breedte:
  38. <input type="text" name="width" />px
  39.         </div>
  40. <input type="submit" value="Uploaden!" name="verzend" />
  41.         </form>
  42.  
  43.         <?php
  44. } else { // Ja
  45.         if (is_uploaded_file($_FILES[‘bestand’][‘tmp_name’])) {
  46.                 if (!in_array($_FILES[‘bestand’][‘type’], $mag_types)) {
  47.                         die(‘Dit type bestand mag niet geupload worden!<a href="javascript:history.back(\’1\’)">Ga terug</a>’);
  48.                 }
  49.  
  50.                 move_uploaded_file($_FILES[‘bestand’][‘tmp_name’], $map . $_FILES[‘bestand’][‘name’]);
  51.                 echo ‘Dit bestand is succesvol geupload in de map uploads onder de naam: <a href="’ $map . $_FILES[‘bestand’][‘name’] . ‘" title="Bekijk je upload">’ . $_FILES[‘bestand’][‘name’] . ‘</a>’;
  52.  
  53.                 if ($resize == ‘on’) {
  54.                         $helebestand = $map . $_FILES[‘bestand’][‘name’];
  55.                     $dimensions = getimagesize($helebestand);
  56.  
  57.                         $bronbreedte = $dimensions[0];
  58.                         $bronhoogte  = $dimensions[1];
  59.  
  60.                         $doelhoogte = ($bronhoogte * $width) / $bronbreedte;
  61.                         $doelhoogte = round($doelhoogte, 0);
  62.  
  63.                         if (substr($helebestand, -3) == ‘gif’) {
  64.                                 $image = imagecreatefromgif($helebestand);
  65.                                 $destination = imagecreatetruecolor($width, $doelhoogte);
  66.                                 imagecopyresampled($destination, $image, 0, 0, 0, 0, $width, $doelhoogte, $bronbreedte, $bronhoogte);
  67.                                 imagegif($destination, $helebestand);
  68.                         } elseif (substr($helebestand, -3) == ‘png’) {
  69.                                 $image = imagecreatefrompng($helebestand);
  70.                                 $destination = imagecreatetruecolor($width, $doelhoogte);
  71.                                 imagecopyresampled($destination, $image, 0, 0, 0, 0, $width, $doelhoogte, $bronbreedte, $bronhoogte);
  72.                                 imagepng($destination, $helebestand);
  73.                         } else {
  74.                                 $image = imagecreatefromjpeg($helebestand);
  75.                                 $destination = imagecreatetruecolor($width, $doelhoogte);
  76.                                 imagecopyresampled($destination, $image, 0, 0, 0, 0, $width, $doelhoogte, $bronbreedte, $bronhoogte);
  77.                                 imagejpeg($destination, $helebestand);
  78.                         }
  79.  
  80.                         imagedestroy($image);
  81.                         imagedestroy($destination);
  82.                 }
  83.         } else {
  84.                 echo
  85.  
  86. Eh, er is denk ik iets fout met het uploaden gegaan, want dat bestand van jou staat er niet tussen! Is de map waar je het in wilde uploaden wel ge-chmod naar 777?
  87.  
  88. ;
  89.         }
  90. }
  91.  
  92. ?>

Jaapz Zonder rubriek

  1. Nog geen reacties.
  1. Nog geen trackbacks.