<?php
//http://www.eksperten.dk/spm/808877
//SETTINGS
$uploadDir = 'upload/'; //Bibliotek, hvor filer uploades til - husk chmod 777
$allowedFileTypes = array('jpg','gif','png'); //Hvilke filer vil vi acceptere bliver uploadet?
//THUMBS SETTINGS
$thumbDir = 'upload/'; //Bibliotek, hvor thumbs gemmes - husk chmod 777
$thumbPrefix1 = ''; //Hvad skal tilføjes til originalt filnavn?
$thumbPrefix2 = '-forside'; //Hvad skal tilføjes til originalt filnavn?
$thumbPrefix3 = '-thumb'; //Hvad skal tilføjes til originalt filnavn?
$thumbHeight1 = 389; //Thumb højde
$thumbWidth1 = 550; //Thumb bredde
$thumbHeight2 = 305; //Thumb højde
$thumbWidth2 = 431; //Thumb bredde
$thumbHeight3 = 194; //Thumb højde
$thumbWidth3 = 274; //Thumb bredde
//Funktioner som bruges:
function getExtension($filename){
return strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
}
function resize( $filename, $newfilename, $maxw, $maxh, $quality=85 )
{
$extension = getExtension($filename);
switch($extension)
{
case 'jpeg':
case 'jpe':
case 'jpg':
$srcim = imagecreatefromjpeg( $filename );
break;
case 'gif':
$srcim = imagecreatefromgif( $filename );
break;
case 'png':
$srcim = imagecreatefrompng( $filename );
break;
default:
return false;
}
$ow = imagesx( $srcim );
$oh = imagesy( $srcim );
$wscale = $maxw / $ow;
$hscale = $maxh / $oh;
$scale = min( $hscale, $wscale );
$nw = round( $ow * $scale, 0 );
$nh = round( $oh * $scale, 0 );
$dstim = imagecreatetruecolor( $nw, $nh );
imagecopyresampled( $dstim, $srcim, 0, 0, 0, 0, $nw, $nh, $ow, $oh );
switch($extension)
{
case 'jpeg':
case 'jpe':
case 'jpg':
imagejpeg( $dstim, $newfilename, $quality );
break;
case 'gif':
imagegif( $dstim, $newfilename );
break;
case 'png':
$png_q = floor( abs( $quality / 10 - 9.9 ) );
imagepng( $dstim, $newfilename, $png_q );
break;
default:
return false;
}
imagedestroy( $dstim );
imagedestroy( $srcim );
return file_exists($newfilename);
}
//Er der trykket på Upload file knappen?
if(isset($_POST['myUpload']))
{
//Har vi en fil?
if(is_uploaded_file($_FILES['myFile']['tmp_name']))
{
//Filendelse
$extension = getExtension($_FILES['myFile']['name']);
//Er filen en af de filer vi gerne vil have?
if(in_array($extension,$allowedFileTypes))
{
//Vi flytter filen fra tmp til vores UPLOADDIR
if(move_uploaded_file($_FILES['myFile']['tmp_name'],$uploadDir . basename($_FILES['myFile']['name'])))
{
$resize1 = resize($uploadDir . basename($_FILES['myFile']['name']), $thumbDir . $thumbPrefix1 . basename($_FILES['myFile']['name']), $thumbWidth1, $thumbHeight1);
$resize2 = resize($uploadDir . basename($_FILES['myFile']['name']), $thumbDir . $thumbPrefix2 . basename($_FILES['myFile']['name']), $thumbWidth2, $thumbHeight2);
$resize3 = resize($uploadDir . basename($_FILES['myFile']['name']), $thumbDir . $thumbPrefix3 . basename($_FILES['myFile']['name']), $thumbWidth3, $thumbHeight3);
if($resize1 && $resize2 && $resize3){
echo '<h3 style="color:green;">File ('.basename($_FILES['myFile']['name']).') uploaded and resized succesfully</h3>';
echo '<h5>Thumb1</h5><img src="'.$thumbDir . $thumbPrefix1 . basename($_FILES['myFile']['name']).'" alt="thumb1" /><br />';
echo '<h5>Thumb2</h5><img src="'.$thumbDir . $thumbPrefix2 . basename($_FILES['myFile']['name']).'" alt="thumb2" /><br />';
echo '<h5>Thumb3</h5><img src="'.$thumbDir . $thumbPrefix3 . basename($_FILES['myFile']['name']).'" alt="thumb3" /><br />';
}else{
echo '<h3 style="color:red;">File ('.basename($_FILES['myFile']['name']).') uploaded succesfully, but resize failed</h3>';
}
}
else
{
echo '<h3 style="color:red;">Error - unable to upload file: '.basename($_FILES['myFile']['name']).'</h3>';
}
}
else
{
echo '<h3 style="color:red;">Wrong filetype, allowed filetypes are: ('.implode(',',$allowedFileTypes).')</h3>';
}
}
else
{
echo '<h3 style="color:red;">No file selected<h3>';
}
}
//Vores form
echo '<form action="808877exp.php" method="post" enctype="multipart/form-data">';
echo 'File ('.implode(',',$allowedFileTypes).'): <input type="file" name="myFile"><br />';
echo '<input type="submit" name="myUpload" value="Upload file">';
echo '</form>';
?>
Og du kan se koden i funktion her:
http://www.codebreaker.dk/exp/808877exp.php