龚哥哥 - 山里男儿 爱生活、做自己!
CentOS安装pcre缺少libpcre.so.0文件
发表于 2015-8-31 | 浏览(17024) | 服务器

centos 6.4 64位系统,安装pcre错误:

grep: error while loading shared libraries: libpcre.so.0: cannot open shared object file: No such file or directory

checking for grep that handles long lines and -e... configure: error: no acceptable grep could be found in /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/xpg4/bin

解决方案

wget http://mirror.centos.org/centos/6/os/x86_64/Packages/pcre-7.8-6.el6.x86_64.rpm
rpm -ivh pcre-7.8-6.el6.x86_64.rpm
上面的文件是适合我操作系统的(centos6 , 64位)。如果不适合你的系统,可以去这个网站http://pkgs.org/download/libpcre.so.0 下载适合的文件。

阅读全文

CentOS安装PHP错误:error: xml2-config not found
发表于 2015-8-31 | 浏览(5139) | 服务器

错误

configure: error: xml2-config not found. Please check your libxml2 installation.

解决方案

yum install libxml2
yum install libxml2-devel

阅读全文

安装PHP错误:configure: error: mcrypt.h not found. Plea
发表于 2015-8-31 | 浏览(4976) | 服务器

今天在编译php的时候,出现如下错误php安装出错:configure: error: mcrypt.h not found. Please reinstall libmcrypt.,意思是,没有查找到mcrytp.h,需要安装libcrytp,在下面的地址下载libmarypt:

解决办法一

1、安装第三方yum源
wget http://www.atomicorp.com/installers/atomic
chmod a+x chmod
sh ./atomic

2、使用yum命令安装
yum  install  php-mcrypt  libmcrypt  libmcrypt-devel 

解决办法二

使用php mcrypt 前必须先安装Libmcrypt

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz

安装

tar -zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
mkdir -p /usr/local/libmcrypt
./configure prefix=/usr/local/libmcrypt/
make
make install

然后再安装PHP

========================================================================

如果以上方法仍然不能安装,请参考:

rpm -ivh "http://www.lishiming.net/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm

yum install -y libmcrypt-devel 

因为centos6.x 默认的yum源没有libmcrypt-devel 这个包,只能借助第三方yum源。

文章原创地址:http://blog.163.com/yxba_02/blog/static/1875576201272583532588/

阅读全文

查看MySQL SQL语句执行的时间
发表于 2015-8-31 | 浏览(6181) | 数据库

1;在控制台操作,查看是否已开启profile

show variables like "%pro%";

+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| have_profiling            | YES   |
| profiling                 | OFF   |未开启
| profiling_history_size    | 15    |
| protocol_version          | 10    |
| proxy_user                |       |
| slave_compressed_protocol | OFF   |
| stored_program_cache      | 256   |
+---------------------------+-------+

2;开启profile

set profiling=1;
show variables like "%pro%";

+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| have_profiling            | YES   |
| profiling                 | ON    |
| profiling_history_size    | 15    |
| protocol_version          | 10    |
| proxy_user                |       |
| slave_compressed_protocol | OFF   |
| stored_program_cache      | 256   |
+---------------------------+-------+

3;进行测试(以毫秒为单位)

select * from user;
show profiles;

+----------+------------+-----------------------------+
| Query_ID | Duration| Query                       |
+----------+------------+-----------------------------+
| 1        | 0.00152800 | show variables like "%pro%" |
| 2        | 0.00098075 | show tables                 |
| 3        | 0.00035975 | select * from user          |
| 4        | 0.00077625 | select * from user      |
| 5        | 0.00090725 | show variables like "%pro%" |
| 6        | 0.00068650 | select * from user      |
| 7        | 0.00069100 | select * from user      |
| 8        | 0.00088225 | select * from user      |
+----------+------------+-----------------------------+

4;查看指定语句的详细执行(Query_ID)

show profile for query 8;

+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000084 |
| checking permissions | 0.000016 |
| Opening tables       | 0.000038 |
| init                 | 0.000068 |
| System lock          | 0.000020 |
| optimizing           | 0.000011 |
| statistics           | 0.000032 |
| preparing            | 0.000019 |
| executing            | 0.000005 |
| Sending data         | 0.000347 |
| end                  | 0.000014 |
| query end            | 0.000008 |
| closing tables       | 0.000054 |
| freeing items        | 0.000141 |
| cleaning up          | 0.000027 |
+----------------------+----------+

阅读全文

Apache自带的压力测试工具 鸭梨测试ab命令详解
发表于 2015-8-31 | 浏览(5265) | 服务器

1;ab命令

