jrlooney Posted February 7, 2008 Share Posted February 7, 2008 Ok, so here is what I am trying to do. I have this old Perl script which I cannot get rid of because it is part of a large content management system. It has an image uploader which currently uploads just about anything. I want to take advantage of OSX's "sips" command to resample the image and generate thumbnails. This is just a unix command, so I should be able to use one of the functions in the title above. However it doesn't work. In Perl, there are also system() and bactick operators. So, in the Perl code, right after the image is uploaded, I add a call to a php script: `/usr/bin/php /Users/php/acuweb_helpers/create_thumb.php $path $thumb_path`; $path is the path to the image $thumb_path is where I want to write the thumbnail file to Now, the php script looks like this: <?php $path = $argv[1]; $thumb_path = $argv[2]; exec("/usr/bin/sips --resampleWidth 60 $path --out $thumb_path", $output, $ret); $stuff = implode(',',$output)."\n"; $string = "/usr/bin/sips --resampleWidth 60 $path --out $thumb_path"." ,returns: ".$ret."\n\n"; $handle = fopen('/Users/php/acuweb_helpers/out.txt', 'a'); fwrite($handle, $string); fwrite($handle, $stuff); fclose($handle); ?> All the extra stuff there is just me troubleshooting - I write to a text file, the command that I am passing to exec() so I can make sure it comes out right (which it does, everytime). So, the problem is that the exec() fails. I have checked permissions and they are wide open. I even modifed the command to be like this (to try and force the command to be executed as root user): sudo -u root -S /usr/bin/sips --resampleWidth 60 $path --out $thumb_path < /etc/root.secret Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 Oh, and since I am printing out to that file (just so I can see the full command being sent to exec()), I can take the string from that file, put it in a php script and it works just fine. In other words, if you look at my code above, the command contains variables that need to be interpolated. However if I just run this script, it works fine: <?php exec("/usr/bin/sips --resampleWidth 60 /Users/aw/www/images/uploads/1.jpg --out /Users/aw/www/images/uploads/thumbs/1.jpg", $output, $ret); ?> So, apparently I am set to run exec w/o trouble. Note that I have also used the backtick operator and passthru on different occasions. Any other thoughts on ways to troubleshoot? For example, does anyone know the significance of the return error codes for exec()? When I run this it always returns a 6. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2008 Share Posted February 7, 2008 Do any of the arguments contain whitespace? What are the return codes for sips? (Try man sips) Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 Thank you for replying. Literally what the php script is getting is $path = /Users/aw/www/images/uploads/1.jpg $thumb_path = /Users/aw/www/images/uploads/thumbs/1.jpg So the resulting command sent to exec: exec("/usr/bin/sips --resampleWidth 60 $path --out $thumb_path", $output, $ret); should interpolate to this: exec("/usr/bin/sips --resampleWidth 60 /Users/aw/www/images/uploads/1.jpg --out /Users/aw/www/images/uploads/thumbs/1.jpg", $output, $ret); I have read the man page on sips and it does not tell the error codes http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/sips.1.html And also, like I said if I just take the resulting command, stick it in another php script and run that script (either from browser or command line), it works fine: <?php exec("/usr/bin/sips --resampleWidth 60 /Users/aw/www/images/uploads/1.jpg --out /Users/aw/www/images/uploads/thumbs/1.jpg", $output, $ret); ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2008 Share Posted February 7, 2008 Are warnings turned on in Perl? What if you literally type those paths into the Perl--does it work? Why not keep the code in Perl? I would use system, although, without knowing the return codes for sips, one cannot properly check for errors. Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 Yeah, I tried just using backtick or system in Perl, but it failed there too. Since it's failing in PHP now too, I guess I might as well remove that link in the chain and just go right to sips from Perl. My thinking is that it's some type of permissions issue between the user that the perl cgi runs as and apache or root. Though I even tried sudoing the command as root like so: `sudo -u root -S /usr/bin/sips --resampleWidth 60 $path --out $thumb_path < /etc/root.secret`; Well, if anyone else has further thoughts, I would appreciate it. Otherwise I'll go back to troubleshooting in Perl. thanks again for your time. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2008 Share Posted February 7, 2008 What does this give you in Perl? system("command and args") == 0 or die "$! / " . ($? >> ; Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 Software error: / 1 at AcuUploadFile.pm line 80. For help, please send mail to the webmaster (webdev@acuitymarketing.com), giving this error message and the time and date of the error. thanks again for trying to help Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2008 Share Posted February 7, 2008 Is there anything useful in Apache's error log? Can you run other commands successfully? What are the user, group, and permissions for sips? What user and group is Apache running under? Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 I don't see anything in the error log related to this script. Yes, I can run other commands. In fact, a few steps before this part, I copy the file from it's temp location to it's live location: system("/bin/cp -pr $pth $path"); and that works fine. Sips is owned by root. here is the ls: -r-xr-xr-x 1 root wheel 174616 Aug 20 2006 sips Apache runs as user "www" Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2008 Share Posted February 7, 2008 And everything is open for www on the web side? Interesting. I still think it's odd that sips will work manually--it still does, right?--but returns an exit code in Perl, and an undocumented one at that! Have you verified that everything works properly leading up to the sips line in the Perl? When you successfully execute sips at the command line, what status does it return? Try echo $status for csh or echo $? for all others (I think). Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 Yeah it's got me totally baffled. Ok so I just tried the sips command on the cli, this one to be exact: /usr/bin/sips --resampleWidth 60 /Users/aw/www/images/uploads/1.jpg --out /Users/aw/www/images/uploads/thumbs/1.jpg Then I did echo $? and got back 0 So it's nice and happy on command line. Here is another trick. Remember in my original post I was using a system call in the perl, to call a php script and it was the php script which was calling sips? Well I just ran that php script on the command line and it worked fine. Here's what I did: php create_thumb.php /Users/aw/www/images/uploads/1.jpg /Users/aw/www/images/uploads/thumbs/1.jpg That was when logged in as root. So, now I am really leaning towards it being a conflict between the "www" user (Apache) and root. And in fact, if I try to run that php script from a browser (passing the two paths on the $_GET) it also fails - so again, there it is running as "www". So, I am going to monkey around with the www user and see if i can assign it to the same groups as root, or maybe change onwership on sips itself. I'll let you know what I find. ** Although what kills me is I ran it using sudo as root and it did not work. *pulls out hair* Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 well i am at my wit's end. this is nuts. I can do all kinds of other commands via system() but this one tanks. I don't know what else to try. I changed ownership on sips. I added the www to the sudoers list so they can do this command with no problem. I checked all ther permissions on all involved files. Command line works perfect, through browser/apache does not. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2008 Share Posted February 7, 2008 What does this give you? <pre> <?php $path = '/Users/aw/www/images/uploads/thumbs'; $dirs = explode('/', $path); foreach ($dirs as $dir) { $cur_dir .= $dir . '/'; echo '<b>', $cur_dir, '</b><br/>'; echo substr(sprintf('%o', fileperms($cur_dir)), -4), '/'; echo is_executable($cur_dir), '/'; echo fileowner($cur_dir), '/'; echo filegroup($cur_dir); echo '<br/><br/>'; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 / 0775/1/0/80 /Users/ 0777/1/0/80 /Users/aw/ 0777/1/0/80 /Users/aw/www/ 0777/1/0/80 /Users/aw/www/images/ 0777/1/70/80 /Users/aw/www/images/uploads/ 0777/1/70/80 /Users/aw/www/images/uploads/thumbs/ 0777/1/0/80 Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 oh and here is another clue. I happened to think about how I had experimented with sips via PHP on my older server. So I went back and found my script on the older machine and ran it - and it works fine through a browser. So now I thinking it could also be version of PHP, because on the old machine I am running 4.3.2 and on the newer machine i am running 5.2.1 ??? Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2008 Share Posted February 7, 2008 I don't see how permissions could be involved, unless there is something specific in how Perl, PHP, and/or Apache are configured. What happens if you try to use a universal directory such as /tmp? Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 same error: 6 / 0 at AcuUploadFile.pm line 82. Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 addition to two posts above (re: older server) I just checked everything on the older server that I had modified on the new server (apache is running as "www" user, "www" user is NOT in the sudoers list, permissions are same on sips) - and i see nothing glaring at me saying "Hey, here I am!!! here is the difference you are looking for!!" Could it be a change in the php implementation of system()? Quote Link to comment Share on other sites More sharing options...
effigy Posted February 7, 2008 Share Posted February 7, 2008 That sounds doubtful. Are the sips the same version? Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 7, 2008 Author Share Posted February 7, 2008 I see no way to pull the version. The typical -v flag does not work, nor --version Quote Link to comment Share on other sites More sharing options...
resago Posted February 8, 2008 Share Posted February 8, 2008 you do know php can already do this natively, right? Quote Link to comment Share on other sites More sharing options...
resago Posted February 8, 2008 Share Posted February 8, 2008 $src = imagecreatefromjpeg("$image"); $im = imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($im,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagejpeg($im, '',75); imagedestroy($im); Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 8, 2008 Author Share Posted February 8, 2008 Ya, and honestly I am heading back that way. This just really ticked me off, because other commands work great, and this one works perfect on an older server. I am the kind that wants to know what the heck is going on. Quote Link to comment Share on other sites More sharing options...
jrlooney Posted February 8, 2008 Author Share Posted February 8, 2008 Ok, maybe you can still help me. If I call my php script directly from the browser, then using the PHP builtin functions works. However, if I call it from the Perl script, then the php script fails at: $src = imagecreatefromjpeg("$path"); So, here is the code. This one works fine when called from the browser directly as so: http://domain.com/create_thumb.php?path=/Users/aw/www/images/uploads/1.jpg&path2=/Users/aw/www/images/uploads/thumbs/1.jpg <?php $path = $_GET['path']; $thumb_path = $_GET['path2']; $new_width = '60'; $new_height = '60'; list($width, $height) = getimagesize($path); $src = imagecreatefromjpeg("$path"); $im = imagecreatetruecolor($new_width,$new_height); imagecopyresampled($im,$src,0,0,0,0,$new_width,$new_height,$width,$height); imagejpeg($im, $thumb_path,75); imagedestroy($im); ?> Now, it fails if from my perl script I call the php script like this: `/usr/bin/php /Users/php/acuweb_helpers/create_thumb.php $path $thumb_path`; <?php $path = $argv[1]; $thumb_path = $argv[2]; $new_width = '60'; $new_height = '60'; list($width, $height) = getimagesize($path); $src = imagecreatefromjpeg("$path"); $im = imagecreatetruecolor($new_width,$new_height); imagecopyresampled($im,$src,0,0,0,0,$new_width,$new_height,$width,$height); imagejpeg($im, $thumb_path,75); imagedestroy($im); ?> And note - I also have the php script writing some stuff to a text file so I can troubleshoot. for instance I did implode(',', $argv) and wrote that string to the text file and it shows that the 2 image paths are coming in correctly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.