图片上传后自动旋转php处理代码

<?php 
/*
 开启 exif php扩展
 exif_read_data() 函数从 JPEG 或 TIFF 图像文件中读取 EXIF 头信息。这样就可以读取数码相机产生的原数据。
 而exif_read_data得到的值的Orientation为图片的上传时的原旋转数值
 !有些android上传的图片没有 Orientation 属性
  */

$source = imagecreatefromjpeg($_FILES['img']['tmp_name']);
$exif = exif_read_data($_FILES['img']['tmp_name']);
if(!empty($exif['Orientation'])) {
	switch($exif['Orientation']) {
		case 8:
			$source = imagerotate($source,90,0);
			break;
		case 3:
			$source = imagerotate($source,180,0);
			break;
		case 6:
			$source = imagerotate($source,-90,0);
			break;
	}
}

imagejpeg($source,"123.jpg");  //存储图片

 

HTTP::Tiny_post使用

#!/usr/bin/perl

use strict;
use HTTP::Tiny;
use Data::Dumper;

my $http = HTTP::Tiny->new(
	'agent'=>'Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
	'default_headers'=>{
		'X-Requested-With'=>'XMLHttpRequest',
		'Accept'=>'application/json, text/javascript, */*; q=0.01',
		'Content-Type'=>'application/x-www-form-urlencoded; charset=UTF-8'

	}
);

my $response = $http->post_form('http://www.yiyou.org/xxx.php',
						 {post_key1=>$value1,post_key2=>$value2});
print Dumper $response;

 

基于mojolicus迷你http代理服务器

来源mojolicus源码里的t 目录,觉得不错,所以分享出来学习

use Mojo::Base -strict;
use Mojo::IOLoop;

# Minimal CONNECT proxy server to test TLS tunneling
my %buffer;
Mojo::IOLoop->server(
  {port => 3000} => sub {
    my ($loop, $stream, $id) = @_;

    # Connection to client
    $stream->on(
      read => sub {
        my ($stream, $chunk) = @_;

        # Write chunk from client to server
        my $server = $buffer{$id}{connection};
        return Mojo::IOLoop->stream($server)->write($chunk) if length $server;

        # Read connect request from client
        my $buffer = $buffer{$id}{client} .= $chunk;
        if ($buffer =~ /\x0d?\x0a\x0d?\x0a$/) {
          $buffer{$id}{client} = '';
          if ($buffer =~ /CONNECT (\S+):(\d+)?/) {
            my ($address, $port) = ($1, $2 || 80);

            # Connection to server
            $buffer{$id}{connection} = Mojo::IOLoop->client(
              {address => $address, port => $port} => sub {
                my ($loop, $err, $stream) = @_;

                # Connection to server failed
                if ($err) {
                  say "Connection error for $address:$port: $err";
                  Mojo::IOLoop->remove($id);
                  return delete $buffer{$id};
                }

                # Start forwarding data in both directions
                say "Forwarding to $address:$port";
                Mojo::IOLoop->stream($id)
                  ->write("HTTP/1.1 200 OK\x0d\x0a"
                    . "Connection: keep-alive\x0d\x0a\x0d\x0a");
                $stream->on(
                  read => sub {
                    my ($stream, $chunk) = @_;
                    Mojo::IOLoop->stream($id)->write($chunk);
                  }
                );

                # Server closed connection
                $stream->on(
                  close => sub {
                    Mojo::IOLoop->remove($id);
                    delete $buffer{$id};
                  }
                );
              }
            );
          }

          # Invalid request from client
          else { Mojo::IOLoop->remove($id) }
        }
      }
    );

    # Client closed connection
    $stream->on(
      close => sub {
        my $buffer = delete $buffer{$id};
        Mojo::IOLoop->remove($buffer->{connection}) if $buffer->{connection};
      }
    );
  }
);

print <<'EOF';
Starting CONNECT proxy on port 3000.
For testing use something like "HTTPS_PROXY=http://127.0.0.1:3000".
EOF

Mojo::IOLoop->start;

1;

 

更新系统已安装的perl模块

方法还挻多的

查看模块版本

cpan-outdated

cpan-outdated -p

用cpanp ,在交互模式下输入 o

1、用cpan

cpan -u

2、用cpan-outdate

# install with cpan
% cpan-outdated | xargs cpan -i

# install with cpanm
% cpan-outdated | cpanm
% cpan-outdated -p | cpanm

www.yiyou.org

一个快速获取/更新 Let’s encrypt 证书的 shell script

一个快速获取/更新 Let’s encrypt 证书的 shell script

