How to wake up a pc by PHP

source: 
http://php.net/manual/en/ref.sockets.php
http://www.rkrishardy.com/2009/12/permission-denied-13-when-opening-socket-in-php-apache/

<?php
/**
 * Wake-on-LAN
 *
 * @return boolean
 *   TRUE:    Socked was created successfully and the message has been sent.
 *   FALSE:   Something went wrong
 *
 * @param string|array  $mac   You will WAKE-UP this WOL-enabled computer, you
 *                             need to add the MAC-address here. Mac can be
 *                             array too.
 *
 * @param string|array  $addr  You will send and broadcast to this address.
 *                             Normally you need to use the 255.255.255.255
 *                             address, so I made it as the default. You don't need to do anything with this.
 *                       
 *                             If you get permission denied errors when using
 *                             255.255.255.255 have permission denied problems
 *                             you can set $addr = false to get the broadcast
 *                             address from the network interface using the
 *                             ifconfig command.
 *
 *                             $addr can be array with broadcast IP values
 *
 * Example 1:
 *   When the message has been sent you will see the message "Done...."
 *   if ( wake_on_lan('00:00:00:00:00:00'))
 *      echo 'Done...';
 *   else
 *      echo 'Error while sending';
 */

function wake_on_lan($mac, $addr=false, $port=7) {
    if ($addr === false){
        exec("ifconfig | grep Bcast | cut -d \":\" -f 3 | cut -d \" \" -f 1",$addr);
        $addr=array_flip(array_flip($addr));
    }
    if(is_array($addr)){
        $last_ret = false;
        for ($i = 0; $i < count($addr); $i++)
            if ($addr[$i] !== false) {
                $last_ret = wake_on_lan($mac, $addr[$i], $port);
            }
        return $last_ret;
    }
    if (is_array($mac)){
        $ret = array();
        foreach($mac as $k =< $v)
            $ret[$k] = wake_on_lan($v, $addr, $port);
        return $ret;
    }
    //Check if it's an real MAC-address and split it into an array
    $mac = strtoupper($mac);
    if (!preg_match("/([A-F0-9]{1,2}[-:]){5}[A-F0-9]{1,2}/", $mac, $maccheck))
        return false;
    $addr_byte = preg_split("/[-:]/", $maccheck[0]);
 
    //Creating hardware adress
    $hw_addr = '';
    for ($a = 0; $a < 6; $a++)//Changing mac adres from HEXEDECIMAL to DECIMAL
        $hw_addr .= chr(hexdec($addr_byte[$a]));
  
    //Create package data
    $msg = str_repeat(chr(255),6);
    for ($a = 1; $a <= 16; $a++)
        $msg .= $hw_addr;
    //Sending data
    if (function_exists('socket_create')){
        //socket_create exists
        $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);    //Can create the socket
        if ($sock){
            $sock_data = socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, 1); //Set
            if ($sock_data){
                $sock_data = socket_sendto($sock, $msg, strlen($msg), 0, $addr,$port); //Send data
                if ($sock_data){
                    socket_close($sock); //Close socket
                    unset($sock);
                    return true;
                }
            }
        }
        @socket_close($sock);
        unset($sock);
    }
    $sock=fsockopen("udp://" . $addr, $port);
    if($sock){
        $ret=fwrite($sock,$msg);
        fclose($sock);
    }
    if($ret)
        return true;
    return false;  
}

if (@wake_on_lan('00:00:00:00:00:00')) {
    echo 'Done...';
} else {
    echo 'Error while sending';
}
?>

加载postgres模块后apache无法启动

想在本地搭建一个PostgreSQL的运行环境,完成之后,始终在apache/php环境无法加载postgresql模块。启动的时候无法加载libpq.dll,将postgresql的libpq拷贝到php目录无效,拷贝到windows目录无效,拷贝到windows\system32目录也无效,只好在网上找解决方法。搜到以下方法:

refering to the libpq.dll that comes bundled with PHP, like this:

LoadFile “C:/php/libpq.dll”

