macos缺少freetype终极解决方案

时间:2022-12-29 15:20:35

最近在用 thinkphp,用到验证码模块时,验证码无法正确显示,log 中错误信息如下:

Call to undefined function think\captcha\imagettftext()[/Users/leif/web/login-test/vendor/topthink/think-captcha/src/Captcha.php:191]

大致意思就是没有 imagettftext()这个函数,这是因为缺少 php 的 gd 库中的 freetype 模块。

网上的解决方案大致有这么几种:

但这些方案都比较老了,无法解决 macos 10.11和 macos 10.12的问题,
因为macos 在2015年下半年修改了安全策略,如果你看了 wwdc 2015的话可能会想起来,或者可以点这查看回顾

如果你只是想快点解决问题的话,我就给你简单解释一下这个安全策略,这种策略叫做Rootless机制,就是 root用户也无法操作某些系统关键部位,例如php 的目录/usr/bin,这就导致以上三种方式根本行不通,他们无法修改 php的默认指向,现我给出两种解决途径:

  • 使用第三方集成环境,推荐 mamp和 mamp pro,xampp 也可以
  • 关闭Rootless机制,然后自己配置环境

我采用了第二种方式,(ps,有点追求就得采用第二种方式吧。。。)

关闭Rootless机制的方法为:

//重启按住 Command+R,进入恢复模式,打开Terminal。

csrutil disable

修改好了之后还可以选择再次打开,打开的方式为:

//重启按住 Command+R,进入恢复模式,打开Terminal。

csrutil enable

好,现在你已经成功的把Rootless机制给关掉了,然后就可以从以下三种方式中选择一种了

我选择的是第二种,
方法为:

brew install php56 --with-apche --with-freetype

这里的 php56,也可以换成你想要的版本,后边的插件也可以自己随便换,但是一定要有--with-freetype(这不废话么,,,老子就是想装这个的。。。)使用方法也很简单,就不赘述了,百度搜索 “brew 安装php”有很多教程

最重要的一点一定要记住,在安装完成后,会出现一段文字,内容大致为

To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so

The php.ini file can be found in:
/usr/local/etc/php/5.6/php.ini

✩✩✩✩ Extensions ✩✩✩✩

If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

PATH="/usr/local/bin:$PATH"

PHP56 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell's equivalent configuration file:

export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"

✩✩✩✩ FPM ✩✩✩✩

To launch php-fpm on startup:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

The control script is located at /usr/local/opt/php56/sbin/php56-fpm

OS X 10.8 and newer come with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:

PATH="/usr/local/sbin:$PATH"

You may also need to edit the plist to use the correct "UserName".

Please note that the plist was called 'homebrew-php.josegonzalez.php56.plist' in old versions
of this formula.

With the release of macOS Sierra the Apache module is now not built by default. If you want to build it on your system
you have to install php with the --with-apache option. See brew options php56 for more details.

To have launchd start homebrew/php/php56 now and restart at login:
brew services start homebrew/php/php56
==> Summary
/usr/local/Cellar/php56/5.6.27_4: 332 files, 48.7M, built in 7 minutes 27 seconds`

这段话什么意思我就不解释了,初中英语水平就能看懂,一定要把这里边要求配置的配置好。


再来说一下其他两种方案,自己编译太费劲我就没试,理论上是可以的,我在 linux 下这么搞过,

再就是那个一句话脚本,因为文件存在了亚马逊云,,,我果断放弃了,,后来我试了一下,,,,半个小时都没下载完,,,(没*),也因为平时习惯用 brew,所以用了 brew的方式,

brew 可以很方便的配置多个版本的 php,配置方法可以看我的博文: