php imagick 文字居中的方法

php imagick 文字居中的方法

  public function getwenzinfo($nickName)
    {

        $nickNamelen   = mb_strlen($nickName, 'utf-8');
        $wzallkd       = 0;
        $wzallkdlist   = array();
        $wzallkdlist[] = 0;
        $perzifulist   = array();
        for ($i = 0; $i < $nickNamelen; $i++) {
            $perzifu       = mb_substr($nickName, $i, 1, 'utf-8');
            $perzifulist[] = $perzifu;
            if (preg_match("/^[A-Za-z0-9]+$/", $perzifu) == false) {
                // echo '是中文';
                $wzkd = 26;
                $wzallkd += 26;
            } else {
                // echo '不是中文';
                $wzkd = 17;
                $wzallkd += 15;
            }
            $wzallkdlist[] = $wzkd;
        }
        /*因为字符串宽度是 字符本身宽度+字符之间的间隔-最后的字符串间隔*/
        $wzallkd=$wzallkd-2;
        array_pop($wzallkdlist);

        $wenziinfo['wzallkd']     = $wzallkd;
        $wenziinfo['wzallkdlist'] = $wzallkdlist;
        $wenziinfo['perzifulist'] = $perzifulist;

        return $wenziinfo;
    }

   $text = new \Imagick();
        $text->newImage(750, 1206, 'none');

        $text->setImageFormat('png');

        $draw1 = new \ImagickDraw();
        $draw1->setFont(__DIR__ . '/../../../Public/tianyangzhongqiu/ziti/fanzhengdahei.ttf'); //设置中文字体 可以解决中文乱码问题
        /*$draw->setTextAlignment(1);*///1  从文字的左下方开始计算的  2 从文字中下来计算  3 从文字右下来计算 默认为1
        $xingzuocolor = $this->getxingzuocolor($xingzuo);
        $draw1->setFillColor(new \ImagickPixel($xingzuocolor));
        $draw1->setFontSize(27);
        $draw1->setTextAlignment(1);
        $nickName  = '啦啊';
        $wenziinfo = $this->getwenzinfo($nickName);
      
        $wzallkd     = $wenziinfo['wzallkd'];
        $wzallkdlist = $wenziinfo['wzallkdlist'];
        $perzifulist = $wenziinfo['perzifulist'];

        $wenzizuobiao = (750 - $wzallkd) / 2;
        
        foreach ($wzallkdlist as $k => $v) {
            $wenzizuobiao += $v;
        
            $draw1->annotation($wenzizuobiao, 240, $perzifulist[$k]);
        }

        $draw1->annotation(167, 290, $chengji);

        $text->drawImage($draw1);

ps:就是先设置宽度 然后打字 就这样文字宽度我来定 就不会出错了

php 5.5使用 array_column的方法

php 5.5使用 array_column的方法
 public function array_column($input, $columnKey, $indexKey = null)
    {
        $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
        $indexKeyIsNull    = (is_null($indexKey)) ? true : false;
        $indexKeyIsNumber  = (is_numeric($indexKey)) ? true : false;
        $result            = array();
        foreach ((array) $input as $key => $row) {
            if ($columnKeyIsNumber) {
                $tmp = array_slice($row, $columnKey, 1);
                $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
            } else {
                $tmp = isset($row[$columnKey]) ? $row[$columnKey] : null;
            }
            if (!$indexKeyIsNull) {
                if ($indexKeyIsNumber) {
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && !empty($key)) ? current($key) : null;
                    $key = is_null($key) ? 0 : $key;
                } else {
                    $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }
            $result[$key] = $tmp;
        }
        return $result;
    }

php 开启微信公众号开发者模式

php 开启微信公众号开发者模式

<?php
/**
  * wechat php test
  */
