Yesterday the company suddenly wanted an upload file with attachments, but also a picture with the upload function and to generate thumbnail effect, I will post my code.
First look at the up.htm file, the code is as follows: <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>
<body>
<form action= "s.php" method= "post" enctype= "Multipart/form-data" Name= "Form1" Id= "Form1" >
<input type= "file" name= "image" id= "image"/>
<label>
<input type= "Submit" name= "button" id= "button" value= "submitted"/>
</label>
</form>
<label></label>
</body>
In fact, the previous page is a simple HTM file, post the information to our next file s.php
if ($_files[' image '] [' name ']) {
if ($_files[' image '] [' size ']) {
if ($_files[' image '] [' type '] = = "Image/pjpeg") {
$im = @imagecreatefromjpeg ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. jpg ';
}elseif ($_files[' image '] [' type '] = = "Image/x-png") {
$im = @imagecreatefrompng ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. png ';
}elseif ($_files[' image '] [' type '] = = "Image/gif") {
$im = @imagecreatefromgif ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. gif ';
}
}
Resizeimage ($im, 120,60,MD5 (Date ("y-m-d h:i:s"));
Imagedestroy ($im);
$tag = 1;
}
function Resizeimage ($im, $maxwidth, $maxheight, $name) {
$width = Imagesx ($im);
$height = Imagesy ($im);
if ($maxwidth && $width > $maxwidth) | | ($maxheight && $height > $maxheight)) {
if ($maxwidth && $width > $maxwidth) {
$widthratio = $maxwidth/$width;
$RESIZEWIDTH =true;
}
if ($maxheight && $height > $maxheight) {
$heightratio = $maxheight/$height;
$RESIZEHEIGHT =true;
}
if ($RESIZEWIDTH && $RESIZEHEIGHT) {
if ($widthratio < $heightratio) {
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif ($RESIZEWIDTH) {
$ratio = $widthratio;
}elseif ($RESIZEHEIGHT) {
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if (function_exists ("imagecopyresampled")) {
$newim = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = Imagecreate ($newwidth, $newheight);
Imagecopyresized ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
Imagejpeg ($newim, $name. ". jpg");
Imagedestroy ($newim);
}else{
Imagejpeg ($im, $name. ". jpg");
}
}
It is so simple, on the page this is to generate files to the directory, there is a binary on the Web page to display, of course, you can also save to the database. And then read out.