/*
* Parameter description * $max_file_size: Upload file size limit, unit BYTE * $destination_folder: Upload file path * $watermark: Whether to attach a watermark ( 1 means adding watermark, others means not adding watermark); * http://bbs.it-home.org * Instructions for use: * 1. In front of the line "extension=php_gd2.dll" in the PHP.INI file Remove the ; sign because we need to use the GD library; * 2. Change extension_dir = to the directory where your php_gd2.dll is located; */ // Upload file type list $uptypes = array ( 'image/ jpg', 'image/png', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png' ); $max_file_size = 20000000; //Upload file size limit, unit BYTE $destination_folder = 'uploadimg/'; //Upload file path $watermark = 1; //Whether to attach a watermark (1 means adding a watermark, others means not adding a watermark ); $watertype = 1; //Watermark type (1 is text, 2 is picture) $waterposition = 1; //Watermark position (1 is the lower left corner, 2 is the lower right corner, 3 is the upper left corner, 4 is the upper right corner corner, 5 is centered); $waterstring = "http://bbs.it-home.org/"; //Watermark string $waterimg = "xplore.gif"; //Watermark image $imgpreview = 1 ; //Whether to generate a preview image (1 means generate, others do not generate); $imgpreviewsize = 1 / 2; //Thumbnail ratio ?>
ZwelL picture upload program
Upload file:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//Determine whether there is an uploaded file
if (is_uploaded_file($_FILES['upfile']['tmp_name']) ) {
$upfile = $_FILES['upfile'];
print_r($_FILES['upfile']);
$name = $upfilep['name']; //File name
$type = $upfile[' type']; //File type
$size = $upfile['size']; //File size
$tmp_name = $upfile['tmp_name']; //Temporary file
$error = $upfile['error' ]; //Cause of error
if ($max_file_size < $size) { //Determine the size of the file
echo 'The uploaded file is too large'; exit (); }< /p>
if (!in_arrar($type, $uptypes)) { //Determine the file type
echo 'Uploaded file type does not match' . $type;
exit ();
}
if (!file_exists($destination_folder)) {
mkdir($destination_folder);
}
if (file_exists("upload/" . $_FILES["file" ]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
} p>
$pinfo = pathinfo($name);
$ftype = $pinfo['extension'];
$destination = $destination_folder . time() . "." . $ftype;
if (file_exists ($destination) && $overwrite != true) {
echo "The file with the same name already exists";
exit ();
}
if (!move_uploaded_file($tmp_name, $destination )) {
echo "Error moving file";
exit ();
}
$pinfo = pathinfo($destination);
$fname = $pinfo[basename];
echo " 已经成功上传 文件名: " . $destination_folder . $fname . " ";
echo " 宽度:" . $image_size[0];
echo " 长度:" . $image_size[1];
echo " 大小:" . $file["size"] . " bytes";
if ($watermark == 1) {
$iinfo = getimagesize($destination, $iinfo);
$nimage = imagecreatetruecolor($image_size[0], $image_size[1]);
$white = imagecolorallocate($nimage, 255, 255, 255);
$black = imagecolorallocate($nimage, 0, 0, 0);
$red = imagecolorallocate($nimage, 255, 0, 0);
imagefill($nimage, 0, 0, $white);
switch ($iinfo[2]) {
case 1 :
$simage = imagecreatefromgif($destination);
break;
case 2 :
$simage = imagecreatefromjpeg($destination);
break;
case 3 :
$simage = imagecreatefrompng($destination);
break;
case 6 :
$simage = imagecreatefromwbmp($destination);
break;
default :
die("不支持的文件类型");
exit;
}
imagecopy($nimage, $simage, 0, 0, 0, 0, $image_size[0], $image_size[1]);
imagefilledrectangle($nimage, 1, $image_size[1] - 15, 80, $image_size[1], $white);
switch ($watertype) {
case 1 : //加水印字符串
imagestring($nimage, 2, 3, $image_size[1] - 15, $waterstring, $black);
break;
case 2 : //加水印图片
$simage1 = imagecreatefromgif("xplore.gif");
imagecopy($nimage, $simage1, 0, 0, 0, 0, 85, 15);
imagedestroy($simage1);
break;
}
switch ($iinfo[2]) {
case 1 :
//imagegif($nimage, $destination);
imagejpeg($nimage, $destination);
break;
case 2 :
imagejpeg($nimage, $destination);
break;
case 3 :
imagepng($nimage, $destination);
break;
case 6 :
imagewbmp($nimage, $destination);
//imagejpeg($nimage, $destination);
break;
}
//覆盖原上传文件
imagedestroy($nimage);
imagedestroy($simage);
}
if ($imgpreview == 1) {
echo " 图片预览: ";
echo " ";
}
}
}
?>