base64图片数据上传,php接收并保存 PHP

前端使用canvas 生成html页面数据,返回Base64 图片数据 php后台接收并保存

下面直接贴代码

项目框架 thinkphp3.2

pubilc function create_img(){

    //传输 base64图片数据 及 保存的路径
    $res=$this->base64_image_content($_POST['base64'],'data/ts_create');
}

//base64图片转换
function base64_image_content($base64_image_content,$path){
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
        $type = $result[2];
        $new_file = $path."/".date('Ymd',time())."/";
        if(!file_exists($new_file)){
            //检查是否有该文件夹,如果没有就创建,并给予最高权限
            mkdir($new_file, 0700);
        }
        $new_file = $new_file.time().".{$type}";
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
            return '/'.$new_file;
        }else{
            return 1;
        }
    }else{
        return 2;
    }
}

杨佳乐 发布于  2024-4-30 11:05 

PHP日期和时间处理组件-Carbon PHP

安装
可以通过 Composer 来安装 Carbon:

composer require nesbot/carbon
Composer的安装和中国镜像的使用请参照:http://www.phpcomposer.com/

使用
我们通过命名空间导入 Carbon 来使用,而不需每次都提供完整的名称。

<?php
require 'vendor/autoload.php';

use Carbon\Carbon;
Carbon提供了许多处理日期和时间的方法,可以满足各种业务需求,以下列出我们常用的处理日期和时间的方法:

1.获取当前时间

<?php
$dt = Carbon::now();
$dt->timezone = 'Asia/Shanghai';
echo $dt; // 2017-10-10 13:02:09
2.昨天、今天、明天

<?php
$dt = Carbon::now();
$dt->timezone = 'Asia/Shanghai';
echo '昨天: '.$dt->yesterday(); // 昨天: 2017-10-09 00:00:00
echo '今天: '.$dt->today(); // 今天: 2017-10-10 00:00:00
echo '明天: '.$dt->tomorrow(); // 明天: 2017-10-11 00:00:00
3.2017年10月最后一天

<?php
echo new Carbon('last day of October 2017'); // 2017-10-31 00:00:00
4.一周前

<?php
echo $dt->subWeek(); // 2017-10-03 13:02:09
5.30天后

<?php
$dt = Carbon::now();
$dt->timezone = 'Asia/Shanghai';
echo $dt->addDay(30); // 2017-11-09 13:02:09
6.计算年龄(出生于1982-12-12)

<?php
$howOldAmI = Carbon::createFromDate(1982, 12, 12)->age;
echo $howOldAmI . '岁'; // 34岁
7.只显示日期

<?php
echo Carbon::now()->toDateString(); // 2017-10-10
8.显示国际时间

<?php
echo Carbon::now('Europe/London'); // 伦敦时间:2017-10-10 06:02:09
echo Carbon::now('Europe/Paris'); // 巴黎时间:2017-10-10 07:02:09
echo Carbon::now('Asia/Shanghai'); // 北京时间:2017-10-10 13:02:09
echo Carbon::now('America/Vancouver'); // 温哥华时间:2017-10-09 22:02:09
9.2小时后

<?php
echo Carbon::now('Asia/Shanghai')->addHours(2); // 2017-10-10 15:02:09
10.显示当前时间戳

<?php
echo Carbon::now()->timestamp; // 1507611729
11.人性化显示时间

<?php
$ts = Carbon::yesterday()->timestamp;
echo Carbon::createFromTimestamp($ts)->diffForHumans(); // 1天前

Carbon有点类似javascript的moment.js,它继承了PHP的 Datetime 类,所以 Carbon 中没有涉及到的,但在 Datetime 中已经实现的方法都是可以使用的。使用Carbon后你会发现处理日期和时间非常灵活,也非常人性化。

更多有关Carbon的用法请参照Carbon在github上的地址: http://carbon.nesbot.com/docs/


杨佳乐 发布于  2024-4-30 11:05 

