I encountered the similar problem today. The cause of the problem was a newer version of libjpeg. And the solution that worked was building php bottle from source.
Check available version of libjpeg:
$brew info libjpeg
jpeg: stable 9b (bottled)
Image manipulation library
http://www.ijg.org
/usr/local/Cellar/jpeg/9b (20 files, 724KB) *
Poured from bottle on 2017-08-07 at 12:06:42
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/jpeg.rb
Install php from source:
brew install --build-from-source php56
Update:
If you have already installed php you have to uninstall it prior to building from source. It also applies to extensions.
As proposed by @jirson-tavera in the homebrew-php repo issue, this can be fixed without overriding your brew installation:
wget -c http://www.ijg.org/files/jpegsrc.v8d.tar.gz
tar xzf jpegsrc.v8d.tar.gz
cd jpeg-8d
./configure
make
cp ./.libs/libjpeg.8.dylib /usr/local/opt/jpeg/lib
I am sharing this as I had the same issue when using PHP5.4. Originally, I had PHP5.6 and it was working fine. Then I downgraded to php5.4 as my app only supports PHP5.4.
Then this error came up. I noticed that I have libjpeg.9.dylib and PHP5.4 is looking for libjpeg.8.dylib.
As @lifecom notes, this is fixed with homebrew's php70 update, but you might still run into the problem if brew upgrade is trying to update other packages before upgrading php70.
Fix this by manually updating php70 first with brew upgrade php70 and you then should be able to run brew upgrade no problem (or, well, at least without this problem).
Even though the solution in the accepted answer works, it's not the right way to fix the problem. It violates brew's metadata integrity.
Problem
The issue is that Homebrew's jpeg formula has been upgraded to v9 but the existing "bottled" PHP formula is still built and linked against the previous version, v8, which is no longer exists on your system.
You have a few options to fix the issue.
1. Recompile phpxx formula from source (highly recommended)
Uninstall your php formula, and rebuild it from the source instead of using the bottled version. This way, php will use and link against the currently installed version of jpeg. Assuming that you're dealing with php71:
brew reinstall php71 --build-from-source
2. Downgrade jpeg formula the right way (preferred over recompiling it manually)
If you haven't run brew cleanup, you already got the previous jpeg version in your brew's cellar, switch to it:
brew switch jpeg 8d
If you get a jpeg does not have a version "8d" in the Cellar. error, you need to first restore it by reverting the history:
You can find out the commit hash by using brew log jpeg and going through the commit messages.
The downside is that there might be other formulas that require the newer version to work properly, e.g. imagemagick. If you face such incompatibility issues, check the first solution above.
3. Downgrade jpeg by manually recompiling (not recommended)
Fetch the source, compile and overwrite brew's version. Refer to Denis' answer for details.
This is not recommended because it violates the integrity of your brew metadata. Brew thinks that it has the 8b version, however, you manually compiled 9b and overwrote the files.
4. Manually symlink the old version (seriously?)
Do not manually symlink the leftover libjpeg.8.dylib. If the file is there, you can just brew switch to it as mentioned in the second solution above.
I was getting a similar problem trying to upgrade composer:
dyld: Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.8.dylib
Referenced from: /usr/local/bin/php
Reason: image not found
composer: PHP Phar support is required for this formula
Error: An unsatisfied requirement failed this build.
Problem was php couldn't find libjpeg library anymore. One way to fix this is to reinstall php and libjpeg. Here's how I reinstalled them:
# find out which version of php is installed
brew list | grep php
# uninstall php
brew uninstall php70 --ignore-dependencies
# uninstall libjpeg
brew uninstall libjpeg --ignore-dependencies
# install libjpeg again
brew install libjpeg
# install php again
brew install php70