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.
-
<?php
-
-
/**
-
* JaapzUpload
-
* Een simpel uploadscript met schaal functie gemaakt door:
-
* Jaap Broekhuizen a.k.a. Jaapz
-
* Site: www.jaapz.nl
-
* Mail: jaapz.b[apenstaartje]gmail[punt]com
-
* Vrijgegeven onder de GNU GPL2 licentie
-
*/
-
-
// Variabelen
-
$verzend = $_POST[‘verzend’];
-
$resize = $_POST[‘resize’];
-
$width = $_POST[‘width’];
-
$map = ‘../uploads/’;
-
-
// Tekst echoen
-
echo ‘
-
<h1>Bestand Uploaden</h1>
-
-
‘;
-
-
// Extensies die mogen upgeload worden
-
‘image/gif’,
-
‘image/png’);
-
-
// Is er al wat upgeload?
-
?>
-
<form method="post" target="_self" enctype="multipart/form-data" action="upload.php">
-
Selecteer een bestand:
-
<input name="bestand" type="file">
-
<input type="checkbox" name="resize" onclick="if(this.checked){document.getElementById(’resize’).style.display=’block’}else{document.getElementById(’resize’).style.display=’none’}" /> Afbeelding schalen?
-
<div id="resize" style="display:none">
-
Breedte:
-
<input type="text" name="width" />px
-
</div>
-
<input type="submit" value="Uploaden!" name="verzend" />
-
</form>
-
-
<?php
-
} else { // Ja
-
die(‘Dit type bestand mag niet geupload worden!<a href="javascript:history.back(\’1\’)">Ga terug</a>’);
-
}
-
-
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>’;
-
-
if ($resize == ‘on’) {
-
$helebestand = $map . $_FILES[‘bestand’][‘name’];
-
-
$bronbreedte = $dimensions[0];
-
$bronhoogte = $dimensions[1];
-
-
$doelhoogte = ($bronhoogte * $width) / $bronbreedte;
-
-
$image = imagecreatefromgif($helebestand);
-
$destination = imagecreatetruecolor($width, $doelhoogte);
-
imagecopyresampled($destination, $image, 0, 0, 0, 0, $width, $doelhoogte, $bronbreedte, $bronhoogte);
-
imagegif($destination, $helebestand);
-
$image = imagecreatefrompng($helebestand);
-
$destination = imagecreatetruecolor($width, $doelhoogte);
-
imagecopyresampled($destination, $image, 0, 0, 0, 0, $width, $doelhoogte, $bronbreedte, $bronhoogte);
-
imagepng($destination, $helebestand);
-
} else {
-
$image = imagecreatefromjpeg($helebestand);
-
$destination = imagecreatetruecolor($width, $doelhoogte);
-
imagecopyresampled($destination, $image, 0, 0, 0, 0, $width, $doelhoogte, $bronbreedte, $bronhoogte);
-
imagejpeg($destination, $helebestand);
-
}
-
-
imagedestroy($image);
-
imagedestroy($destination);
-
}
-
} else {
-
echo ‘
-
-
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?
-
-
‘;
-
}
-
}
-
-
?>