<?php
/*
'-##########################################-|
'| Program design: (Green Bamboo Residence) |
'| Msn:[email]lzj_zhangjun@hotmail.com[/email] |
'| email:cszjun@gmail.com |
'| Combine their previous upload and build abbreviations and adapt a
'| The old-timers create a picture watermark made this |
'| Upload + Generate thumbnails + Generate text watermark and image watermark |
'| can refer directly to |
'-##########################################-|
*/
Picture folder of the examination
$bigfolder="img";
$smallfolder="smallimg" ;
//The name of the folder in the year date suggested folder
$fdate=date("YMD" );
//echo $fdate;
The name of the document to be examined. Named after the month and the day
$fname=date("Ymdhis" );
$bigname="0724e_com_". $fname ;
$smallname="0724e_com_". $fname . "_s" ;
//echo $fname. " <br/> ". $bigname." <br/> ". $smallname;
Determine the name and path of the size folder
$bigaddrname=$bigfolder. "/" . $fdate . "/" . $bigname ;
$smalladdrname=$smallfolder. "/" . $fdate . "/" . $smallname ;
//The height and width of the small picture
$RESIZEWIDTH=180 ;
$RESIZEHEIGHT= ;
//The address of the printed text or picture and the address of the font
$imgaddr="img.jpg"; //Image watermark
$fontname="www.0724e.com"; Text watermark
Uploaded file extensions are allowed
$exit _name=array (". jpg",". gif",". png" );
if (Isset ($_post[' Submit ' )])){
//Upload part----------------------------
Fixed upload name and upload error
$upfile=$_files[' image '][' name ' ];
$uperror=$_files[' image '][' ERROR '] ];
//Most summary form verification
switch ($uperror ) {
case 1 :
Die ("<div align=" center "> Fax the maximum number of PHP settings allowed <a href=" javascript:history.back (); " > Click Back </a></div> " );
Break
Case 2 :
Die ("<div align=" center "> uploaded more than the maximum value of the form allowed <a href=" Javascript:history.back "();" > Click Back </a></div> " );
Break
Case 3 :
Die ("<div align=" center "> files are only partially uploaded <a href=" Javascript:history.back "();" > Click Back </a></div> " );
Break
Case 4 :
Die ("<div align=" center "> files are not uploaded <a href=" Javascript:history.back ";" > Click Back </a></div> " );
Break
}
//detect whether the extension is a file type that is allowed to upload
Get file name extension
$exname=strrchr($upfile,"." );
//Determine if the extension is within the required extension
if (! In_array ($exname,$exit _name ))
Die ("<div align=" center > file name extension is not correct.) Only allow uploading of jpg gif png pictures <a href= "javascript:history.back ();" > Click Back </a></div> " );
//detect the existence of directories and subdirectories that hold pictures, if no directories and subdirectories exist, and give the directory maximum permissions 777 for Linux or UNIX is not necessary for Windows
Big picture
if (! file_exists ($bigfolder )){
mkdir($bigfolder,0777 );
mkdir($bigfolder. "/" . $fdate , 0777 );
}else{
if (! file_exists ($bigfolder. "/" . $fdate )){
mkdir($bigfolder. "/" . $fdate , 0777 );
}
}
//thumbnail image
if (! file_exists ($smallfolder )){
mkdir($smallfolder,0777 );
mkdir($smallfolder. "/" . $fdate , 0777 );
}else{
if (! file_exists ($smallfolder. "/" . $fdate )){
mkdir($smallfolder. "/" . $fdate , 0777 );
}
}
//Create directory End
Get a temporary uploaded file
$upfiletmp=$_files[' image ']['tmp_ Name ' ];
//Determine if temporary files exist
if ($uperror= =6 )
Die ("<div align=" center "> Can't find temporary upload file, upload failed <a href=" Javascript:history.back "();" > Click Back </a></div> " );
Watermark Part ==========================================
Watermark function Starts ============
/** $groundImage background image, that is, the need to add a watermark picture, temporarily support gif,jpg,png format;
* $waterPos Watermark location, there are 10 kinds of states, 0 is random position;
* 1 for the top left, 2 for the top center, 3 for the top right;
* 4 for the middle of the left, 5 for the Middle center, 6 for the middle of the right;
* 7 for the bottom of the left, 8 for the bottom center, 9 for the bottom right;
* $waterImage image watermark, that is, as a watermark picture, temporarily support gif,jpg,png format;
* $waterText text watermark, that is, the text as a watermark, support ASCII code, does not support Chinese;
* $textFont text size, values are 1, 2, 3, 4, or 5, and the default is 5;
* $textColor The text color, the value is hexadecimal color value, the default is #ff0000 (red);
* */
functionImagewatermark($groundImage,$waterPos=0,$waterImage="",$waterText="", $textFont=5,$textColor="#FF0000")
{
$isWaterImage = FALSE ;
$formatMsg = "This file format is not supported, please use picture processing software to convert the picture to GIF, JPG, PNG format." ";
//Read watermark file
if (!empty ($waterImage) && file_exists( $waterImage )) {
$isWaterImage = TRUE ;
$water _info = getimagesize($waterImage );
$water _w = $water _info[0]; //Get the width of the watermark picture
$water _h = $water _info[1]; //Get the high watermark image
switch ($water _info[2]) { Get the format of a watermark picture
Case 1:$water _im = imagecreatefromgif($ Waterimage );
Case 2:$water _im = imagecreatefromjpeg( $waterImage );
Case 3:$water _im = imagecreatefrompng( $waterImage );
Default:die ($formatMsg );
}
}
//Read background picture
if (!empty ($groundImage) && file_exists($ Groundimage )) {
$ground _info = getimagesize($groundImage );
$ground _w = $ground _info[0]; //Get a wide background picture
$ground _h = $ground _info[1]; //Get a high background picture
switch ($ground _info[2]) { Get the formatting of the background picture
Case 1:$ground _im = imagecreatefromgif($ Groundimage );
Case 2:$ground _im = imagecreatefromjpeg( $groundImage );
Case 3:$ground _im = imagecreatefrompng( $groundImage );
Default:die ($formatMsg );
}
} else {
Die ("need to watermark the picture does not exist!") ");
}
//Watermark Location
if ($isWaterImage) { //Image watermark
$w = $water _w ;
$h = $water _h ;
$label = " picture's" ;
} else { //Text watermark
$temp = imagettfbbox(ceil($textFont *2.5),0,"./cour.ttf",$ Watertext); Get the range of text that uses TrueType fonts
$w = $temp[2]- $temp[6 ];
$h = $temp[3]- $temp[ 7 ];
unset ($temp );
$label = "text area" ;
}
if ($ground _w<$w) | | ($ground _h<$h ) ) {
echo "requires a watermark of the length or width of the picture than the watermark. " $label . "Still small, unable to generate watermark!" ";
Return
}
Switch ($waterPos ) {
Case 0: Random
$posX = rand(0,$ground _w - $w ));
$posY = rand(0,$ground _h - $h ));
Break
case 1: 1 for top left
$posX = 0 ;
$posY = 0 ;
Break
Case 2: //2 for top center
$posX = ($ground _w - $w)/ 2 ;
$posY = 0 ;
Break
Case 3: //3 to the top right
$posX = $ground _w - $w ;
$posY = 0 ;
Break
Case 4: //4 for central left
$posX = 0 ;
$posY = ($ground _h - $h)/ 2 ;
Break
case 5: 5 is centered in the middle
$posX = ($ground _w - $w)/ 2 ;
$posY = ($ground _h - $h)/ 2 ;
Break
Case 6: 6 for the middle right
$posX = $ground _w - $w ;
$posY = ($ground _h - $h)/ 2 ;
Break
Case 7: //7 to the bottom left
$posX = 0 ;
$posY = $ground _h - $h ;
Break
Case 8: //8 is centered at bottom
$posX = ($ground _w - $w)/ 2 ;
$posY = $ground _h - $h ;
Break
Case 9: //9 to the bottom right
$posX = $ground _w - $w ;
$posY = $ground _h - $h ;
Break
Default: Random
$posX = rand(0,$ground _w - $w ));
$posY = rand(0,$ground _h - $h ));
Break
}
Set the blending mode of the image
imagealphablending($ground _im, true );
if ($isWaterImage) { //Image watermark
imagecopy($ground _im, $water _im, $ Posx, $posY, 0, 0, $water _ W,$water _h); //copy watermark to target file
} else { Text watermark
if (!empty ($textColor) && (strlen($textColor ) =7 ) ) {
$R = hexdec(substr($textColor , 1 , 2 ));
$G = hexdec(substr($textColor , 3 , 2 ));
$B = hexdec(substr($textColor , 5 ));
} else {
Die ("watermark text color format is not correct!") ");
}
imagestring ( $ground _im, $textFont, $posX , $posY, $waterText, imagecolorallocate( $ground _im , $R , $G , $B ));
}
//Image after watermark is generated
@unlink($groundImage );
Switch ($ground _info[2]) { //Get the background picture format
Case 1:imagegif($ground _im,$ Groundimage );
Case 2:imagejpeg($ground _im,$ Groundimage );
Case 3:imagepng($ground _im,$ Groundimage );
Default:die ($errorMsg );
}
//Free memory
if (isset ($water _info)) unset ($water _info );
if (Isset ($water _im)) Imagedestroy($water _im );
unset ($ground _info );
Imagedestroy($ground _im );
}
//Watermark function complete
Existence is moving over the upload
$goodupfile=@move_uploaded_file($upfiletmp,$ Bigaddrname. $exname );
if (! $goodupfile ){
Die ("<div align=" center "> Upload image failed <a href=" Javascript:history.back (); " > Click Back </a></div> " );
}else{
Text watermark
imagewatermark($bigaddrname. $exname , 5 , "" , $fontname , 5 , "#FF0000" );
//Image watermark
Imagewatermark ($bigaddrname. $exname, 5, $IMGADDR);
End Watermark Part ===================================
Thumbnail section------------------------------------------------------------
Determine the thumbnail size function-----
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,$smalladdrname. $name . ". jpg" );
Imagedestroy ($newim );
}else{
imagejpeg ($im,$smalladdrname. $name . ". jpg" );
}
}
Build part
if ($_files[' image '][' size ']]){
if ($_files[' image '] [' type '] = = " Image/pjpeg " ){
$im = imagecreatefromjpeg($bigaddrname. $exname );
}elseif ($_files[' image '] ['type '] = = "Image/x-png" ){
$im = imagecreatefrompng($bigaddrname. $exname );
}elseif ($_files[' image '] ['type '] = = "Image/gif" ){
$im = imagecreatefromgif($bigaddrname. $exname );
}
if ($im ){
if (file_exists($smalladdrname. ". jpg" )){
unlink($smalladdrname. ". jpg" );
}
resizeimage($im,$RESIZEWIDTH,$ Resizeheight,$smalladdrname );
Imagedestroy ($im );
}
}
echo "<div align= ' center ' ><a href= ' javascript:window.history.back () ' > Upload success </a></div > " ;
}
//thumbnail end-----------------------------------------------------
}
<form action= " method=" post " enctype=" multipart/ Form-data " name=" Form1 " id=" Form1 ""
<input type= "file" name= "image" />
<input type= "hidden name=" max_file_size " value=" <?=1024 * 100?> <input type= "Submit" name= "Submit" value= "Upload picture" />
</form>