Upload og resize, modificering af script
Hejsa,Jeg har dette script, der laver størrelsen af et billede om og uploader det. Det undetstøtter kun jpeg og png, jeg har så ændret det lige så den også godtager gif billeder. Problemet er bare at den ikke reseizer billedet. Nogle der kan se fejlen?
//Upload function START
$uploaddir = "/home/mytackle/public_html/pics/";
$maxsize = 150; // maximum bredde
if (isset($_POST['submit']) && $_POST['submit'] == "upfile") {
$source = $_FILES['upfile']['tmp_name'];
if (is_uploaded_file($source)) {
if (tjektype($source)) {
$dest = $uploaddir . $_FILES['upfile']['name'];
$dest = tjekfil($dest);
if (move_uploaded_file($source, $dest)) {
thumb($dest, $maxsize);
echo "Picture Uploaded";
echo "<p><img src='".$dest."' alt='thumbnail' /></p>";
} else {
echo "Picture not uploaded";
}
} else {
echo "Filetype not allowed. Contact the webmaster if you have a picture of the model";
}
} else {
echo "No picture selected";
}
}
function Thumb($sourcefile, $size) {
if (preg_match("/png$/i", $sourcefile)) {
$im = imageCreateFromPNG($sourcefile);
} elseif (preg_match("/jpe?g$/i", $sourcefile)) {
$im = imageCreateFromJPEG($sourcefile);
} elseif (preg_match("/gif$/i", $sourcefile)) {
$im = imageCreateFromGIF($sourcefile);
}
$source_x = imagesx($im);
$source_y = imagesy($im);
$delta = $size/$source_x;
$dest_x = round($source_x*$delta);
$dest_y = round($source_y*$delta);
$target_id = imagecreatetruecolor($dest_x, $dest_y);
imagecopyresampled($target_id, $im,0,0,0,0, $dest_x,$dest_y, $source_x,$source_y);
if (preg_match("/png$/i", $sourcefile)) {
imagePNG($target_id, $sourcefile);
} elseif (preg_match("/jpe?g$/i", $sourcefile)) {
imageJPEG($target_id, $sourcefile);
} elseif (preg_match("/gif?g$/i", $sourcefile)) {
imageGIF($target_id, $sourcefile);
}
}
function tjektype($source) {
if(preg_match("/[png|gif|jpe?g]$/i", $source)) {
return true;
}else{
return false;
}
}
function tjekfil($filnavn) {
$i = 0;
$arr = explode(".", $filnavn);
$ext = array_pop($arr);
$navn = implode(".", $arr);
while (is_file($filnavn)) {
$i++;
$filnavn = $navn . "." . sprintf("%06u", $i) . "." . $ext;
}
return $filnavn;
}
//UPLOAD function END
//UPLOAD START
$uploaddir = $_SERVER["DOCUMENT_ROOT"] ."/pics/";
$maxsize = 150; //pixels (width)
if (isset($_FILES["upfile"])) {
$source = $_FILES['upfile']['tmp_name'];
if (is_uploaded_file($source)) {
if (tjektype($_FILES['upfile']['type'])) {
$dest = $uploaddir . $_FILES['upfile']['name'];
$dest = tjekfil($dest);
if (move_uploaded_file($source, $dest)) {
thumb($dest,$maxsize);
echo '<font color=green>Picture uploaded </font>';
} else {
echo '<font color=red>Picture not uploaded. Contact the webmaster if you want to add a picture</font>';
}
} else {
echo '<font color=red>This type of file can not be uploaded</font>';
}
} else {
echo '<font color=red>No picture selected</font>';
}
}
$pic_name=basename($dest);
//Upload script END