Php image upload code (supports watermarks and thumbnails ). This code can add a shark to the uploaded image and scale the image proportionally. For more information, see. This code can add a shark to the uploaded image and scale down the image proportionally. this code can add a shark to the uploaded image and scale the image proportionally. For more information, see.
This code can add a shark to the uploaded image and scale the image proportionally. For more information, see.
$ Uptypes = array ('image/jpg ', // list of uploaded File types
'Image/jpeg ',
'Image/png ',
'Image/pjpeg ',
'Image/GIF ',
'Image/bmp ',
'Image/x-png ');
$ Max_file_size = 5000000; // size limit of uploaded files, in bytes
$ Destination_folder = "upload/"; // file upload path
$ Watermark = 1; // whether to add a watermark. (1 indicates adding a watermark. otherwise, no watermark is added );
$ Watertype = 1; // watermark type (1 is text, 2 is image)
$ Waterposition = 1; // watermark position (1 indicates the lower left corner, 2 indicates the lower right corner, 3 indicates the upper left corner, 4 indicates the upper right corner, and 5 indicates the center );
$ Waterstring = "newphp. site. cz"; // watermark string
$ Waterimg = "xplore.gif"; // watermark image
$ Imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated );
$ Imgpreviewsize = 1/2; // Thumbnail ratio
?>
M4U BLOG-fywyj.cn
If ($ _ SERVER ['request _ method'] = 'post ')
{
If (! Is_uploaded_file ($ _ FILES ["upfile"] [tmp_name])
// Whether a file exists
{
Echo "the file does not exist! ";
Exit;
}
$ File = $ _ FILES ["upfile"];
If ($ max_file_size <$ file ["size"])
// Check the file size
{
Echo "the file is too large! ";
Exit;
}
If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
{
Echo "only Image files or Flash files can be uploaded! ";
Exit;
}
If (! File_exists ($ destination_folder ))
Mkdir ($ destination_folder );
$ Filename = $ file ["tmp_name"];
$ Image_size = getimagesize ($ filename );
$ Pinfo = pathinfo ($ file ["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 ($ filename, $ destination ))
{
Echo "an error occurred while moving the file! ";
Exit;
}
$ Pinfo = pathinfo ($ destination );
$ Fname = $ pinfo [basename];
Echo "uploaded successfully
File name: ". $ destination_folder. $ fname ."
";
Echo "width:". $ image_size [0];
Echo "length:". $ image_size [1];
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 );
$ 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 ("This type of file cannot be uploaded! ");
Exit;
}
Imagecopy ($ nimage, $ simage, 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: // add a watermark string
Imagestring ($ nimage, 2, 3, $ image_size [1]-15, $ waterstring, $ black );
Break;
Case 2: // add a watermark image
$ Simage1 = imagecreatefromgif ("xplore.gif ");
Imagecopy ($ nimage, $ simage1 );
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;
}
// Overwrite the original uploaded File
Imagedestroy ($ nimage );
Imagedestroy ($ simage );
}
If ($ imgpreview = 1)
{
Echo"
Image preview:
";
Echo "echo" alt = "image preview: r File name:". $ destination. "r Upload Time:" border = '0'> ";
}
}
?>
Bytes. This code can add sharks to the uploaded image and scale down the image...