Php upload, generate thumbnails, generate text watermarks and image watermarks_PHP tutorial

WBOY
Release: 2016-07-13 10:59:43
Original
915 people have browsed it

/*
'-##########################################-|
'| 程序设计:(绿竹居) |
'| MSN:[email]lzj_zhangjun@hotmail.com[/email] |
'| Email:cszjun@gmail.com |
'| 结合自己以前做的上传和生成缩略和改编一个
'| 老前辈的生成图片水印做成这个 |
'| 上传+生成缩略图+生成文字水印和图片水印 |
'| 可以直接引用 |
'-##########################################-|
*/
//审明图片文件夹
$bigfolder="img";
$smallfolder="smallimg";
//审明文件夹名称 以年月日来建议文件夹
$fdate=date("Ymd");
//echo $fdate;
//审明文件名称。以年月日时分秒命名
$fname=date("YmdHis");
$bigname="0724e_com_".$fname;
$smallname="0724e_com_".$fname."_s";
//echo $fname."
".$bigname."
".$smallname;
//确定大小文件夹的名称和路经
$bigaddrname=$bigfolder."/".$fdate."/".$bigname;
$smalladdrname=$smallfolder."/".$fdate."/".$smallname;
//审明小图片的高度和宽度
$RESIZEWIDTH=180;
$RESIZEHEIGHT=150;
//审明水印的文字或图片的地址及字体的地址
$imgaddr="img.jpg";    //图片水印
$fontname="www.0724e.com";  //文字水印
//充许上传的文件扩展名
$exit_name=array(".jpg",".gif",".png");
if (isset($_POST['Submit'])){
//上传部分----------------------------
//定议上传名称和上传错误
$upfile=$_FILES['image']['name'];
$uperror=$_FILES['image']['error'];
//最简表单验证

switch ($uperror) {
  case 1:
      die("

上传真过PHP设置充许的最大值点击返回
");
  break;
  case 2:
   die("
上传超过了表单充许的最大值点击返回
");
  break;
  case 3:
   die("
文件只有部分被上传了点击返回
");
     break;
  case 4:
   die("
文件不有被上传点击返回
");
     break;
}

//检测扩展是否是充许上传的文件类型
//取得文件扩展名
$exname=strrchr($upfile,".");
//判断取得扩展名是否在要求的扩展名内
if(!in_array($exname,$exit_name))
die("

文件扩展名不对。只允许上传jpg gif png的图片点击返回
");

//检测存放图片的目录和子目录是否存在,如果不存在则建目录和子目录,并给目录最大权限777 对LINUX或unix对WINDOWS没必要
//大图
if (!file_exists($bigfolder)){
  mkdir($bigfolder,0777);
  mkdir($bigfolder."/".$fdate,0777);
}else{
  if (!file_exists($bigfolder."/".$fdate)){
   mkdir($bigfolder."/".$fdate,0777);
  }
}
//缩略图
if (!file_exists($smallfolder)){
  mkdir($smallfolder,0777);
  mkdir($smallfolder."/".$fdate,0777);
}else{
  if (!file_exists($smallfolder."/".$fdate)){
  mkdir($smallfolder."/".$fdate,0777);
  }
}
//创建目录结束

//得到临时上传的文件
$upfiletmp=$_FILES['image']['tmp_name'];
//判断临时文件是否存在
if ($uperror==6)
die("

找不到临时上传文件,上传失败点击返回
");

//Watermark part============================================
//Start of watermark function============
/**$groundImage Background image, that is, the image that needs to be watermarked, currently only supports GIF, JPG, and PNG formats;
* $waterPos Watermark position, there are 10 states, 0 is a random position;
* 1 means the top is on the left, 2 means the top is in the center, 3 means the top is on the right;
* 4 means the middle is on the left, 5 means the middle is on the middle, 6 means the middle is on the right;
* 7 means the bottom is on the left, 8 means the bottom is in the middle, 9 means the bottom is on the right;
* $waterImage Image watermark, that is, the image used as a watermark, currently only supports GIF, JPG, and PNG formats;
* $waterText Text watermark, that is, text is used as a watermark. It supports ASCII code and does not support Chinese;
* $textFont Text size, value is 1, 2, 3, 4 or 5, default is 5;
* $textColor Text color, the value is a hexadecimal color value, the default is #FF0000 (red);
**/

function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="", $textFont=5,$textColor="#FF0000")
{
$isWaterImage = FALSE;
$formatMsg = "This file format is not supported yet. Please use image processing software to convert the image to GIF, JPG, or PNG format.";

//Read watermark file
If(!empty($waterImage) && file_exists($waterImage)) {
          $isWaterImage = TRUE;
          $water_info = getimagesize($waterImage);
                      $water_w                                                                                                                                                                                                   only                        $water_h                                                                                  
switch($water_info[2]) { //Get the format of the watermark image

               case 1:$water_im = imagecreatefromgif($waterImage);break;

               case 2:$water_im = imagecreatefromjpeg($waterImage);break;
               case 3:$water_im = imagecreatefrompng($waterImage);break;
                   default:die($formatMsg);
}
}

//Read background image

If(!empty($groundImage) && file_exists($groundImage)) {

          $ground_info = getimagesize($groundImage);
                    $ground_w                                                                                                                                                                                                           only  $ GROUND_H = $ GROUND_INFO [1]; // Give the background picture high

switch($ground_info[2]) { //Get the format of the background image
               case 1:$ground_im = imagecreatefromgif($groundImage);break;

               case 2:$ground_im = imagecreatefromjpeg($groundImage);break;

               case 3:$ground_im = imagecreatefrompng($groundImage);break;
                  default:die($formatMsg);
}
} else {
​​​​die("The picture that needs to be watermarked does not exist!");
}

//Watermark position
If($isWaterImage) { //Image watermark
          $w = $water_w;
          $h = $water_h;
          $label = "Picture";
} else { //Text watermark
$temp = imagettfbbox(ceil($textFont*2.5),0,"./cour.ttf",$waterText);//Get the range of text using TrueType font
          $w = $temp[2] - $temp[6];
          $h = $temp[3] - $temp[7];
          unset($temp);
          $label = "Text area";
}
If( ($ground_w<$w) || ($ground_h<$h) ) {
echo "The length or width of the image that needs to be watermarked is smaller than the watermark ".$label.", so the watermark cannot be generated!";
         return;
}
switch($waterPos) {
case 0://random
                $posX = rand(0,($ground_w - $w));
                $posY = rand(0,($ground_h - $h));
             break;
case 1://1 means the top is on the left
                $posX = 0;
                $posY = 0;
             break;
​​​​​case 2://2 is top-centered
               $posX = ($ground_w - $w) / 2;
                $posY = 0;
             break;
           case 3://3 is the top right
               $posX = $ground_w - $w;
               $posY = 0;
             break;
           case 4://4 is the center on the 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 is the center on the right
               $posX = $ground_w - $w;
              $posY = ($ground_h - $h) / 2;
             break;
case 7://7 means the bottom is on the left
                $posX = 0;
               $posY = $ground_h - $h;
             break;
          case 8://8 is the bottom center
               $posX = ($ground_w - $w) / 2;
               $posY = $ground_h - $h;
             break;
case 9://9 means the bottom is on the 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;    
    }

    //设定图像的混色模式
    imagealphablending($ground_im, true);

    if($isWaterImage) { //图片水印
        imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//拷贝水印到目标文件        
    } else {//文字水印
        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("水印文字颜色格式不正确!");
        }
        imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));        
    }

    //生成水印后的图片
    @unlink($groundImage);
    switch($ground_info[2]) {//取得背景图片的格式
        case 1:imagegif($ground_im,$groundImage);break;
        case 2:imagejpeg($ground_im,$groundImage);break;
        case 3:imagepng($ground_im,$groundImage);break;
        default:die($errorMsg);
    }

    //释放内存
    if(isset($water_info)) unset($water_info);
    if(isset($water_im)) imagedestroy($water_im);
    unset($ground_info);
    imagedestroy($ground_im);
}
//水印函数完


//存在则移动完在上传
$goodupfile=@move_uploaded_file($upfiletmp,$bigaddrname.$exname);
if (!$goodupfile){
die("

上传图片失败点击返回
");
}else{

//Text watermark
ImageWaterMark($bigaddrname.$exname,5,"",$fontname,5,"#FF0000");

//Picture watermark
//imageWaterMark($bigaddrname.$exname,5,$imgaddr);
//End watermark part====================================

//缩略图部分------------------------------------------------------------
//判断缩略图大小函数-----
    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");
}
}
//生成部分
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 "

";
}
//End of thumbnail------------------------------------------------ --------

}
?>





www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631890.htmlTechArticle?php /* '-################# ########################-| '| Programming: (Green Bamboo House) | '| MSN: [email]lzj_zhangjun@hotmail.com[/ email] | '| Email:cszjun@gmail.com | '| Combined from...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template