header('Content-type:text');
//define your token
//定义TOKEN密钥
define("TOKEN", "weixin");
//实例化微信对象
$wechatObj = new wechatCallbackapiTest();
//验证成功后注释掉valid方法
$wechatObj->valid();
//开启自动回复功能
$wechatObj->responseMsg();
//定义类文件
class wechatCallbackapiTest
{
    //实现valid验证方法:实现对接微信公众平台
    public function valid()
    {
        //通过GET请求接收随机字符串
        $echoStr = $_GET["echostr"];
        //调用checkSignature方法进行用户(开发者)数字签名验证
        //valid signature , option
        if($this->checkSignature()){
            //如果成功,则返回接收到的随机字符串
            echo $echoStr;
            //并退出
            exit;
        }
    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        //接收用户端(客户)发送过来的XML数据
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

          //extract post data
        //判断XML数据是否为空
        if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
                //通过simplexml进行xml解析     PHP中有两大类可以完成对XML的解析,1.PHP的Dom模型2.通过simplexml模型
                  $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                //手机端
                $fromUsername = $postObj->FromUserName;
                //微信公众平台
                $toUsername = $postObj->ToUserName;
                //接收用户发送的关键词
                $keyword = trim($postObj->Content);
                //时间戳
                $time = time();
                //文本发送模板
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";   
                //判断用户发送关键词是否为空
                if(!empty( $keyword ))
                {
                    //回复类型,如果为"text",代表文本类型
                      $msgType = "text";
                    //回复内容
                    $contentStr = "Welcome to wechat world!";
                    //格式化字符串(对xml进行格式化操作,把里面相关的变量格式化成字符串)
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    //把XML数据返回给手机端
                    echo $resultStr;
                }
                //如果用户发送的关键词为空,则返回下列字符串
                else{
                    echo "Input something...";
                }

        }else {
            echo "";
            exit;
        }
    }
    
    //定义checkSignature方法
    private function checkSignature()
    {
        // you must define TOKEN by yourself
        
        //判断TOKEN密钥是否定义
        if (!defined("TOKEN")) {
            //如果没有定义则抛出异常,返回'TOKEN is not defined!'字符串
            throw new Exception('TOKEN is not defined!');
        }
        //接收微信加密签名
        $signature = $_GET["signature"];
        //接收时间戳信息
        $timestamp = $_GET["timestamp"];
        //接收随机数
        $nonce = $_GET["nonce"];
        //把TOKEN常量赋值给$token变量    
        $token = TOKEN;
        //把相关参数组装为数组(密钥文件、时间戳、随机数)
        $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
        //通过字典法进行排序
        sort($tmpArr, SORT_STRING);
        //把排序后的数组转化为字符串
        $tmpStr = implode( $tmpArr );
        //通过哈希算法对字符串进行加密操作
        $tmpStr = sha1( $tmpStr );
        //与加密签名进行对比
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>

1 TOKEN要跟微信公众号后台的TOKEN对应就行
2 $wechatObj->valid(); 微信公众号后台验证完就可以注释掉

php ffmpeg视频和序列帧转化

php ffmpeg视频和序列帧转化

$cmd=shell_exec("ffmpeg -i ".__DIR__ . "/shipin1.mp4 -r 25 -q:v 2   ".__DIR__ . "/testxulie/%03d.jpg");
        print_r($cmd);
        exit();
       $cmd = shell_exec("ffmpeg -i ".__DIR__ . "/testxulie/%03d.jpg -c:v copy -c:a copy  ".__DIR__ . "/testxulie/1.mp4");
        print_r($cmd);
        exit();

ps:-c:v copy -c:a这个是保证无损压缩的 -q:v 2保证无损导出图片 如果导出图片无损 一般图片很大 最终图片再拼成视频都要压缩

php imagick蒙版做法

php imagick蒙版做法

   $image = new \Imagick();
        $image->readImage(__DIR__ . '/mengban.png');
        $watermark = new \Imagick();
        $watermark->readImage(__DIR__ . '/testmengban.jpg');
        /*$watermark->scaleImage(750, 1206); *///放到等比例按照宽度缩小图片 按照
        $image->compositeImage($watermark, \imagick::COMPOSITE_ATOP, 0, 0);
        $image->writeImage(__DIR__ . '/mengbanresult.png');

ps:注意蒙版和最终生成的图片都要png

这个是imagick 其他一些的教程 http://www.netingcn.com/category/imagemagick