PHP simple_html_dom 使用记录 PHP

 官方文档使用file_get_html乱码问题处理
              先使用file_get_contents获取页面内容然后转换编码,再使用str_get_html
              mb_convert_encoding($content,'UTF-8',"auto");

        //保存图片路径
        $path=IA_ROOT."/images/";
        // Fetch the html content
        $url = "要抓取的微信文章网址";
        $content = file_get_contents($url);

        $url_info = explode('/', trim($url, '/'));
        $url_info = array_reverse($url_info);
        $page_name = $url_info[0] . '.html';

        // Fetch the images of the page

        //引入simple_html_dom文件 下载地址,下方提供

        require "simple_html_dom.php";

        $html = str_get_html($content);

        // Fetch the real path of the imgaes and download them
        $images = $html->find('img');
        foreach ($images as $index => $image) {
            $image_url = $image->getAttribute('data-src');
            if ($image_url) {
                $image_file = file_get_contents($image_url);
                $url_info = explode('/', trim($image_url, '/'));
                $url_info = array_reverse($url_info);
                $file_name = $url_info[1] . '.jpg';
                $new_image_url = 'images' . DIRECTORY_SEPARATOR . $file_name;
                file_put_contents($path.$file_name, $image_file);
                //设置src 属性内容
                $html->find('img', $index)->setAttribute('src',"/attachment/wechat_article_image/".$file_name);

            }
        }

        // Ftech the real url of the iframe
        $iframes = $html->find('iframe');
        foreach ($iframes as $index => $iframe) {
            $iframe_url = $iframe->getAttribute('data-src');
            // $iframe_w                            = $iframe->getAttribute('data-w');
            $html->find('iframe', $index)->src = $iframe_url;
            // TODO need to calculate the width and height of the video

        }

        //获取div id为 js_content内容
        $res=$html->find('div[id=js_content]',0);
        echo $res;die;
        $doc=$html;
        //修改后的内容
        echo $doc;die;

杨佳乐 发布于  2024-4-30 11:04 

记录一个php导出csv方法 PHP

 public function export_excel(){

       //查询列表传入即可

        $html = $this->wxapp_house_export_parse_zan($list);
        header("Content-type:text/csv");
        header("Content-Disposition:attachment; filename=点赞信息.csv");
        echo $html;
        exit();

    }

    //改善后
        function export_jslog($list) {
            if (empty($list)) {
                return false;
            }
            $header = array(
                'id'=>'id',
                'openid' => 'openid',
                'nickname' => '用户名称',
                'mobile'=>'手机号',
                'province'=>'省',
                'city'=>'市',
                'area'=>'区',
                'mj'=>'面积',
                'shi'=>'室',
                'ting'=>'厅',
                'chu'=>'厨',
                'wei'=>'卫',
                'addtime'=>'计算时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";

            foreach ($list as $row) {

                $fans=$this->get_fansinfo($row['openid']);
                $row['nickname']=$fans['nickname'];
                $row['addtime']=date('Y-m-d H:i:s',$row['addtime']);
                foreach ($keys as $key) {
                    $data[] = $row[$key];
                }
                $user[] = implode("\t ,", $data) . "\t ,";
                unset($data);
            }

            $html .= implode("\n", $user) . "\n";

            return $html;
        }

    function wxapp_house_export_parse_zan($cards) {
        if (empty($cards)) {
            return false;
        }
        $header = array(
            'openid' => 'openid',
            'name' => '用户名称','phone'=>'手机号','dw'=>'单位','zw'=>'职务','address'=>'地址'
        );
        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
        $count = count($cards);
        $pagesize = ceil($count / 5000);
        for ($j = 1; $j <= $pagesize; $j++) {
            $list = array_slice($cards, ($j - 1) * 5000, 5000);
            if (!empty($list)) {
                $size = ceil(count($list) / 500);
                for ($i = 0; $i < $size; $i++) {
                    $buffer = array_slice($list, $i * 500, 500);
                    $user = array();
                    foreach ($buffer as $row) {
                        $area = pdo_fetchcolumn("SELECT name FROM " . tablename('amouse_wxapp_category') . " WHERE id=:id  limit 1", array(":id" => $row['categoryId']));
                        $row['categoryId'] = $area;
                        $row['createtime'] = date('Y-m-d H:i:s', $row['createtime']);
                        foreach ($keys as $key) {
                            $data[] = $row[$key];
                        }
                        $user[] = implode("\t ,", $data) . "\t ,";
                        unset($data);
                    }
                    $html .= implode("\n", $user) . "\n";
                }
            }
        }
        return $html;
    }

杨佳乐 发布于  2024-4-30 11:03 

PHP gd库写入文字居中 PHP

function mergeText_mobile($target, $data, $text)
{
    $font = IA_ROOT . "/web/resource/fonts/msyhbd.ttf";
    $colors = hex2rgb($data["color"]);

    $color = imagecolorallocate($target, $colors["red"], $colors["green"], $colors["blue"]);

    $res=imagettfbbox(16,0,$font,$text);
    logging_run($res);
    $x=(640-$res[2]*2)/2;

    //计算文字占用像素

    imagettftext($target, $data["size"], 0, $x, $data["top"] + $data["size"], $color, $font, $text);
    return $target;
}

杨佳乐 发布于  2024-4-30 11:01