ab -n100 -c10 http://mei.la/index.php
-n100 在测试会话中所执行的请求个数。默认时,仅执行一个请求
-c10 一次产生的请求个数。默认是一次一个
-t60 测试所进行的最大秒数。其内部隐含值是-n 50000,它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制
http://mei.la/index.php 目标url地址

2;测试结果

被测试的Web服务器软件名称
Server Software:        nginx

请求的URL主机名
Server Hostname:        mei.la

被测试的Web服务器软件的监听端口
Server Port:            80

请求的URL中的根绝对路径,通过该文件的后缀名,我们一般可以了解该请求的类型
Document Path:          /index.php

HTTP响应数据的正文长度
Document Length:        2041 bytes

并发用户数,这是我们设置的参数之一
Concurrency Level:      10

所有这些请求被处理完成所花费的总时间
Time taken for tests:   0.766 seconds

总请求数量,这是我们设置的参数之一
Complete requests:      100

失败的请求数量,这里的失败是指请求在连接服务器、发送数据等环节发生异常,以及无响应后超时的情况。如果接收到的HTTP响应数据的头信息中含有2XX以外的状态码,则会在测试结果中显示另一个名为       “Non-2xx responses”的统计项,用于统计这部分请求数,这些请求并不算在失败的请求中。
(Connect: 0, Receive: 0, Length: 2, Exceptions: 0)
Failed requests:        2

写入错误
Write errors:           0

所有请求的响应数据长度总和,包括每个HTTP响应数据的头信息和正文数据的长度。注意这里不包括HTTP请求数据的长度,仅仅为web服务器流向用户PC的应用层数据总长度。
Total transferred:      242120 bytes

所有请求的响应数据中正文数据的总和,也就是减去了Total transferred中HTTP响应数据中的头信息的长度
HTML transferred:       219348 byte

