照片水印

<?php  
//set_time_limit(0);//0不限制

error_reporting(E_ALL ^ E_NOTICE);

/**
 * 照片水印 
 * 将拍摄时间作为水印打在照片,仅支持jpg
 *
 */
class photoWaterMark{  

    var $sourceDir;// 源照片目录
    var $storeDir;// 存储目录
    var $text;  //要在图片里显示的内容
    var $font = 'C:\Windows\Fonts\arial.ttf'; //字体文件位置
    var $angle = 0;  //字体输出角度
    var $size = 100; //字体大小
    var $showX = 100; //输出位置x坐标 
    var $showY = 200; //输出位置y坐标
      
    
    /**
     * 整理文字
     * @param  [type] $instring text
     * @return [type]           [description]
     */
    function createText($instring) {  
        $outstring = "";  
        $max = strlen ( $instring );  
        for($i = 0; $i < $max; $i ++) {  
            $h = ord ( $instring [$i] );  
            if ($h >= 160 && $i < $max - 1) {  
                $outstring .= substr ( $instring, $i, 2 );  
                $i ++;  
            } else {  
                $outstring .= $instring [$i];  
            }  
        }  
        return $outstring;  
    }  

    /**
     * 移动文件
     * @param  [type] $fromFile 源文件
     * @param  [type] $toDir    目标目录
     * @return [type]           [description]
     */
    function mvFile($fromFile,$toDir){
        $fileName = strrchr($fromFile, '/');
        $toFile = $toDir.$fileName;
        rename($fromFile,$toFile);
    }


    /**
     * 遍历目录下的文件
     * @param  [type] $path 目录
     * @return [type]       [description]
     */
    function iteralDir($path){
        $filearr = array();
        foreach (glob($path.'\*') as $file){
            if(is_dir($file)){
                $filearr = array_merge($filearr,iteral($file));
            }else{
                $filearr[] = str_replace('\\','/',$file);
            }
        }
        return $filearr;
    }

    /**
     * 给图片打水印
     *
     * @param  [array] $imagePath 图片路径
     * 
     * @return [type] [description]
     */
    function waterMark($imagePath){
        //输出头内容  
        //header('Content-type: image/jpg');

        //建立图象  
        $imageSize = getimagesize($imagePath);
        if(strpos($imageSize['mime'],'png')){//png 移动操作
            $this->mvFile($imagePath,$this->storeDir);
        }else{
            $image = imagecreatefromjpeg ($imagePath); 
        }
         
        $imageExif = exif_read_data($imagePath);

        $imageInfo = [
            'DateTimeOriginal' => $imageExif['DateTimeOriginal'],
            'FileDateTime' => date('Y:m:d H:i:s',$imageExif['FileDateTime']),   
            'DateTime' => $imageExif['DateTime'],   
            'width'=>$imageSize[0],
            'height'=>$imageSize[1]
        ];

        //text
        foreach (['DateTimeOriginal','FileDateTime','DateTime'] as $key) {
            if($imageExif[$key]){
                $this->text = $imageInfo[$key];
                break;
            }
        }

        if(!$this->text){//移动操作
            $this->mvFile($imagePath,$this->storeDir);
        }else{
            $this->text = date("Y/m/d H:i:s",strtotime($this->text));
        }

        //height width
        $length = $imageInfo['width'] > $imageInfo['height'] ? $imageInfo['height'] : $imageInfo['width'];
        $offset = round($length/30);
        $this->size = $offset;
        $this->showX = $offset-($offset/2);
        $this->showY = $offset+($offset/2);

        

        //定义颜色  
        //$color = imagecolorallocate($image, 255, 0, 0);//red
        //$color = imagecolorallocate ( $image, 255, 255, 255 );//white  
        //$color = imagecolorallocate ( $image, 0, 0, 0 );//black  
        
        $color = imagecolorallocate($image, 255,255,0);//yellow
        //显示文字  
        $txt = $this->createText ( $this->text );  
        
        //写入文字  
        imagettftext ( $image, $this->size, $this->angle, $this->showX, $this->showY, $color, $this->font, $txt );  
        
        //ImageString($image,5,50,10,$txt,$white);  
        //显示图形  
        $fileName = strrchr($imagePath, '/');
        $toFile = $this->storeDir.$fileName;
        imagejpeg ( $image ,$toFile);  
        //imagegif ( $image, "a2.jpg" );  
        ImageDestroy ( $image );
        unlink($imagePath);//删除图片
    }


    /**
     * 执行
     * @param  [array] $params [
     * sourceDir 源照片目录
     * storeDir 存储目录
     * ]
     * @return [type]         [description]
     */
    function execute($params) {  
        $this->sourceDir = $params['sourceDir'];
        $this->storeDir = $params['storeDir'];

        $sourcePhotos = $this->iteralDir($this->sourceDir);

        if(empty($sourcePhotos)){
            exit("empty dir");
        }
        foreach ($sourcePhotos as $imagePath) {
            $this->waterMark($imagePath);
        }

          
    }  
}  

  
//使用  
$photoWaterMark = new photoWaterMark();
$photoWaterMark->execute(['sourceDir'=>'./photo/2017-09-11/zsc','storeDir'=>'./photo/newphoto/zsc']);

?> 
照片水印 - Stars - The future is in my
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值