(原文地址:http://stackoverflow.com/questions/551734/php-not-loading-php-pgsql-dll-on-windows)

在http.conf文件中增加这一行,完美解决。其实这个答案之前有混淆的问题,比如加载pg目录的libpq.dll,似乎无效,只能用php目录里面自带的libpq.dll。

[转载]PHP 5.3.17 编译安装时出现 undefined reference to `libiconv’ 错误的解决方法

 

手动编译PHP安装时遇到如下错误
/usr/local/src/php-5.3.10/ext/xmlrpc/libxmlrpc/encodings.c:73: undefined reference to `libiconv_open’ /usr/local/src/php-5.3.10/ext/xmlrpc/libxmlrpc/encodings.c:81: undefined reference to `libiconv’ /usr/local/src/php-5.3.10/ext/xmlrpc/libxmlrpc/encodings.c:101: undefined reference to `libiconv_close’ collect2: ld returned 1 exit status make: *** [sapi/fpm/php-fpm] 错误
 1 表面看,是libiconv安装问题,重装libiconv之后问题依旧,网上看有人舍弃libiconv,使用 –without-iconv,我觉的不可取,这样是回避问题。 找了n久,终于找到bug所在: 在执行完 ./configure … 之后,修改下 Makefile,找到其中的
EXTRA_LIBS = -lcrypt -lz -lcrypt -lrt -lmysqlclient -lmcrypt -lldap -llber -lfreetype -lpng -lz -ljpeg -lcurl -lz -lrt -lm -ldl -lnsl -lrt -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lcurl -ldl -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lidn -lssl -lcrypto -lz -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt
 在最后面添加 -liconv ,修改后如下
EXTRA_LIBS = -lcrypt -lz -lcrypt -lrt -lmysqlclient -lmcrypt -lldap -llber -lfreetype -lpng -lz -ljpeg -lcurl -lz -lrt -lm -ldl -lnsl -lrt -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lcurl -ldl -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lidn -lssl -lcrypto -lz -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -liconv

本文出自 “技术成就梦想” 博客,请务必保留此出处http://flyfishes.blog.51cto.com/3527694/819693

PHP根据图片Orientation旋转图片

最近发现发送到微信公众平台的图片被旋转了90度,导致后续操作出错。后来仔细研究,发现用iphone前置摄像头拍出来的照片会自动镜像,而且会右旋90度,之前TX服务器会处理这种情况,貌似最近不干了,害客户抱怨我这里的问题。

然后开始研究服务器传回来照片的格式,发现exif里面Orientation的值为6,正好是右旋了90度,正好可以根据这个来自动调整图片。

首先读取exif值

$exif = exif_read_data($src, 0, true);

if($exif[‘IFD0’][‘Orientation’]==6){
flip($src,$src,-90);
}

其实还有其他方向,也可以根据这个来旋转,我这里只需要这么多。

另外附上flip方法

function flip($filename,$src,$degrees=90)
{
//读取图片
$data = @getimagesize($filename);
if($data==false)return false;
//读取旧图片
switch ($data[2]) {
case 1:
$src_f = imagecreatefromgif($filename);break;
case 2:
$src_f = imagecreatefromjpeg($filename);break;
case 3:
$src_f = imagecreatefrompng($filename);break;
}
if($src_f==””)return false;
$rotate = @imagerotate($src_f, $degrees,0);
if(!imagejpeg($rotate,$src,100))return false;
@imagedestroy($rotate);
return true;
}

 

万一你机器上面没有exif模块,参照我前一篇博文。

PHP的图片处理功能

在一个小项目里需要用到php的图像处理功能,才发现php通过imagefilter能够实现强大的功能。

imagefilter

(PHP 5)

imagefilter — Applies a filter to an image

Description

bool imagefilter ( resource $image , int $filtertype [, int $arg1 [, int $arg2 [, int $arg3[, int $arg4 ]]]] )

imagefilter() applies the given filter filtertype on the image.

Parameters

image
An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
filtertype
filtertype can be one of the following:

  • IMG_FILTER_NEGATE: Reverses all colors of the image.
  • IMG_FILTER_GRAYSCALE: Converts the image into grayscale.
  • IMG_FILTER_BRIGHTNESS: Changes the brightness of the image. Use arg1 to set the level of brightness.
  • IMG_FILTER_CONTRAST: Changes the contrast of the image. Use arg1 to set the level of contrast.
  • IMG_FILTER_COLORIZE: Like IMG_FILTER_GRAYSCALE, except you can specify the color. Use arg1arg2 andarg3 in the form of redgreenblue and arg4 for the alpha channel. The range for each color is 0 to 255.
  • IMG_FILTER_EDGEDETECT: Uses edge detection to highlight the edges in the image.
  • IMG_FILTER_EMBOSS: Embosses the image.
  • IMG_FILTER_GAUSSIAN_BLUR: Blurs the image using the Gaussian method.
  • IMG_FILTER_SELECTIVE_BLUR: Blurs the image.
  • IMG_FILTER_MEAN_REMOVAL: Uses mean removal to achieve a “sketchy” effect.
  • IMG_FILTER_SMOOTH: Makes the image smoother. Use arg1 to set the level of smoothness.
  • IMG_FILTER_PIXELATE: Applies pixelation effect to the image, use arg1 to set the block size and arg2 to set the pixelation effect mode.
arg1
  • IMG_FILTER_BRIGHTNESS: Brightness level.
  • IMG_FILTER_CONTRAST: Contrast level.
  • IMG_FILTER_COLORIZE: Value of red component.
  • IMG_FILTER_SMOOTH: Smoothness level.
  • IMG_FILTER_PIXELATE: Block size in pixels.
arg2
  • IMG_FILTER_COLORIZE: Value of green component.
  • IMG_FILTER_PIXELATE: Whether to use advanced pixelation effect or not (defaults to FALSE).
arg3
  • IMG_FILTER_COLORIZE: Value of blue component.
arg4
  • IMG_FILTER_COLORIZE: Alpha channel, A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.

Return Values

Returns TRUE on success or FALSE on failure.

我用到的几个功能

imagefilter($dst_r, IMG_FILTER_BRIGHTNESS, -80);

The documentation misses the exact meaning and valid ranges of the arguments for ImageFilter(). According to the 5.2.0 sources the arguments are:
IMG_FILTER_BRIGHTNESS
-255 = min brightness, 0 = no change, +255 = max brightness

IMG_FILTER_CONTRAST
-100 = max contrast, 0 = no change, +100 = min contrast (note the direction!)

IMG_FILTER_COLORIZE
Adds (subtracts) specified RGB values to each pixel. The valid range for each color is -255…+255, not 0…255. The correct order is red, green, blue.
-255 = min, 0 = no change, +255 = max
This has not much to do with IMG_FILTER_GRAYSCALE.

IMG_FILTER_SMOOTH
Applies a 9-cell convolution matrix where center pixel has the weight arg1 and others weight of 1.0. The result is normalized by dividing the sum with arg1 + 8.0 (sum of the matrix).
any float is accepted, large value (in practice: 2048 or more) = no change

ImageFilter seem to return false if the argument(s) are out of range for the chosen filter.

 

 

You must add imagesavealpha($im, true); so the alpha channel will be saved on the new image.

$im = imagecreatefrompng('image.png');
imagealphablending($im, false);

imagesavealpha($im, true);

if($im && imagefilter($im, IMG_FILTER_COLORIZE, 0,0,255,0)) {
    imagepng($im, 'image-new.png');
    imagedestroy($im);
}

【转载】java 时间戳跟PHP时间戳的转换

java 时间戳和PHP时间戳 的转换

原文地址:http://www.myexception.cn/php/1032678.html
总结一下java 时间戳和PHP时间戳 的转换问题:
由于精度不同,导致长度不一致,直接转换错误。
JAVA时间戳长度是13位,如:1294890876859
PHP时间戳长度是10位, 如:1294890859

主要最后三位的不同,JAVA时间戳在PHP中使用,去掉后三位,如:1294890876859-> 1294890876 结果:2011-01-13 11:54:36
echo date(‘Y-m-d H:i:s’,’1294890876′);
PHP时间戳在JAVA中使用,最后加三位,用000补充,如:1294890859->1294890859000
结果:2011-01-13 11:54:19
SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String dateTime = df.format(1294890859000L);
System.out.println(df);

php读取mp3文件信息 (转)

一个读取的程序。其实稍作改动,就可以变成读写的(id3v1),id3v2复杂的多,以后看情况再写吧。
<?php
$file=”回到我身边.mp3″; //要读取的文件–俺偶像的
$mp3info=readmp3($file); //获得文件信息,数组
print_r($mp3info); //输出数组
function readmp3($mp3_file)
{
$fp=fopen($mp3_file,”rb”); //读取mp3文件
//首先判断是否有TAG,如果没有,那就没必要读取了,方法就是读取倒数128-126字节,看是否是TAG
//详情参看http://www.readlog.cn/archives/2961/
fseek($fp,-128,SEEK_END); //指针移到倒数128字节处
$tag=fread($fp,3); //读取倒数128-126字节位置的数据
if($tag==”TAG”) //如果这3个字节是TAG,表明有TAG
{
$mp3=array();
//标题30个字节,从倒数125字节到倒数96字节
//现在直接读就可以了
$mp3[‘标题’]=fread($fp,30);
//艺术家30个字节,从倒数95字节到66字节
$mp3[‘艺术家’]=fread($fp,30);
//专辑30个字节,从倒数65字节到36字节
$mp3[‘专辑’]=fread($fp,30);
//年份4个字节,从倒数35字节到32字节
$mp3[‘年份’]=fread($fp,4);
//注释28个字节,从倒数31字节到4字节 (有的是30个字节,那就把倒数第2,3位归入注释了)
$mp3[‘注释’]=fread($fp,28);
fseek($fp,1,SEEK_CUR); //跳过倒数第3位保留位
//第几首1个字节,倒数第2位
$mp3[‘编号’]=ord(fread($fp,1));
//流派1个字节,就是倒数第一个字节了
//流派这里是存放的整型数据,可以写个函数来把数字变成具体的文字。
//具体含义参看http://www.readlog.cn/archives/2961/
$mp3[‘流派’]=ord(fread($fp,1));
return $mp3;
}
}
?>