调用 acme_tiny.py 认证、获取、更新证书,不需要额外的依赖。

下载到本地

wget https://raw.githubusercontent.com/xdtianyu/scripts/master/lets-encrypt/letsencrypt.conf
wget https://raw.githubusercontent.com/xdtianyu/scripts/master/lets-encrypt/letsencrypt.sh
chmod +x letsencrypt.sh

配置文件

只需要修改 DOMAIN_KEY DOMAIN_DIR DOMAINS 为你自己的信息

ACCOUNT_KEY="letsencrypt-account.key"
DOMAIN_KEY="example.com.key"
DOMAIN_DIR="/var/www/example.com"
DOMAINS="DNS:example.com,DNS:whatever.example.com"
#ECC=TRUE
#LIGHTTPD=TRUE

执行过程中会自动生成需要的 key 文件。其中 ACCOUNT_KEY 为账户密钥, DOMAIN_KEY 为域名私钥, DOMAIN_DIR 为域名指向的目录,DOMAINS 为要签的域名列表, 需要 ECC 证书时取消 #ECC=TRUE 的注释,需要为 lighttpd 生成 pem 文件时,取消#LIGHTTPD=TRUE 的注释。

运行

./letsencrypt.sh letsencrypt.conf

注意

需要已经绑定域名到 /var/www/example.com 目录,即通过 http://example.com http://whatever.example.com 可以访问到/var/www/example.com 目录,用于域名的验证

将会生成如下几个文件

lets-encrypt-x1-cross-signed.pem
example.chained.crt          # 即网上搜索教程里常见的 fullchain.pem
example.com.key              # 即网上搜索教程里常见的 privkey.pem 
example.crt
example.csr

在 nginx 里添加 ssl 相关的配置

ssl_certificate     /path/to/cert/example.chained.crt;
ssl_certificate_key /path/to/cert/example.key;

cron 定时任务

每个月自动更新一次证书,可以在脚本最后加入 service nginx reload等重新加载服务。

0 0 1 * * /etc/nginx/certs/letsencrypt.sh /etc/nginx/certs/letsencrypt.conf >> /var/log/lets-encrypt.log 2>&1

php基于curl的get/post请求函数

以下为代码片段,需要根据自己的功能修改

function request($url, $params = array(), $httpMethod = 'GET', $multi = false)
    {
    	// when using bae(baidu app engine) to deploy the application,
    	// just comment the following line
    	$ch = curl_init();
    	// when using bae(baidu app engine) to deploy the application,
    	// and uncomment the following two lines
    	//$fetch= new BaeFetchUrl();
  		//$ch = $fetch->getHandle();
  		
    	$curl_opts = array(
			CURLOPT_CONNECTTIMEOUT	=> 3,
			CURLOPT_TIMEOUT			=> 5,
			CURLOPT_USERAGENT		=> 'baidu-apiclient-php-2.0',
	    	CURLOPT_HTTP_VERSION	=> CURL_HTTP_VERSION_1_1,
	    	CURLOPT_RETURNTRANSFER	=> true,
	    	CURLOPT_HEADER			=> false,
	    	CURLOPT_FOLLOWLOCATION	=> false,
		);

		if (stripos($url, 'https://') === 0) {
			$curl_opts[CURLOPT_SSL_VERIFYPEER] = false;
		}
		
		if (strtoupper($httpMethod) === 'GET') {
			$query = http_build_query($params, '', '&');
			$delimiter = strpos($url, '?') === false ? '?' : '&';
    		$curl_opts[CURLOPT_URL] = $url . $delimiter . $query;
    		$curl_opts[CURLOPT_POST] = false;
		} else {
			$headers = array();
			if ($multi && is_array($params) && !empty($params)) {
				$body = self::buildHttpMultipartBody($params);
				$headers[] = 'Content-Type: multipart/form-data; boundary=' . self::$boundary;
			} else {
				$body = http_build_query($params, '', '&');
			}
			$curl_opts[CURLOPT_URL] = $url;
    		$curl_opts[CURLOPT_POSTFIELDS] = $body;
    		$curl_opts[CURLOPT_HTTPHEADER] = $headers;
		}
    
    	curl_setopt_array($ch, $curl_opts);
        $result = curl_exec($ch);
        
    	if ($result === false) {
    		self::setError(curl_errno($ch), curl_error($ch));
            curl_close($ch);
            return false;
    	} elseif (empty($result)) {
    		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    		if ($http_code != 200) {
    			self::setError($http_code, 'http response status code: ' . $http_code);
    			curl_close($ch);
    			return false;
    		}
    	}
        
    	curl_close($ch);
    	
    	return $result;
    }