How to view jpg images with a command line PHP program

三世轮回 提交于 2019-12-11 11:49:26

问题


Is there a way to view jpg images within command line PHP?

I would like to loop thru a directory of photos within a command line PHP program and use a keystroke, by utilizing php stdin, to take an action on that photo, like move it to another directory. I have not been able to locate a method to output each image from command line PHP.

Thanks

Here is the code I have thus far. If I can view the jpg I think I might be ok …

<?php

// View the jpg images one at a time
$files = glob("images/*.jpg");
foreach($files as $jpg){
//    echo "<img src='$jpg'></br>";
  echo "view image somehow" . PHP_EOL;

echo "The jpg variable equals '$jpg'" . PHP_EOL;

// show them a message to enter letter for category
echo "Enter the category, a(family), r(friends), c(coworkers), d(delete), ctrl-letter(two folders)" . PHP_EOL;

// the script will wait here until the user has entered something and hit ENTER
$category = read_stdin();

// This will display the category message including the category they entered.
echo "You chose $category . Next photo" . PHP_EOL;

switch ($category) {
    case "a":
        echo "$category equals family" . PHP_EOL;
        r    ename("$jpg", "images/sorted/family/$jpg");
        break;
    case "r":
        echo "$category equals friends" . PHP_EOL;
        rename("$jpg", "images/sorted/friends/$jpg");
        break;
    case "c":
        echo "$category equals coworkers" . PHP_EOL;
        rename("$jpg", "images/sorted/coworkers/$jpg");
        break;
    default:
       echo "$category is not equal to a,r, or c" . PHP_EOL;
 }

}

// our function to read from the command line
function read_stdin()
{
        $fr=fopen("php://stdin","r");   // open our file pointer to read from stdin
        $input = fgets($fr,128);        // read a maximum of 128 characters
        $input = rtrim($input);         // trim any trailing spaces.
        fclose ($fr);                   // close the file handle
        return $input;                  // return the text entered
}

?>

回答1:


You mailed me about ImageMagick "display".

What you can do is run "display" in the background and then use "-remote" to send it pictures and updates to display. I have done this using shell scripts under Linux, though I don't think "display" works in the same way for Windows.

For some ideas about the remote control of various image programs see my raw notes on (for linux only though) http://www.ict.griffith.edu.au/anthony/info/X/Image_Remote_Control.txt




回答2:


I ended up using the "open" command which opens the image in "Preview"

It looks like "qlmanage" might also be an option. I could not get ImageMagick to work. It seems to require X11 be installed, which has not been included in OS X in the last few versions. The Apple support page points to "xquartz" as a site from which to install X11 on OS X. I installed it but after wrestling with its setup and Imagemagick looked around more and found "open". thanks



来源:https://stackoverflow.com/questions/27936086/how-to-view-jpg-images-with-a-command-line-php-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!