Can't get ImageMagick run with php - php

I'm very frustrated with getting ImageMagick to run.
The Situation:
ImageMagick is installed on the Server. (My hosting provider did this. as a standard)
I asked my provider and they only said: the path to ImageMagick is /usr/bin/php_safemode and that i should "deposit" the path in my application. Whatever that means.
nothing works:
exec('convert SOME CODE') ;
doesn't work.
$im = new Imagick($image);
(these are no complete scripts, because not the scripts are the problem.. ) i have no idea how to test it. i mean ..no idea how to get it run. how to include this path in my script..
note: i have no access to php.ini or something like this.. my hosting provider told me that i can do it just include this path.. but how could this work?
Any ideas? tips?
i'm searching for an answer the whole day

Try this:
exec('/usr/bin/php_safemode/convert SOME CODE');

Related

Nothing has worked to code PHP in Visual Studio Code via Windows 10

I am taking a Udemy class for WordPress. I can create a function just fine. However, when I started using the variables and array it will not work. I get a message saying there is no "php.validate.executablePath" and "php.executablePath" via settings.json.
So I downloaded the zip file of PHP 7.3 (7.3.17) VC15 x64 Non Thread Safe and extracted it to C:\php7.3. I then added the path to the settings.json: "php.validate.executablePath": "C:\php7.3\php.exe", "php.executablePath": "C:\php7.3\php.exe"
I then restarted my VSC and nothing happened. I even restarted my computer. The notification never popped up again but I still cannot run PHP. In replace of the \, I used one \, then one /, then two / but it did not change anything. I looked this issue up - found others who had the same issue - and have not found anything that has worked for me so far.
I also tried downloading the xampp but there was an issue at port 443. That's when I downloaded the PHP file directly and uninstalled xampp.
EDIT: Do I have to have something like xampp or wamp to execute the PHP? If so, I would just need to figure out how to fix that error.
Here is a screenshot of the code I used: http://prntscr.com/sahb1f
Here is a screenshot of the settings.json: http://prntscr.com/sahbk0
I am on Windows 10 x64. A tool extension for VSC costs money, which I do not want to spend.
Any help?
Thanks!
You are missing the echo in front of the $names[0] array element access.
change your line to this <p>Hi, my name is <?php echo $names[0]; ?></p> and it should work however your error to do with executablePath might be because of something else.

how to convert with princeXML and PHP

I followed the directions on the readme file but I didn't get any errors nor any output or anything written to the log file.
so, what is the right way of converting and HTML file to PDF with prince, thank you in advance.
ini_set('display_errors', '1');
error_reporting(-1);
require 'prince.php';
$exepath='/Users/agk/Desktop/prince/bin/prince';
$prince= new Prince($exepath);
if(!$prince) die("Prince instantiation failed");
$prince->setHTML(TRUE);
$prince->setLog('prince.log');
$xmlPath='/Applications/MAMP/htdocs/test/newhtml.html';
$msgs= array();
$convert=$prince->convert_file($xmlPath, $msgs);
if(!$convert)
{
echo 'nothing converted';
var_dump($msgs);
}
I notice you are using MAMP - and this is where I hit the same issue - and here's how I fixed it.
I managed to find an answer elsewhere on SO relating to running shell commands via PHP on MAMP, so will share this.
The problem is that the DYLD_LIBRARY_PATH is set in MAMP, and the shell/shell_exec command in PHP is looking elsewhere.
As explained in the original post, this might not be the best fix but it has me up and running for now - and the alternative suggestions there didn't work for me.
So, try this:
Open the /Applications/MAMP/Library/bin/envvars file and comment the following lines as below:
#DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#export DYLD_LIBRARY_PATH
then restart Apache
It might mess up other MAMP settings, but I can live with that for now, as I need this to work locally on my Mac for testing before I load it all on a live Linux server.
Hope this helps.

