1. PHP7.1.8新特性概述
在升级到PHP7.1.8之前,了解其新特性有助于更好地利用新版本的优势。以下是PHP7.1.8的一些主要新特性:
- 性能提升:PHP7.1.8在性能方面进行了优化,包括更快的脚本执行速度和内存使用效率。
- 新特性:引入了诸如Null Coalescing Operator(??)、Intersection Type(交集类型)等新特性。
- 安全改进:增强了安全性,修复了多个安全漏洞。
2. 安装PHP7.1.8
以下是安装PHP7.1.8的步骤:
- GCC编译器
- Make
- Autoconf
- Automake
- Libtool
- Zlib
- Bzip2
- OpenSSL
- GD
- IMAP
- CURL
- MCrypt
- MBString
- PCRE
获取PHP7.1.8源码:从下载PHP7.1.8源码包。
安装依赖:确保系统已安装以下依赖:
编译安装:
./configure --prefix=/usr/local/php7.1.8 --with-config-file-path=/usr/local/php7.1.8/etc \
--enable-fpm --enable-mbstring --enable-zlib --with-gd --with-openssl \
--with-imap --with-imap-ssl --with-curl --with-mysql --with-pdo-mysql \
--enable-xml --enable-dom --enable-simplexml --enable-xmlreader --enable-xmlwriter \
--enable-json --enable-bcmath --enable-ftp --enable-phar --enable-pdo
make
make install
- 配置环境变量:
export PATH=/usr/local/php7.1.8/bin:$PATH
export PHP_HOME=/usr/local/php7.1.8
export LD_LIBRARY_PATH=$PHP_HOME/lib
- 复制默认配置文件到指定目录:
- 在
php.ini
中设置extension_dir
为扩展模块的安装路径。 - 启用Imagick扩展:
配置PHP配置文件:
cp /usr/local/php7.1.8/etc/php-fpm.conf.default /usr/local/php7.1.8/etc/php-fpm.conf
cp /usr/local/php7.1.8/etc/php.ini.default /usr/local/php7.1.8/etc/php.ini
修改配置文件:
extension=imagick
启动PHP-FPM:
/usr/local/php7.1.8/sbin/php-fpm
3. 配置Imagick扩展
Imagick扩展是PHP中处理图像的强大工具。以下是如何配置Imagick扩展的步骤:
- 对于Ubuntu系统,使用以下命令安装:
- 对于CentOS系统,使用以下命令安装:
- 重新运行
./configure
命令,并添加--with-imagick
参数:
安装Imagick库:
sudo apt-get install libmagickwand-dev
sudo yum install ImageMagick-devel
编译PHP:
./configure --prefix=/usr/local/php7.1.8 --with-config-file-path=/usr/local/php7.1.8/etc \
--enable-fpm --enable-mbstring --enable-zlib --with-gd --with-openssl \
--with-imap --with-imap-ssl --with-curl --with-mysql --with-pdo-mysql \
--enable-xml --enable-dom --enable-simplexml --enable-xmlreader --enable-xmlwriter \
--enable-json --enable-bcmath --enable-ftp --enable-phar --enable-pdo --with-imagick
make
make install
重启PHP-FPM:
/usr/local/php7.1.8/sbin/php-fpm restart
4. 使用Imagick扩展
以下是一个使用Imagick扩展的基本示例:
<?php
$image = new Imagick('example.jpg');
$image->resizeImage(100, 100, Imagick::FILTER_LANCZOS, 1);
$image->writeImage('resized.jpg');
$image->clear();
$image->destroy();
?>