吞吐率,计算公式:Complete requests / Time taken for tests
Requests per second:    130.55 [#/sec] (mean)

用户平均请求等待时间,计算公式:Time token for tests/(Complete requests/Concurrency Level)
Time per request:       76.599 [ms] (mean)

服务器平均请求等待时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。也可以这么统计:Time per request/Concurrency Level
Time per request:       7.660 [ms] (mean, across all concurrent requests)

表示这些请求在单位时间内从服务器获取的数据长度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量
Transfer rate:          308.68 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       29   33   3.2     32      47
Processing:    30   40  24.0     35     209
Waiting:       30   37  23.6     33     203
Total:         62   73  24.5     68     244

Percentage of the requests served within a certain time (ms)
  50%     68      50%的请求在68ms内返回
  66%     71
  75%     73
  80%     75
  90%     79
  95%     87
  98%    234
  99%    244
  100%   244 (longest request)

阅读全文

PHP支付宝支付接口pc+wap MD5加密方式
发表于 2015-8-31 | 浏览(9410) | PHP
<?php

/**
 * 支付宝支付驱动
 * @author  Devil
 * @version V_1.0.0
 */
class AlipayLibrary
{
    /**
     * [__construct 构造方法]
     */
    public function __construct(){}

    /**
     * [SoonPay 立即支付]
     * @param [array] $data [支付信息]
     */
    public function SoonPay($data, $config)
    {
        if(empty($data) || empty($config)) return false;

                //这里判断是否是手机访问,自己写一个方法接口。根据访问终端类型进行区分pc或wap
        if(IsMobile())
        {
            $this->SoonPayMobile($data, $config);
        } else {
            $this->SoonPayWeb($data, $config);
        }
    }

    /**
     * [SoonPayMobile wap支付]
     * @param  [array] $data   [参数列表]
     * @param  [array] $config [配置信息]   
     */
    private function SoonPayMobile($data, $config)
    {
        $request_token = $this->GetRequestToken($data, $config);

        $req_data = '<auth_and_execute_req><request_token>'.$request_token.'</request_token></auth_and_execute_req>';
        $parameter = array(
            'service'               =>   'alipay.wap.auth.authAndExecute',
            'format'                =>   'xml',
            'v'                     =>   '2.0',
            'partner'               =>   $config['id'],
            'sec_id'                =>   'MD5',
            'req_data'              =>   $req_data,
            'request_token'         =>   $request_token
        );

        $param = $this->GetParamSign($parameter, $config);
        header('location:http://wappaygw.alipay.com/service/rest.htm?'.$param['param']. '&sign='.md5($param['sign']));

    }

    /**
     * [GetRequestToken 获取临时token]
     * @param  [array] $data   [参数列表]
     * @param  [array] $config [配置信息]
     * @return [string]        [返回临时token]
     */
    private function GetRequestToken($data, $config)
    {
        $parameter = array(
            'service'               =>   'alipay.wap.trade.create.direct',
            'format'                =>   'xml',
            'v'                     =>   '2.0',
            'partner'               =>   $config['id'],
            'req_id'                =>   $data['order_sn'],
            'sec_id'                =>   'MD5',
            'req_data'              =>   $this->GetReqData($data, $config),
            'subject'               =>   $data['name'],
            'out_trade_no'          =>   $data['order_sn'],
            'total_fee'             =>   $data['total_price'],
            'seller_account_name'   =>   $config['name'],
            'call_back_url'         =>   $data['call_back_url'],
            'notify_url'            =>   $data['notify_url'],
            'out_user'              =>   $data['out_user'],
            'merchant_url'          =>   $data['merchant_url'],
        );

        $param = $this->GetParamSign($parameter, $config);
        $ret = urldecode(file_get_contents('http://wappaygw.alipay.com/service/rest.htm?'.$param['param'].'&sign='.md5($param['sign'])));

        $para_split = explode('&',$ret);
        //把切割后的字符串数组变成变量与数值组合的数组
        foreach ($para_split as $item) {
            //获得第一个=字符的位置
            $nPos = strpos($item,'=');
            //获得字符串长度
            $nLen = strlen($item);
            //获得变量名
            $key = substr($item,0,$nPos);
            //获得数值
            $value = substr($item,$nPos+1,$nLen-$nPos-1);
            //放入数组中
            $para_text[$key] = $value;
        }

        $req = Xml_Array($para_text['res_data']);
        if(empty($req['request_token']))
        {
            exit(header('location:'.__ROOT__.'index.php?g=Info&c=Prompt&f=PromptInfo&state=error&content=支付宝异常错误&url='.__ROOT__));
        }

        return $req['request_token'];
    }

    private function GetReqData($data, $config)
    {
        return '<direct_trade_create_req>
                    <subject>'.$data['name'].'</subject>
                    <out_trade_no>'.$data['order_sn'].'</out_trade_no>
                    <total_fee>'.$data['total_price'].'</total_fee>
                    <seller_account_name>'.$config['name'].'</seller_account_name>
                    <call_back_url>'.$data['call_back_url'].'</call_back_url>
                    <notify_url>'.$data['notify_url'].'</notify_url>
                    <out_user>'.$data['out_user'].'</out_user>
                    <merchant_url>'.$data['merchant_url'].'</merchant_url>
                    <pay_expire>3600</pay_expire>
                    <agent_id>0</agent_id>
                </direct_trade_create_req>';
    }

    /**
     * [SoonPayWeb web支付]
     * @param [array] $data   [订单信息]
     * @param [array] $config [配置信息]
     */
    private function SoonPayWeb($data, $config)
    {
        $parameter = array(
            'service'           => 'create_direct_pay_by_user',
            'partner'           => $config['id'],
            '_input_charset'    => ML_CHARSET,
            'notify_url'        => $data['notify_url'],
            'return_url'        => $data['call_back_url'],

            /* 业务参数 */
            'subject'           => $data['name'],
            'out_trade_no'      => $data['order_sn'],
            'price'             => $data['total_price'],

            'quantity'          => 1,
            'payment_type'      => 1,

            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',

            /* 买卖双方信息 */
            'seller_email'      => $config['name']
        );

        $param = $this->GetParamSign($parameter, $config);
        header('location:https://mapi.alipay.com/gateway.do?'.$param['param']. '&sign='.md5($param['sign']).'&sign_type=MD5');
    }

    /**
     * [GetParamSign 生成参数和签名]
     * @param  [array] $data   [待生成的参数]
     * @param  [array] $config [配置信息]
     * @return [array]         [生成好的参数和签名]
     */
    private function GetParamSign($data, $config)
    {
        $param = '';
        $sign  = '';
        ksort($data);

        foreach($data AS $key => $val)
        {
            $param .= "$key=" .urlencode($val). "&";
            $sign  .= "$key=$val&";
        }

        return array(
            'param' =>   substr($param, 0, -1),
            'sign'  =>   substr($sign, 0, -1).$config['key']
        );
    }

    /**
     * [Respond 异步处理]
     * @param  [array] $config [配置信息]
     * @return [array|string] [成功返回数据列表,失败返回no]
     */
    public function Respond($config)
    {
        if(empty($config)) return 'no';

        $data = empty($_POST) ? $_GET :  array_merge($_GET, $_POST);
        ksort($data);

        $sign = '';
        $sec = isset($data['sec_id']) ? $data['sec_id'] : '';
        if($sec == 'MD5')
        {
            $data_xml = json_decode(json_encode((array) simplexml_load_string($data['notify_data'])), true);
            $data = array_merge($data, $data_xml);
            $sign = 'service='.$data['service'].'&v='.$data['v'].'&sec_id='.$data['sec_id'].'&notify_data='.$data['notify_data'];
        } else {
            foreach($data AS $key=>$val)
            {
                if ($key != 'sign' && $key != 'sign_type' && $key != 'code')
                {
                    $sign .= "$key=$val&";
                }
            }
            $sign = substr($sign, 0, -1);
        }
        if(!isset($data['sign']) || md5($sign.$config['key']) != $data['sign']) return 'no';

        /* 支付状态 */
        $state = isset($data['trade_status']) ? $data['trade_status'] : $data['result'];
        switch($state)
        {
            case 'TRADE_SUCCESS':
                return $data;
                break;

            case 'TRADE_FINISHED':
                return $data;
                break;

               case 'success':
                return $data;
                break;
        }
        return 'no';
    }

}
?>

阅读全文

PHP图片上传
发表于 2015-8-31 | 浏览(5332) | PHP
<?php

/**
 * 图片上传驱动
 * @author  Devil
 * @version 1.0.0
 */
class ImageLibrary
{
    private $i_path;
    private $i_type;
    private $i_is_new_name;
    private $i_quality;
    private $i_data;

    /**
     * [__construct 构造方法]
     * @param [array] $parameters [参数列表]
     */
    private function __construct($parameters)
    {
        /* 检测是否支持gd */
        $this->IsGD();

        /* 图片类型 */
        $this->i_type = empty($parameters['type']) ? $this->GetImageType() : $parameters['type'];

        /* 图片名称是否使用新名称, 则使用原图片名称 */
        $this->i_is_new_name = (isset($parameters['is_new_name']) && is_bool($parameters['is_new_name'])) ? $parameters['is_new_name'] : true;

        /* jpg图片的质量值, 值越大质量越好 */
        $this->i_quality = max(75, isset($parameters['quality']) ? intval($parameters['quality']) : 75);
    }

    /**
     * [Instance 静态方法实例化本类]
     * @return [object] [实例对象]
     */
    public static function Instance($parameters = array())
    {
        static $object = null;
        if(!is_object($object)) $object = new self($parameters);
        return $object;
    }

    /**
     * [SetParameters 参数设置]
     * @param [array] $parameters [参数列表]
     */
    public function SetParameters($parameters = array())
    {
        if(empty($parameters)) return;
        $this->__construct($parameters);
    }

    /**
     * [Is_GD 检查是否支持gd库]
     */
    private function IsGD()
    {
        if(!isset(gd_info()['GD Version']) || empty(gd_info()['GD Version'])) Api_Return(L('code_412'), 412);
    }

    /**
     * [GetImageType 获取图片后缀类型]
     * @return [array] [图片后缀类型]
     */
    private function GetImageType()
    {
        return array(
            'gif'   => 'image/gif',
            'png'   => 'image/png',
            'jpg'   => 'image/jpeg',
            'bmp'   => 'image/bmp'
        );
    }

    /**
     * [Initialization 基本信息初始化]
     * @param  [array]   $data [临时图片数据]
     * @param  [string]  $path [图片保存路径]
     * @return [boolean]       [true或false]
     */
    private function Initialization($data, $path)
    {
        if(empty($data)) return false;

        /* 图片保存地址 */
        $path .= (substr($path, -1) == '/') ? '' : '/';
        $this->i_path = empty($path) ? '/tmp/image' : $path;

        /* 设置存储路径是否存在 */
        if(!is_dir($this->i_path)) @mkdir($this->i_path, 0777, true);
        if(!@opendir($this->i_path)) exit('dir not access');

        /* 初始化返回数据 */
        $this->i_data = array();

        return true;
    }

    /**
     * [GetNewFileName 获取随机名称]
     * @return [string] [新的名称]
     */
    private function GetNewFileName()
    {
        return date('YmdHis').str_shuffle(rand());
    }

    /**
     * [SaveBaseImages base64图片存储]
     * @param  [array] $data  [需要存储的base数据, 一维数组]
     * @param  [string] $path [存储路径]
     * @return [array]        [返回图片地址, 一维数组]
     */
    public function SaveBaseImages($data, $path)
    {
        if(!$this->Initialization($data, $path)) return null;

        for($i=0; $i<count($data); $i++)
        {
            if(empty($data[$i])) continue;

            $temp_img = str_replace(array("\n", '\n', ' '), '', $data[$i]);
            $img_info = @getimagesize('data://application/octet-stream;base64,'.$temp_img);
            if(empty($img_info['mime'])) continue;

            $type = $this->Is_ImageType($img_info['mime']);
            if($type == 'no') continue;

            $file_name = $this->GetNewFileName().'.'.$type;
            if(file_put_contents($this->i_path.$file_name, base64_decode($temp_img)) !== false) $this->i_data[] = $file_name;
        }
        return $this->i_data;
    }

    /**
     * [SaveBinaryImages 二进制图片保存]
     * @param  [array]  $data [二进制图片数据, 一维数组]
     * @param  [string] $path [存储路径]
     * @return [array]        [返回图片地址, 一维数组]
     */
    public function SaveBinaryImages($data, $path)
    {
        if(!$this->Initialization($data, $path)) return null;

        for($i=0; $i<count($data); $i++)
        {
            if(empty($data[$i])) continue;

            $img_info = getimagesizefromstring($data[$i]);
            if(empty($img_info['mime'])) continue;

            $type = $this->Is_ImageType($img_info['mime']);
            if($type == 'no') continue;

            $file_name = $this->GetNewFileName().'.'.$type;
            if(file_put_contents($this->i_path.$file_name, $data) !== false) $this->i_data[] = $file_name;
        }
        return $this->i_data;
    }

    /**
     * [GetOriginal 获取临时图片文件的原图]
     * @param  [array]  $data [临时图片数据]
     * @param  [string] $path [存储路径]
     * @return [string]       [返回图片地址]
     */
    public function GetOriginal($data, $path)
    {
        if(!$this->Initialization($data, $path)) return '';

        if(empty($data['tmp_name'])) return '';

        $type = $this->Is_ImageType($data['type']);
        if($type == 'no') return '';

        $file_name = $this->GetNewFileName().'.'.$type;

        if(move_uploaded_file($data['tmp_name'], $this->i_path.$file_name)) return $file_name;
        return '';
    }

    /**
     * [GetBinaryCompress 获取指定图片路径的压缩图]
     * @param  [string] $file  [图片地址]
     * @param  [string] $path  [存储路径]
     * @param  [int]    $width [设定图片宽度]
     * @param  [int]    $height[指定图片高度, 不指定则高度按照比例自动计算]
     * @return [string]        [返回图片地址]
     */
    public function GetBinaryCompress($file, $path, $width = 0, $height = 0)
    {
        if(!$this->Initialization($file, $path) || is_array($file) || !file_exists($file)) return '';

        $img_info = pathinfo($file);
        if(empty($img_info['basename'])) return '';

        $type = $this->Is_ImageType($img_info['extension']);
        if($type == 'no') return '';

        $file_name = ($this->i_is_new_name) ? $this->GetNewFileName().'.'.$type : $img_info['basename'];

        if($this->ImageCompress($width, $height, $file, $type, $this->i_path.$file_name)) return $file_name;

        return '';
    }

    /**
     * [GetCompress 获取临时图片文件的压缩图]
     * @param  [array]  $data  [临时图片数据]
     * @param  [string] $path  [存储路径]
     * @param  [int]    $width [设定图片宽度]
     * @param  [int]    $height[指定图片高度, 不指定则高度按照比例自动计算]
     * @return [atring|空字符串] [返回图片存储的名称]
     */
    public function GetCompress($data, $path, $width = 0, $height = 0)
    {
        if(!$this->Initialization($data, $path)) return '';

        if(empty($data['tmp_name'])) return '';

        $type = $this->Is_ImageType($data['type']);
        if($type == 'no') return '';

        $file_name = $this->GetNewFileName().'.'.$type;

        if($this->ImageCompress($width, $height, $data['tmp_name'], $type, $this->i_path.$file_name)) return $file_name;
        return '';
    }

    /**
     * [GetCompressCut 临时图像裁剪]
     * @param [array]   $data       [图像临时数据]
     * @param [string]  $path       [图像存储地址]
     * @param [int]     $width      [指定存储宽度]
     * @param [ing]     $height     [指定存储高度]
     * @param [ing]     $src_x      [裁剪x坐标]
     * @param [ing]     $src_y      [裁剪y坐标]
     * @param [ing]     $src_width  [裁剪区域宽度]
     * @param [ing]     $src_height [裁剪区域高度]
     * @return [atring|空字符串] [返回图片存储的名称]
     */
    function GetCompressCut($data, $path, $width = 0, $height = 0, $src_x = 0, $src_y = 0, $src_width = 0, $src_height = 0)
    {
        if(!$this->Initialization($data, $path)) return '';

        if(empty($data['tmp_name'])) return '';

        $type = $this->Is_ImageType($data['type']);
        if($type == 'no') return '';

        $file_name = $this->GetNewFileName().'.'.$type;

        if($this->ImageCompress($width, $height, $data['tmp_name'], $type, $this->i_path.$file_name, $src_x, $src_y, $src_width, $src_height)) return $file_name;
        return '';
    }

    /**
     * [ImageCompress 图片压缩]
     * @param  [int]    $width [指定图片宽度]
     * @param  [int]    $height[指定图片高度]
     * @param  [string] $file  [原图片地址]
     * @param  [string] $type  [类型]
     * @param  [string] $path  [新图片地址]
     * @return [boolean]       [成功true, 失败false]
     */
    private function ImageCompress($width, $height, $file, $type, $path, $src_x = 0, $src_y = 0, $src_width = 0, $src_height = 0)
    {
        /* 获取图片原本尺寸 */
        list($w, $h) = getimagesize($file);

        /* 尺寸计算 */
        $new_width = ($width > 0 && $w > $width) ? $width : $w;
        $new_height = ($width > 0 && $w > $width) ? (round($h/($w/$width))) : $h;
        if($width > 0 && $height > 0) $new_width = $width;
        if($height > 0) $new_height = $height;

        /* url创建一个新图象 */
        switch($type)
        {
            case 'gif':
                $src_im = @imagecreatefromgif($file);
                break;
            case 'png':
                $src_im = @imagecreatefrompng($file);
                break;
            default:
                $src_im = @imagecreatefromjpeg($file);
        }
        if(!$src_im) return;

        /* 新建一个真彩色图像 */
        $dst_im = imagecreatetruecolor($new_width, $new_height);

        /* 是否裁剪图片 */
        if($src_width > 0 && $src_height > 0)
        {
            /* 新建拷贝大小的真彩图像 */
            $cpd_im = imagecreatetruecolor($src_width, $src_height);

            /* 拷贝图片 */
            imagecopy($cpd_im, $src_im, 0, 0, $src_x, $src_y, $src_width, $src_height);

            /* 图片缩放 */
            $s = imagecopyresampled($dst_im, $cpd_im, 0, 0, 0, 0, $new_width, $new_height, $src_width, $src_height);
        } else {
            /* 图片缩放 */
            $s = imagecopyresampled($dst_im, $src_im, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
        }

        if($s)
        {
            switch($type)
            {
                case 'png':
                    imagepng($dst_im, $path);
                    break;
                case 'gif':
                    imagegif($dst_im, $path);
                    break;
                default:
                    imagejpeg($dst_im, $path, $this->i_quality);
            }
        }
        imagedestroy($dst_im);
        imagedestroy($src_im);

        return $s;
    }

    /**
     * [Is_ImageType 验证后缀名是否合法]
     * @param  [string] $image_type [图片后缀类型]
     * @return [string]             [后缀名或no]
     */
    private function Is_ImageType($image_type)
    {
        if(empty($image_type)) return 'no';
        if(array_key_exists($image_type, $this->i_type)) return $image_type;

        foreach($this->i_type as $key=>$val)
        {
            if($val == $image_type) return $key;
        }
        return 'no';
    }
}
?>

阅读全文

PHP Memcached操作类
发表于 2015-8-31 | 浏览(5563) | PHP
<?php

/**
 * 缓存驱动
 * @author  Devil
 * @version 1.0.0
 */
class CacheLibrary
{
    private $c_obj;
    private $c_time;
    /**
     * [__construct 构造方法]
     */
    public function __construct($host, $port = 11211)
    {
        /* 实例化memcached */
        $this->c_obj = new Memcached();
        if(!$this->c_obj->addServer($host, $port)) Api_Return(L('code_413'), 413);

        /* 基础参数设置 */
        $this->c_time = empty(C('cache')['time']) ? 0 : C('cache')['time'];
    }

    /**
     * [GetTime 获取缓存时间]
     * @return [int] [缓存时间]
     */
    private function GetTime($time = 0)
    {
        return (intval($time) > 0) ? intval($time) : $this->c_time;
    }

    /**
     * [Add 缓存添加]
     * @param  [string]     $key  [索引]
     * @param  [mixed]      $val  [值]
     * @param  [integer]    $time [过期时间(单位秒), 0永久, 或时间戳]
     * @return [boolean]    [成功true, 失败false]
     */
    public function Add($key, $val, $time = 0)
    {
        return $this->c_obj->add($key, $val, $this->GetTime($time));
    }

    /**
     * [Set 缓存替换]
     * @param  [string]     $key  [索引]
     * @param  [mixed]      $val  [值]
     * @param  [integer]    $time [过期时间(单位秒), 0永久, 或时间戳]
     * @return [boolean]    [成功true, 失败false]
     */
    public function Set($key, $val, $time = 0)
    {
        return $this->c_obj->set($key, $val, $this->GetTime($time));
    }

    /**
     * [SetMulti 设置多个索引的缓存数据]
     * @param [type] $key_all [索引数组]
     * @param  [integer]      $time [过期时间(单位秒), 0永久, 或时间戳]
     * @return [boolean]      [成功true, 失败false]
     */
    public function SetMulti($key_all, $time = 0)
    {
        return $this->c_obj->setMulti($key_all, $time);
    }

    /**
     * [Append 向已存在索引后面追加数据]
     * @param [string] $key [索引]
     * @param [string] $val [追加的数据(字符串)]
     */
    public function Append($key, $val)
    {
        $this->setOption();
        return $this->c_obj->append($key, $val);
    }

    /**
     * [Prepend 向已存在索引前面追加数据]
     * @param [string] $key [索引]
     * @param [string] $val [追加的数据(字符串)]
     */
    public function Prepend($key, $val)
    {
        $this->setOption();
        return $this->c_obj->prepend($key, $val);
    }

    /**
     * [Replace 替换已存在索引下的元素]
     * @param  [string]     $key  [索引]
     * @param  [mixed]      $val  [值]
     * @param  [integer]    $time [过期时间(单位秒), 0永久, 或时间戳]
     * @return [boolean]    [成功true, 失败false]
     */
    public function Replace($key, $val, $time = 0)
    {
        $this->c_obj->replace($key, $val, $time);
    }

    /**
     * [setOption 设置选项]
     */
    private function setOption()
    {
        $this->c_obj->setOption(Memcached::OPT_COMPRESSION, false);
    }

    /**
     * [Fetch 抓取下一个结果]
     * @param [string]  $key_all [索引名]
     * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
     */
    public function Fetch($key_all, $cas = false)
    {
        $this->GetDelayed($key_all, $cas);
        return $this->c_obj->fetch();
    }

    /**
     * [FetchAll 抓取所有剩余的结果]
     * @param [string]  $key_all [索引名]
     * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
     */
    public function FetchAll($key_all, $cas = false)
    {
        $this->GetDelayed($key_all, $cas);
        return $this->c_obj->fetchAll();
    }

    /**
     * [GetDelayed 请求多个索引]
     * @param [string]  $key_all [索引名]
     * @param [boolean] $cas     [是否返回长度, 是true, 否false默认]
     */
    private function GetDelayed($key_all, $cas)
    {
        $this->c_obj->getDelayed($key_all, $cas);
    }

    /**
     * [Get 缓存获取]
     * @param  [string]  $key  [索引]
     * @return [mixed]   [当前索引对应的数据]
     */
    public function Get($key)
    {
        return $this->c_obj->get($key);
    }

    /**
     * [GetMulti 获取多个索引的缓存数据]
     * @param [type] $key_all [索引数组]
     */
    public function GetMulti($key_all)
    {
        return $this->c_obj->getMulti($key_all);
    }

    /**
     * [Delete 删除一个索引]
     * @param  [string]  $key [索引]
     * @param  [integer] $time [等待时间(单位秒), 0立即, 或时间戳]
     * @return [boolean] [成功true, 失败false]
     */
    public function Delete($key, $time = 0)
    {
        return $this->c_obj->delete($key, $time);
    }

    /**
     * [DeleteMulti 删除多个索引]
     * @param  [array]            $key_all     [索引数组]
     * @param  [integer]          $time [过期时间(单位秒), 0立即, 或时间戳]
     * @return [boolean或array]    [成功true, 失败false]
     */
    public function DeleteMulti($key_all, $time = 0)
    {
        $s = $this->c_obj->deleteMulti($key_all, $time);
        if(empty($s)) return false;
        foreach($s as $key=>$val)
        {
            if($val !== true) if($this->Get($key) == false) $s[$key] = true;
        }
        return $s;
    }

    /**
     * [Flush 作废所有元素]
     * @param  [integer] $time [等待时间(单位秒), 0立即]
     * @return [boolean] [成功true, 失败false]
     */
    public function Flush($time = 0)
    {
        return $this->c_obj->flush($time);
    }
}
?>

阅读全文

PHP生成分页类 前端基于amazeui
发表于 2015-8-31 | 浏览(5861) | PHP
<?php

/**
 * 分页驱动
 * @author  Devil
 * @version v_1.0.0
 */
class PageLibrary
{
    private $page;
    private $total;
    private $number;
    private $bt_number;
    private $where;
    private $page_total;
    private $url;
    private $html;

    /**
     * [__construct description]
     * @param [int]    $param['total']      [数据总数]
     * @param [int]    $param['number']     [每页数据条数]
     * @param [int]    $param['bt_number']  [分页显示按钮个数]
     * @param [array]  $param['where']      [额外条件(键值对)]
     * @param [string] $param['url']        [url地址]
     */
    public function __construct($param = array())
    {
        $this->page = max(1, isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1);
        $this->total = max(1, isset($param['total']) ? intval($param['total']) : 1);
        $this->number = max(1, isset($param['number']) ? intval($param['number']) : 1);
        $this->bt_number = isset($param['bt_number']) ? intval($param['bt_number']) : 2;
        $this->where = (isset($param['where']) && is_array($param['where'])) ? $param['where'] : '';
        $this->url = isset($param['url']) ? $param['url'] : '';
        $this->page_total = 1;
        $this->html = '';

        /* 参数设置 */
        $this->SetParem();
    }

    /**
     * [SetParem 参数设置]
     */
    private function SetParem()
    {
        /* 防止超出最大页码数 */
        $this->page_total = ceil($this->total/$this->number);
        if($this->page > $this->page_total) $this->page = $this->page_total;

        /* 额外条件url设置 */
        if(!empty($this->where) && is_array($this->where))
        {
            foreach($this->where as $k=>$v) $this->url .= '&'.$k.'='.$v;
        }

    }

    /**
     * [GetPageHtml 获取生成好的分页代码]
     */
    public function GetPageHtml()
    {
        $this->html .= '<ul class="am-pagination am-pagination-centered"><li';
        $this->html .= ($this->page > 1) ? '' : ' class="am-disabled"';
        $this->html .= '><a href="'.$this->url.'&page='.($this->page-1).'" class="meila-radius">&laquo;</a></li>';
        $this->html .= $this->GetButtonNumberHtml();
        $this->html .= '<li';
        $this->html .= ($this->page > 0 && $this->page < $this->page_total) ? '' : ' class="am-disabled"';
        $this->html .= '><a href="'.$this->url.'&page='.($this->page+1).'" class="meila-radius">&raquo;</a></li></ul>';

        return $this->html;
    }

    /**
     * [GetButtonNumberHtml 获取button显示个数的html]
     * @return [string] [按钮个数html代码]
     */
    private function GetButtonNumberHtml()
    {
        $html_before = '';
        $html_after = '';
        $html_page = '<li class="am-active"><a class="meila-radius">'.$this->page.'</a></li>';
        if($this->bt_number > 0)
        {
            /* 前按钮 */
            if($this->page > 1)
            {
                $total = ($this->page-$this->bt_number < 1) ? 1 : $this->page-$this->bt_number;
                for($i=$this->page-1; $i>=$total; $i--)
                {
                    $html_before = '<li><a href="'.$this->url.'&page='.$i.'" class="meila-radius">'.$i.'</a></li>'.$html_before;
                }
            }

            /* 后按钮 */
            if($this->page_total > $this->page)
            {
                $total = ($this->page+$this->bt_number > $this->page_total) ? $this->page_total : $this->page+$this->bt_number;
                for($i=$this->page+1; $i<=$total; $i++)
                {
                    $html_after .= '<li><a href="'.$this->url.'&page='.$i.'" class="meila-radius">'.$i.'</a></li>';
                }
            }
        }
        return $html_before.$html_page.$html_after;
    }

    /**
     * [GetPageStarNumber 获取分页起始值]
     */
    public function GetPageStarNumber()
    {
        return intval(($this->page-1)*$this->number);
    }
}
?>

阅读全文

Linux下使用webbench测试服务器压力
发表于 2015-8-31 | 浏览(5392) | 服务器

webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便。

适用系统:Linux

1;安装

360云盘下载地址:

    http://yunpan.cn/ccQeU9IE5cRU7 (提取码:74e2)

wget http://blog.zyan.cc/soft/linux/webbench/webbench-1.5.tar.gz
tar zxvf webbench-1.5.tar.gz
cd webbench-1.5
make && make install

2;使用

webbench -c 500 -t 30 http://mei.la/

参数说明
  -c表示并发数,-t表示时间(秒)

3;测试结果

Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://mei.la/
500 clients, running 30 sec.

Speed=3230 pages/min, 11614212 bytes/sec.
Requests: 1615 susceed, 0 failed.

阅读全文

TOP