user3143218
user3143218

Reputation: 1816

Running commandline applications with PHP

How can I successfully run commandline applications from PHP on a linux/Mac system.

I'm using a mac and trying to run optipng. I've downloaded optipng from here

http://sourceforge.net/projects/optipng/files/OptiPNG/optipng-0.7.5/optipng-0.7.5.tar.gz/download?use_mirror=softlayer-sng&download=

After downloading it, I installed it via the command line by CD'ing to the download folder, and installing it accordingly:

cd path/to/optipng
sudo ./configure
sudo make install

I can now run it and compress images like this:

optipng path/to/example/file.png

Everything works fine so it's installed and working on my system.

I want to run it via a php page so i try this:

<?php
echo "origional size =". filesize(path/to/example/file.png)."\n";

$compress = shell_exec('optipng path/to/example/file.png');

echo "<pre>$compress</pre>";

echo "new size =". filesize(path/to/example/file.png)."\n";

?>

But it does nothing. What am I missing here? How do I use shell_exec() or exec() successfully?

Upvotes: 0

Views: 263

Answers (1)

Rajesh Ujade
Rajesh Ujade

Reputation: 2735

<?php
exec('/dev/null > /tmp/optipng.log');
echo "origional size =". filesize('/tmp/steve-jobs.png')."\n";

shell_exec('/usr/local/bin/optipng -log /tmp/optipng.log /tmp/steve-jobs.png');
$compress = file_get_contents('/tmp/optipng.log');

echo "<pre>$compress</pre>";

echo "new size =". filesize('/tmp/steve-jobs.png')."\n";

?>

Upvotes: 1

Related Questions