Home  >  Article  >  Backend Development  >  PHP实例:实现给上传图片加水印图案_PHP教程

PHP实例:实现给上传图片加水印图案_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:31:40761browse

  用php(做为现在的主流开发语言)给上传图片加水印的程序是通过判断文件类型建立图形,然后把其复制到原建立的图形上,填充并建立rectangle,以备写入imagestring()或是原已经定好的图像程序当中判断水印类型:一是字符串,另是增加一个图形对象在上面。如果你对php(做为现在的主流开发语言)的GD库比较熟悉,看懂这篇文章一点都不难了!

以下是引用片段:

/*****************************************************
参数说明:
$max_file_size  : 上传文件大小限制, 单位BYTE
$destination_folder : 上传文件路径
$watermark   : 是否附加水印(1为加水印,其他为不加水印);
使用说明:
1. 将php(做为现在的主流开发语言).INI文件里面的"extension=php(做为现在的主流开发语言)_gd2.dll"一行前面的;号去掉,因为我们要用到GD库;
2. 将extension_dir =改为你的php(做为现在的主流开发语言)_gd2.dll所在目录;
3. http://www.knowsky.com/php(做为现在的主流开发语言).asp;
****************************************************/
//上传文件类型列表
$uptypes=array(
    image/jpg, 
    image/jpeg,
    image/png,
    image/pjpeg,
    image/gif,
    image/bmp,
    image/x-png
);
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE
$destination_folder="uploadimg/"; //上传文件路径
$watermark=1;      //是否附加水印(1为加水印,其他为不加水印);
$watertype=1;      //水印类型(1为文字,2为图片)
$waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
$waterstring="http://www.xplore.cn/";  //水印字符串
$waterimg="xplore.gif";    //水印图片
$imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2;    //缩略图比例
?>


ZwelL图片上传程序




  上传文件:
 
 

  允许上传的文件类型为:=implode(, ,$uptypes)?>

(做为现在的主流开发语言)
if ($_SERVER[REQUEST_METHOD] == POST)
{
    if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
    //是否存在文件
    {
         echo "图片不存在!";
         exit;
    }
    $file = $_FILES["upfile"];
    if($max_file_size     //检查文件大小
    {
        echo "文件太大!";
        exit;
    }
    if(!in_array($file["type"], $uptypes))
    //检查文件类型
    {
        echo "文件类型不符!".$file["type"];
        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 "同名文件已经存在了";
        exit;
    }
    if(!move_uploaded_file ($filename, $destination))
    {
        echo "移动文件出错";
        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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/508919.htmlTechArticle用php (做为现在的主流开发语言) 给上传图片加水印的程序是通过判断文件类型建立图形,然后把其复制到原建立的图形上,填充并建立re...
Statement:
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