Victory Road  

Go Back   Victory Road > General > Technology
FAQ Community Today's Posts Search

Notices

 
 
Search this Thread
  #1  
Old March 6, 2011, 03:29:43 AM
FreezeWarp's Avatar
FreezeWarp FreezeWarp is offline
Kyurem
 
Join Date: Oct 2009
Posts: 2,186
Default [PHP/CLI] Comic Book (CBZ) File Creation

During some random cleanup of my hard drive I decided I would try to compress my various mangas, etc. into simpler comic book files. The main benefit of these files is saving space, but they also help boost application compatibility.

Creating a file generally entails compresssing all the images as one of so many compression types, then changing the file extension:
Compression TypeExtension
GNU Tar.cbt
ZIP Archive.cbz
RAR Archive.cbr
7zip Archive.cb7

While this is simple enough, it can be quite cumbersome. As a result, I crafted a fairly simple PHP script to do the job:

PHP Code:
## CLI-to-GET Functionality (To replace create a file that can handle CLI input and convert it to GET variables, or call file from webserver, replacing this file with a text/plain mimetype. http://www.php.net/manual/en/features.commandline.php#93086 is used by author.
require('clitoget.php');

## Copyright
echo "CBZ Creation Script (c) 2011 Joseph T. Parsons.\n\n";

if (isset(
$_GET['help'])) {
  die(
"Usage (Standard CLI): php cbz.php [OPTION] --d=[DIR]\nUsage (Web Server): cbz.php?d=[DIR]&[OPTION]\n\nArguments:\n  --d\t\tThe directory containing the image files to turn into a comic book.\n  --o\t\tThe directory the comic book file should be placed in. If not specified the current working directory will be used.\n  --ow\t\tIf specified, any existing comic book file will be overwritten.\n  --filetypes\t\tA comma-seperated list (without spaces) containing all accepted file types. By default this is \"png,jpg,jpeg\".\n\n");
}

## Initialize Variables
static $filesQue$overwrite$fileOutput;

if (!isset(
$_GET['d'])) {
  if (
$cli) {
    echo 
"Please Enter a Directory: ";
    
$directory trim(fgets(STDIN));
  }
  else {
    die(
"No directory specified.\n");
  }
}
else
  
$directory $_GET['d'];

$directoryNamePieces explode('/',$directory);
$directoryName $directoryNamePieces[count($directoryNamePieces) - 1];

if (!isset(
$_GET['o']))
  
$outputDirectory __DIR__;
else
  
$outputDirectory $_GET['o'];

if (!isset(
$_GET['filetypes']))
  
$fileTypes 'png|jpg|jpeg';
else
  
$fileTypes implode('|',explode(',',$_GET['filetypes'])); // Converts Comma-Seperated-List into Pipe-Seperated-List (Yes, str_replace would probably work just as well)

if (isset($_GET['ow']))
  
$overwrite true;

## More Initialization
$zipFile "$outputDirectory/$directoryName.cbz";

## Check Permissions
if (!is_dir($directory))
  die(
"Directory \"$directory\" does not exist.\n");
elseif (!
is_readable($directory))
  die(
"Directory \"$directory\" is not readable.\n");
elseif (!
is_dir($outputDirectory))
  die(
"Output Directory \"$outputDirectory\" does not exist.\n");
elseif (!
is_writable($outputDirectory))
  die(
"Output Directory \"$outputDirectory\" can not be written to.\n");
else
  echo 
"Directory: $directory.\nOutput File: $zipFile.\n\n";


$files scandir($directory);

## Read Files
if (count($files) >= 1) {
  foreach (
$files AS $file) {
//    $filePieces = explode('/',$file);
//    $file = $filePieces[count($filePieces) - 1];

    
if (!preg_match("/\.($fileTypes)$/",$file)) {
      
$fileOutput .= "Ignored $file\n";
    }
    else {
      
$filesQue[] = $file;
    }
  }
}
else {
  die(
"No files found in \"$directory\".\n");
}

## Create Comic Book
if (count($filesQue) >= 1) {
  
$zip = new ZipArchive
    
or die("Could not initialize ZipArchive class. Please check to make sure it is installed.\n");
  echo 
"Creating ZIP File \"$zipFile\"... ";

  if (
file_exists($zipFile) && !$overwrite)
    die(
"Already Exists (Specify --ow to Delete)\n");
  elseif (
file_exists($zipFile) && $overwrite)
    
unlink($zipFile) or die("Already Existed (Could Not be Deleted)");

  if (
$zip->open($zipFileZipArchive::CREATE) === true)
    echo 
"Sucess\n";

  foreach (
$filesQue AS $file) {
    
$fullPath "$directory/$file";

    echo 
"Adding file \"$fullPath\" as \"$file\"... ";
    if (
$zip->addFile($fullPath$file)) echo "Sucess\n";
    else echo 
"Failed\n";
  }

  
$zip->close();
  exit(
0);
}
else {
  die(
"No files can be processed. Exiting.\n");

The first required file is used to process CLI requests, though it can be easily replaced for use on a webserver. Here is the script I used on a Linux command line:

PHP Code:
 function parseArgs($argv){
     
array_shift($argv);
     
$out = array();
     foreach (
$argv as $arg){
         if (
substr($arg,0,2) == '--'){
             
$eqPos strpos($arg,'=');
             if (
$eqPos === false){
                 
$key substr($arg,2);
                 
$out[$key] = isset($out[$key]) ? $out[$key] : true;
             } else {
                 
$key substr($arg,2,$eqPos-2);
                 
$out[$key] = substr($arg,$eqPos+1);
             }
         } else if (
substr($arg,0,1) == '-'){
             if (
substr($arg,2,1) == '='){
                 
$key substr($arg,1,1);
                 
$out[$key] = substr($arg,3);
             } else {
                 
$chars str_split(substr($arg,1));
                 foreach (
$chars as $char){
                     
$key $char;
                     
$out[$key] = isset($out[$key]) ? $out[$key] : true;
                 }
             }
         } else {
             
$out[] = $arg;
         }
     }
     return 
$out;
 }
 
 
$_GET parseArgs($argv);
 
$cli true
For use on the web, this would do the job (untested):
PHP Code:
 $cli false;
 
header('Content-type: text/plain'); 

Licensing

The file given above is provided free for personal, non-commercial use, but without relinquishing any copyright ownership. For use beyond this I ask that you contact me either via email or PM. Additionally, usage of the script is granted without any warranty or guarantee of it working.



--
Joseph T. Parsons

Last edited by FreezeWarp; March 6, 2011 at 03:36:05 AM.
 

Forum Jump


All times are GMT -8.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Victory Road ©2006 - 2024, Scott Cat333Pokémon Cheney
Theme by A'bom and Cat333Pokémon