PHP-Imagick on OSX, ImagickException: NoDecodeDelegateForThisImageFormat `/my-path/file.jpg' # error/constitute.c/ReadImage/550

My environment is: MacOSX 10.8.3, PHP 5.3.17, ImageMagick 6.8.0, Apache 2.2.22
I have installed ImageMagick and PHP-Imagick module by using macports.
ImageMagick works as expected, for example I can convert a JPG to GIF by using the console command convert /my-path/file.jpg file.gif.
However when I create a new Imagick instance by using a sample code as follow:
<?php
$m = new Imagick('/my-path/file.jpg');
I get the exception:
NoDecodeDelegateForThisImageFormat `/my-path/file.jpg' #
error/constitute.c/ReadImage/550
The format of the file doesnt change the result; I tested different JPG, GIF and PNG images however same exception occurs.
So I suspect, the PHP/Apache - ImageMagick integration part is somewhat faulty.
I saw this post on SO and applied the given resolution (adding the environment variable "MAGICKCODERMODULE_PATH" with the value "/opt/local/lib/ImageMagick-6.8.0/modules-Q16/coders") however that didn't make sense for my case. (I confirmed the existence of this environment variable from my phpinfo)
Outputs of some commands on my system:
identify -list format: http://pastebin.com/tiE7Jr1m
identify -list configure: http://pastebin.com/Zt8yiRhj
I will appreciate any suggestion.
Solution
chmod 755 /opt
Insight and Thoughts
At last. I managed to solve the problem.
As there were no answers and suggestions on SO; I'm not suprised that something not expected was causing it!..
#sathia suggested to define an environment variable to call ImageMagick binaries. However in my case, I need to use phpImagick extension and access ImageMagick through its API, so this suggestion didn't make sense.
But I wanted to give it a try and call ImageMagick's convert binary by using PHP's system function. After calling it, I got a return value of "126"; and 126 means, binary is found however it's not executable!..
I checked the binary in /opt/local/bin/convert and it has appropriate execute permissions, then I checked /opt/local/bin and /opt/local directories as well and they were executable too. However the catch was /opt folder's permission was 700!
I managed to execute chmod 755 /opt and magically error has gone.
I've been there too, this is how I solved it:
<?php
define("IMAGE_MAGICK_PATH","/usr/local/bin/");
$in = '/my-path/file.jpg';
$out = '/my-path/file.gif';
$convertString = IMAGE_MAGICK_PATH." convert ".$yourfile." ".$out;
exec($convertString);
hope it helps

Bug with the PHP exec() function for an ADA program

I really need some help for a problem.
I'm trying to use the exec() PHP function which is directly integrated on PHP, but it doesn't work.
I've tried this :
$directory = "C:/wamp/www/ADA-WEB/Conversion";
$file ="/main.exe";
chmod($directory, 0600);
exec($directory.$file);
But nothing goes on. So, i've tried to see if an error was reported on the Apache Log, and this is what appears :
raised ADA.IO_EXCEPTIONS.NAME_ERROR : convertir\EXPORT.DAT: No such file or directory
It's a typical error that ADA may raise.
But i don't understand why this error is generated. It seems that PHP runs the file on a random folder (perhaps a temp one).
When i launch directly the exe on windows i don't have this kind of problem.
If you should help...
Thanks a lot.
Nicolas
You could consider using http://php.net/manual/fr/function.chdir.php before launching your ada application. This way you can control what is the current directory used when running your binary
$directory = "C:/wamp/www/ADA-WEB/Conversion";
$file ="/main.exe";
$current = getcwd();
chdir($direcoty);
chmod($directory, 0600);
exec($directory.$file);
chdir($current);
Well, i've changed my mind, going on another direction..
After more searches about this problem it really seems that the problem is not because of the exec() function but because Windows doesn't allow a program to be executed through a navigator.
Thanks for your answers.
Nicolas
There may be alternative approaches to the problem. that sidestep the limitation (apparently with Windows) that you are facing.
If you're driving an Ada program through a web-based interface, the Ada Web Server (AWS) toolkit is a way to build a webserver right into your program - it's easy to use, and fast too. Apparently there is some other toolkit called AWS, but the one I mean is here: with an introduction here. See also this StackOverflow question for some other views on the toolkit.

Add AV Scan to File Upload Site

i am not sure whether this is in the right section or not but i am building an file upload site and want to be able to scan the files on upload for viruses etc.. How would i be able to do this?
Any ideas to get me started?
Thanks
The clamav library has a PHP binding called php-clamav. You then can scan files for viruses from within your PHP code:
if ($_FILES['file']['size'] == 0 || !is_file($_FILES['file']['tmp_name']))
{
throw new Exception('Please select a file for upload!');
} else {
cl_setlimits(5, 1000, 200, 0, 10485760);
if ($malware = cl_scanfile($_FILES['file']['tmp_name']))
throw new Exception($malware.'(ClamAV version: '.clam_get_version(),')');
}
...
Another alternative is to install the Mod_Security web application firewall. It can be configured to scan all upload files for viruses using modsec-clamscan.
You could try something like the following using AVG:
Windows:
<?php
exec("avgscanx.exe /SCAN=filename.ext/");
$result = exec("echo %ERRORLEVEL%");
?>
Linux:
<?php
exec("avgscan filename.ext -a -H -c");
$result = exec("echo $?");
?>
Both platforms return the same error codes, allowing you to determine whether a scan was successful or not.
References:
http://www.avg.com/ww-en/faq.num-4443
http://www.avg.com/ww-en/faq.num-4441
http://www.avg.com/ww-en/faq.num-1854
http://www.avg.com/ww-en/faq.num-1759
It depends on your server configuration, but for example on linux, it's easy to install something like clam and access it through the command line. You can use something like php's exec() to run it.
You could also use VirusTotals public API. You can read more about it here. There is some PHP code available here.
This way you get a lot of scanners, and you don't have to run AV locally. On the other hand you'll have to wait a while for the result.

Categories