Imagemagic and php exec: convert: no images defined - php

The command works just fine from the shell
/opt/ImageMagick/bin/convert /private/var/folders/nl/9cky1krj5_j7zwm34tfkndd40000gn/T/pdfPyflQF /private/var/folders/nl/9cky1krj5_j7zwm34tfkndd40000gn/T/imgRilIdW 2>&1
I got image generated. But if I run it with php exec function I get error
Array
(
[0] => convert: no images defined `/private/var/folders/nl/9cky1krj5_j7zwm34tfkndd40000gn/T/imgRilIdW' # error/convert.c/ConvertImageCommand/3212.
)
Seems it's not permission issue. File permissions are 666. Can't figure out what is wrong.

Usually error "convert: no images defined" is coming when convert couldn't find gs tool.
I had the same problem and solved it with defining PATH environment before doing shell_exec like this:
putenv('PATH=/usr/local/bin:/usr/bin:/bin:/opt/local/bin/');
Because gs tool is located in the /opt/local/bin.

I can not see a file extension on the input or output images.
It would probably be worth enclosing your image path in " " and what actual code are you using in exec()

Related

Chroma key in php using ImageMagic extension for php

Im trying to use the ImageMagick library in order to chorma key an image programmaticly in php. So far i've installed the ImageMagick extension onto my server and made sure it is working using some simple commands in the terminal itself. My goal is to get this working as php code but currently im just trying to find a script that works for me. My problem is that I cant get the greenscreen script from fred's imagemagick scripts to work. when trying to excute the scripti get a file does not exist or is not an ordinary file not readable or has zero size error.
The same is true when I try to use any of the imagemagick/fred's scripts on php.
I've tried:
In php:
giving everything 777 permissions
specifying full paths from the root directory of the server
chagning the temp dir within the script itself
In the terminal:
works with regular scripts though didn't find a script the does real life chroma key
can't make it use freds scripts
Update: Got the Imagemagick fred's script to work in the terminal though the image is returned in black and white. currently working on getting it to work as php code using exec()
Update 2: using this exec on my php
exec("/bin/bash /home/full_path_within_server/public_html/imagemagictest/greenscreen.sh shirt.jpg t.png",$out,$returnval);
I get the following error message: "FILE shirt.jpg DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAS ZERO"
Found a solution, change the all instances of the convert in the script to the full path of the convert function.

PHP exec() with Pygments for PHP

I'm currently using the Pygments for PHP plugin that is located here: http://derek.simkowiak.net/pygments-for-php/.
The line that actually calls Pygments from that code is an exec() passed: pygmentize -f html $extra_opts -l $language $temp_name as the command. This all works fine, and I get back the output and it is formatted by the plugin.
What I would like to happen at the same time is for Pygments to create an image of it, so I pass exec() a similar command: pygmentize -f png $extra_opts -l $language -o $full_image_path/$output_file.png $temp_name This is where I run into a problem. The image never shows up in the expected folder.
However, if I var_dump() that command string before I exec() it and take it and run it straight from the command line, it works fine.
I have tried echoing exec('whoami') which tells me that the PHP user is www-data. I've tried giving permissions to www-data and changing ownership to www-data on the folder where I store the images. I've also tried changing permissions to 777 just to see what would happen, and the answer is nothing.
Is there something I'm missing? I'm running out of ideas to try. Thank you!
Edit: Another thing that I've checked is the output from the exec command, and the return value. It outputs an empty array, and it returns 1 as the return value.
Edit 2: After seeing that that directory should be writeable/readable for the PHP user, is it possible that pygments doesn't have permission to write it as a specific user? I'm not sure this makes sense, as when I run it myself it works fine, and in fact, when PHP runs it with the HTML lexer, it is able to run. I'm not very experienced in Python, so I don't know if this is a potential issue.
I guess you cannot do it like this.
$output_file.png
Try
$file = $output_file.".png"
and substitute in the exec
Ended up being an issue with the font that was installed for use by the www-root user. Apparently the one that is used by default for Pygments was installed only for the user that I was running as when I use the command line.
The way I was able to figure this out, was running
exec("$command 2>&1", $out, $code);.
The extra 2>&1 redirects stderr into the output for me to see the issue.
The $out parameter showed the FontNotFound error that pygments was throwing.
I changed the font that Pygments used via the command line using: full,style=manni,cssclass=pygmentize_kbOKBd,font_name='DejaVu Sans Mono' -l php -o /srv/www/path/to/images/uploads/2513732976ad4b7.02729290.png /tmp/pygmentize_kbOKBd after finding which fonts I had available to me.
To see which fonts I had available to me as the user running the script, I just ran fc-list in an exec() command for Ubuntu, and checked the output of that for the list of available fonts.

Exec command doesn't work as expected

I'm trying to launch a CLI command from a PHP script:
in particular I wanna use this command convert a.png a.tif to convert an image to tiff.
When I launch this command from CLI it works as expected but if I launch from a PHP script with the following code it doesn't create any tiff image in my folder:
$exec = "convert a.png a.tif";
exec($exec,$yaks,$err);
echo "<pre>";
print_r($yaks);
echo "$err";
echo "</pre>";
Moreover $yaks is empty and $err is set to 127.
I'm not an expert, why this doesn't work as expected?
Best regards
UPDATE
I used this command instead $exec = "convert 4.png 4.tif 2>&1"; and I got in return [0] => sh: convert: command not found
This seems to me strange since I can use it from CLI!
FINAL UPDATE
Thanks a lot guys!
$exec = "/usr/local/bin/convert a.png a.tif";
This command solved the problem!
You're great.
you should enter fullpath to "convert" and may be files.
err 127 - file not found
It looks like the 'convert' binary isn't in any of the directories on the PATH PHP is using. You could try using the full path, e.g. /opt/local/bin/convert or whatever the path is.
You could also modify the PATH used by PHP (but I don't know how).
The PHP script probably doesn't know where to find these things you're referring to in the exec command. When you run this from the command line, the shell will look for them in the directory you are in at that point in time; but when you run it from PHP, it probably defaults to the PHP dir and not the specific dir in which your files are. So write out the full path.

How do I fix this error with ImageMagick?

I'm using ImageMagick to convert a RAW file (*.nef extension) into a JPEG image. I'm actually doing this from within PHP, but not using the IMagick extension (I spent too much time banging my head against a wall trying to get that working that eventually I just gave up). Instead, I'm using PHP's exec() function to call ImageMagick from the command line and just do the manipulations there.
Anyways, I wrote a simple PHP script to do this -- to convert the NEF image into a JPEG image -- and it worked! I ran it through a number of tests. When the JPEG didn't exist, it created it. When the JPEG already did exist, it overwrote it. Perfect!
Until today. I made that script last Friday. Today (Monday), when it tried to use it, it stopped working. I literally changed nothing about it, but a couple days later it's failing all the same tests that I ran on Friday. I can retrieve the output from the command line, and these are the errors it's encountering this time around:
Magick: `%s' (%d) dcraw.exe -4 -w -O "C:/Windows/TEMP/magick-iDvVnHw-.ppm" "C:/Windows/TEMP/magick-hYeYwWRd" # error/utility.c/SystemCommand/2094.
Magick: delegate failed `dcraw.exe -4 -w -O "%u.ppm" "%i"' # error/delegate.c/InvokeDelegate/1058.
Magick: unable to open image `C:/Windows/TEMP/magick-iDvVnHw-.ppm': No such file or directory # error/blob.c/OpenBlob/2588.
Magick: missing an image filename `E:\test.jpg' # error/convert.c/ConvertImageCommand/3015.
These errors didn't show up on Friday. Why are they showing up now? What do they mean? And what can I do about them? Thanks in advance.
I had a similar error on linux:
No such file or directory # error/blob.c/OpenBlob/2709. convert: no images defined `/tmp/transform_5c88983-1.jpg' # error/convert.c/ConvertImageComman
The problem was a special character in my german picturename:
images/1/14/Ministerium-für-Bildung.jpg
I went into the folder and created a symlink without the character:
ln -s Ministerium-für-Bildung.jpg Ministerium-fr-Bildung.jpg
Well, I was able to solve it. I did two things, and I'm not entirely sure that the first thing had any impact or not, but I'll mention both for anyone else struggling. First, since it seemed to be having trouble with a temp file, I changed the Windows security/permissions on the C:\Windows\TEMP folder to explicitly allow IIS_USER full control, as well as Everyone.
But secondly, and more importantly, I changed the command I was passing into exec(). Previously my command looked something like this:
$cmd = 'C: & "C:\\Program Files (x86)\\ImageMagick\\convert.exe" ';
$cmd .= '"E:\\test.nef" "E:\\test.jpg" 2>&1';
exec($cmd, $output, $return);
So instead I specified a new "temp" directory, and set the current path, like so:
$cmd = 'SET MAGICK_TMPDIR=E:\\Temp&SET path=C:\\Program Files (x86)\\;%path%& ';
$cmd .= '"C:\\Program Files (x86)\\ImageMagick\\convert.exe" ';
$cmd .= '"E:\\test.nef" "E:\\test.jpg" 2>&1';
exec($cmd, $output, $return);
And that did the trick! The script is working again.

Creating a PHP script from BASH code to run on Windows

I have this line of code:
convert 1234_Page_1_....png 1234_Page_2_....png output.pdf
This merges the listed pngs to a single pdf (using ImageMagick). I have a bunch of PNG files in this format. I would like to create a loop in PHP to run through the convert/merge action on all the files that I have. For all the files that have the same number before the "Page", I would like them to be merged to one PDF with that same number as a name. Sometimes there are more than two pages to convert.
For this example, I would like "1234_Page_1_....png" and "1234_Page_2_....png" to result as 1234.pdf. And I would like files "1235_Page_1_....png" and "1235_Page_2_....png" to result as 1235.pdf, and so on.
Here's what I've been told is the BASH way to handle the problem:
for i in `seq 1234 1350` ; do convert ${i}_Page_*.png ${i}_output.pdf ; done
I would like to have this done in a PHP script that I can run on Windows.
Thanks in advance,
Jake
The reason you're getting that error message is that you edited the script using a Windows editor and then tried to run it in a Unix-type environment. Run dos2unix scriptname and the Windows line endings will be converted to Unix line endings. No more $'\r' errors. Also, in Bash use for i in {1234..1350} - there's no need to use seq.

Categories