Hej alle sammen
Først og fremmest.
Jeg er nybegynder til php og selvlært udvikler, så hav tålmodighed.
Jeg er igang med at lave en php file uploader, men er stødt på et problem.
Uploaderen virker fint og kan ses her
http://www.mistake.at/test Når man uploader et billede fx med navn: "cowboy.jpg",
ender det i mappen "/content/index/" med filnavnet "26-07-11__17:34:51:cowboy.jpg"
Mit problem er at, de uploadede billedfiler bevarer deres oprindelige "navn".
Jeg ønsker at fil-navnet KUN skal være dato og tid og altså skille sig af med filens oprindelige navn.
Jeg ønsker, at således at overnævnte eksempel bliver:
"26-07-11__17:34:51:.jpg" i stedet for 26-07-11__17:34:51:cowboy.jpg"
Endvidere er der i koden defineret en $max_dimension for maksimal width og height på de uploadede billeder.. Er der mulighed for, at man kan lave en minimum værdi også?
jeg håber at nogen kan hjælpe
vh sebastian
<?php
function generate_resized_image(){
$max_dimension = 500; // Max new width or height, can not exceed this value.
$min_dimension = 80;
$dir = "content/index/"; // Directory to save resized image. (Include a trailing slash - /)
// Collect the post variables.
$postvars = array(
"image" => trim($_FILES["image"]["name"]),
"image_tmp" => $_FILES["image"]["tmp_name"],
"image_size" => (int)$_FILES["image"]["size"],
"image_max_width" => (int)$_POST["image_max_width"],
"image_max_height" => (int)$_POST["image_max_height"]
);
// Array of valid extensions.
$valid_exts = array("jpg","jpeg","gif","png");
// Select the extension from the file.
$ext = end(explode(".",strtolower(trim($_FILES["image"]["name"]))));
// Check not larger than 175kb.
if($postvars["image_size"] <= 3000000){
// Check is valid extension.
if(in_array($ext,$valid_exts)){
if($ext == "jpg" || $ext == "jpeg" ){
$image = imagecreatefromjpeg($postvars["image_tmp"]);
}
else if($ext == "gif"){
$image = imagecreatefromgif($postvars["image_tmp"]);
}
else if($ext == "png"){
$image = imagecreatefrompng($postvars["image_tmp"]);
}
// Grab the width and height of the image.
list($width,$height) = getimagesize($postvars["image_tmp"]);
// If the max width input is greater than max height we base the new image off of that, otherwise we
// use the max height input.
// We get the other dimension by multiplying the quotient of the new width or height divided by
// the old width or height.
if($postvars["image_max_width"] > $postvars["image_max_height"]){
if($postvars["image_max_width"] > $max_dimension){
$newwidth = $max_dimension;
} else {
$newwidth = $postvars["image_max_width"];
}
$newheight = ($newwidth / $width) * $height;
} else {
if($postvars["image_max_height"] > $max_dimension){
$newheight = $max_dimension;
} else {
$newheight = $postvars["image_max_height"];
}
$newwidth = ($newheight / $height) * $width;
}
// Create temporary image file.
$tmp = imagecreatetruecolor($newwidth,$newheight);
// Copy the image to one with the new width and height.
imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height);
// Create random 4 digit number for filename.
#$rand = rand(1000,9999);
$filename = $dir.date("d-m-y__H:i:s:", time()).$postvars["image"];
// Create image file with 100% quality.
imagejpeg($tmp,$filename,100);
header("Location: index.php");
echo "Thanks";
imagedestroy($image);
imagedestroy($tmp);
} else {
return "File size too large. Max allowed file size is 3 Mb.";
}
} else {
return "Invalid file type. You must upload an image file. (jpg, jpeg, gif, png).";
}
}
if(isset($_GET["do"])){
if($_GET["do"] == "upload"){
$upload_and_resize = generate_resized_image();
} else {
$upload_and_resize = "";
}
} else {
$upload_and_resize = "";
}
?>
Indlæg senest redigeret d. 27.07.2011 19:27 af Bruger #16714