php扩展
大约 5 分钟
php扩展
JSON扩展
json_decode
对JSON格式的字符串进行解码
接受一个JSON编码的字符串并且把它转换为PHP变量
此函数仅能处理UTF-8编码的数组
mixed json_decode(string $json [, bool $assoc=false [, int $depth = 512 [, int $options=0]]])
json_encode
对变量进行JSON编码
等编码的value,除了resource类型之外,可以为任何数据类型
所有字符串数据的编码必须是UTF-8
string json_encode(mixed $value [, int $options=0 [, int $depth=521]])
Zlib扩展
$filename = tempnam('/tmp', 'zlib') . 'gz';
$content = 'Hello World!';
// 以最大压缩比进行写入的方式打开一个文件
$zp = gzopen($filename, 'w9');
// 写入字符串到文件中
gzwrite($zp, $content);
// 关闭文件
gzclose($zp);
// 以读的方式打开一个文件
$zp = gzopen($filename, 'r');
// 读到3个字符
echo gzread($zp, 3);
// 输出所有剩余的字符串
gzpassthru($zp);
// 关闭文件
gzclose($zp);
// 删除文件
unlink($filename);
PDO扩展
PDO数据对象是(PHP DATA Object)的缩写。
连接
DSN数据结构: http://php.net/manual/zh/ref.pdo-mysql.connection.php
创建一个表示数据库连接的PDO实例
创建一个表示连接到请求数据库的数据库连接PDO实例
PDO::__construct(string $dsn [, string $username [, string $password [, array $driver_options]]])
$dsn = 'mysql:host=localhost;dbname=py_news';
$username = 'root';
$password = '123456';
$dbh = new PDO($dsn, $username, $password);
// 解决乱码问题
$dbh->query('set names utf8');
$result = $dbh->query('select * from articles limit 1');
var_dump($result->fetchAll(PDO::FETCH_ASSOC));
执行语句query
执行一条sql语句,返回PDOStatement对象,可以理解为结果集
public PDOStatement PDO::query(string $statement)
执行语句exec
执行一个SQL语句,并返回受影响的行数
int PDO::exec(string $statement)
预处理prepare,执行execute
在执行语句前预处理SQL语句,并返回结果集
public PDOStatement PDO::prepare(string $statement [, array $driver_options = array()])
$dsn = 'mysql:host=localhost;dbname=py_news';
$username = 'root';
$password = '123456';
$dbh = new PDO($dsn, $username, $password);
// 解决乱码问题
$dbh->query('set names utf8');
$sth = $dbh->prepare('select * from articles where id >= ? and id <= ? limit 10');
$sth->execute([2,3]);
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
$sth->execute([5,6]);
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
事务处理beginTransaction
启动一个事务
关闭自动提交模式。 自动提交模式被关闭的同时,通过PDO对象实例对数据库做出的更改直到调用PDO::commit()结束,事务才被提交。 调用PDO::rollBack()将回滚对数据库做出的更改并将数据库连接返回到自动提交模式。
包括MySQL在内的一些数据库,当发出一条类似DROP TABLE或CREATE TABLE这样的DDL语句时, 会自动进行一个隐式地事务提交。隐式地提交将阴止你在此事务范围内回滚任何其他更改。
bool PDO::beginTransaction(void)
$dsn = 'mysql:host=localhost;dbname=py_news';
$username = 'root';
$password = '123456';
$dbh = new PDO($dsn, $username, $password);
// 解决乱码问题
$dbh->query('set names utf8');
try{
$dbh->beginTransaction();
$dbh->exec('update articles set name="Hello World!" where id = 2');
// throw new PDOException();
$dbh->commit();
}catch(PDOException $e){
echo $e->getCode();
$dbh->rollBack();
}
$sth = $dbh->prepare('select * from articles where id >= ? and id <= ? limit 10');
$sth->execute([2,2]);
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);
GD扩展
使用PHP创建PNG图像
// 载入带 alpha 通道的 png 图像
$png = imagecreatefrompng('http://d.lanrentuku.com/down/png/1601/20yuanxingyinyingtubiao/light.png');
// 关闭 alpha 渲染并设置 alpha 标志
imagealphablending($png, false);
imagesavealpha($png, true);
// 输出图像到浏览器
header('Content-Type: image/png');
$string = 'Hello World!';
$orange = imagecolorallocate($png, 220, 210, 60);
$px = (imagesx($png) - 6.5 * strlen($string)) / 2;
imagestring($png, 3, $px, 15, $string, $orange);
imagepng($png);
imagedestroy($png);
使用Alpha通道为图像加水印
// 封面展示图
$frontImage = imagecreatefrompng('http://d.lanrentuku.com/down/png/1712/10shengdanjie-pngtubiao/mistle_toe.png');
// 水印图
$waterImage = imagecreatefrompng('http://d.lanrentuku.com/down/png/1007/comma_face/comma_face_16.png');
// 设定图像的混色模式
imagealphablending($frontImage, false);
// 保存完整的alpha通道信息
imagesavealpha($frontImage, true);
// 设定图像的混色模式
imagealphablending($waterImage, false);
// 保存完整的alpha通道信息
imagesavealpha($waterImage, true);
// 背景图,创建一个真彩色图像
$backImage = imagecreatetruecolor(imagesx($frontImage), imagesy($frontImage));
// 为一幅图像分配颜色+alpha通道
$whiteColor = imagecolorallocatealpha($backImage, 255, 255, 255, 0);
// 区域填充
imagefill($backImage, 0, 0, $whiteColor);
// 设置水印图像的外边距,并且获取水印图像的尺寸
$marge_right = 110;
$marge_bottom = 110;
$sx = imagesx($waterImage);
$sy = imagesy($waterImage);
imagecopy($backImage, $frontImage, 0, 0, 0, 0, imagesx($frontImage), imagesy($frontImage));
// 利用图像的宽度和水印的外边距计算位置,并且将水印复制到图像上
imagecopy($backImage, $waterImage, imagesx($backImage) - $sx - $marge_right, imagesy($backImage) - $sy - $marge_bottom, 0, 0, imagesx($waterImage), imagesy($waterImage));
// 输出图像并释放内存
header('Content-type:image/png');
imagepng($backImage);
imagedestroy($backImage);
使用imagecopymerge_alpha函数创建半透明水印
function imagecopymerge_alpha2($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
if(!isset($pct)){
return false;
}
$pct /= 100;
// Get image width and height
$w = imagesx( $src_im );
$h = imagesy( $src_im );
// Turn alpha blending off
imagealphablending( $src_im, false );
// Find the most opaque pixel in the image (the one with the smallest alpha value)
$minalpha = 127;
for( $x = 0; $x < $w; $x++ )
for( $y = 0; $y < $h; $y++ ){
$alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF;
if( $alpha < $minalpha ){
$minalpha = $alpha;
}
}
//loop through image pixels and modify alpha for each
for( $x = 0; $x < $w; $x++ ){
for( $y = 0; $y < $h; $y++ ){
//get current alpha value (represents the TANSPARENCY!)
$colorxy = imagecolorat( $src_im, $x, $y );
$alpha = ( $colorxy >> 24 ) & 0xFF;
//calculate new alpha
if( $minalpha !== 127 ){
$alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha );
} else {
$alpha += 127 * $pct;
}
//get the color index with new alpha
$alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha );
//set pixel with the new color + opacity
if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){
return false;
}
}
}
// The image copy
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
}
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
// creating a cut resource
$cut = imagecreatetruecolor($src_w, $src_h);
// copying relevant section from background to the cut resource
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
// copying relevant section from watermark to the cut resource
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
// insert cut resource to destination image
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
}
// 封面展示图
$frontImage = imagecreatefrompng('http://d.lanrentuku.com/down/png/1712/10shengdanjie-pngtubiao/mistle_toe.png');
// 水印图
$waterImage = imagecreatefrompng('http://d.lanrentuku.com/down/png/1007/comma_face/comma_face_16.png');
// 设定图像的混色模式
imagealphablending($frontImage, false);
// 保存完整的alpha通道信息
imagesavealpha($frontImage, true);
// 设定图像的混色模式
imagealphablending($waterImage, false);
// 保存完整的alpha通道信息
imagesavealpha($waterImage, true);
// 背景图,创建一个真彩色图像
$backImage = imagecreatetruecolor(imagesx($frontImage), imagesy($frontImage));
// 为一幅图像分配颜色+alpha通道
$whiteColor = imagecolorallocatealpha($backImage, 255, 255, 255, 0);
// 区域填充
imagefill($backImage, 0, 0, $whiteColor);
// 设置水印图像的外边距,并且获取水印图像的尺寸
$marge_right = 110;
$marge_bottom = 110;
$sx = imagesx($waterImage);
$sy = imagesy($waterImage);
imagecopymerge_alpha($backImage, $frontImage, 0, 0, 0, 0, imagesx($frontImage), imagesy($frontImage), 50);
// 利用图像的宽度和水印的外边距计算位置,并且将水印复制到图像上
imagecopymerge_alpha($backImage, $waterImage, imagesx($backImage) - $sx - $marge_right, imagesy($backImage) - $sy - $marge_bottom, 0, 0, imagesx($waterImage), imagesy($waterImage), 50);
// 输出图像并释放内存
header('Content-type:image/png');
imagepng($backImage);
imagedestroy($backImage);