Changeset 1011

Show
Ignore:
Timestamp:
05/21/05 09:53:27 (4 years ago)
Author:
torben
Message:

Moved the utility helper locations to the MidCOM configuration.
Did a lot of code cleanup along the way.

Note, that this transition is yet untested.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/midcom/config/midcom_config.php

    r1000 r1011  
    142142 *   that, it might contain an URL which is not suitable for relcoations. 
    143143 *  
     144 * <b>Utility Programs</b> 
     145 *  
     146 * The various paths set here lead to the utility programs required by MidCOM, both  
     147 * mandatory and optional applications are listed here. To indicate that a certain  
     148 * application is unavailable, set it to null. It is recommended to set this in the 
     149 * /etc/midgard/midcom.conf file. The defaults assume that the files are within the 
     150 * $PATH of the Apache user and should be sufficient in most cases. Package mainatainers 
     151 * are encouraged to make the paths explicit. 
     152 *  
     153 * - <b>string utility_imagemagick_base:</b> The base path of the ImageMagick executables, 
     154 *   the tools <i>mogrify</i>, <i>identify</i> and <i>convert</i> are needed for almost  
     155 *   all kinds of image operations in MidCOM and have to be present therefore. The path 
     156 *   entered here requires a trailing slash. 
     157 * - <b>string utility_jpegtran:</b> JPEGTran is used to do losless rotation of JPEG 
     158 *   images for automatic EXIF rotation in n.s.photos for example. If unavailable,  
     159 *   there is an automatic fallback to imagemagick. 
     160 * - <b>string utility_unzip:</b> The unzip utility, used for bulk uploads. 
     161 * - <b>string utility_gzip:</b> The gzip utility, used for bulk uploads. 
     162 * - <b>string utility_tar:</b> The tar utility, used for bulk uploads. 
     163 * - <b>string utility_jhead:</b> The jhead utility, used as a fallback to read EXIF 
     164 *   information if the PHP implementation (pre 4.3) is buggy. 
     165 * - <b>string utility_find:</b> The Find utility is used for bulk upload preprocessing  
     166 *   and the like. 
     167 * - <b>string utility_file:</b> Utility to identify all kinds of uploaded files. 
     168 * - <b>string utility_catdoc:</b> Transforms Word Documents into text for indexing. 
     169 * - <b>string utility_pdftotext:</b> Transforms PDF Documents into text for indexing. 
     170 * - <b>string utility_unrtf:</b> Transforms RTF Documents into text files for indexing. 
     171 *   
     172 *  
    144173 * <b>Visibility settings (NAP)</b> 
    145174 *  
     
    149178 * - <b>bool show_unapproved_objects:</b> This flag indicates wether objects should be 
    150179 *   shown even if they are not approved. This defaults to true.   
    151  *  
    152180 *  
    153181 *  
     
    218246$GLOBALS['midcom_config_default']['midcom_root_topic_guid'] = ''; 
    219247$GLOBALS['midcom_config_default']['midcom_site_url'] = '/'; 
     248 
     249 
     250// Utilities 
     251$GLOBALS['midcom_config_default']['utility_imagemagick_base'] = ''; 
     252$GLOBALS['midcom_config_default']['utility_jpegtran'] = 'jpegtran'; 
     253$GLOBALS['midcom_config_default']['utility_unzip'] = 'unzip'; 
     254$GLOBALS['midcom_config_default']['utility_gzip'] = 'gzip'; 
     255$GLOBALS['midcom_config_default']['utility_tar'] = 'tar'; 
     256$GLOBALS['midcom_config_default']['utility_jhead'] = 'jhead'; 
     257$GLOBALS['midcom_config_default']['utility_find'] = 'find'; 
     258$GLOBALS['midcom_config_default']['utility_file'] = 'file'; 
     259$GLOBALS['midcom_config_default']['utility_catdoc'] = 'catdoc'; 
     260$GLOBALS['midcom_config_default']['utility_pdftotext'] = 'pdftotext'; 
     261$GLOBALS['midcom_config_default']['utility_unrtf'] = 'unrtf'; 
     262 
     263 
     264// gzip tar jhead 
     265 
    220266 
    221267 
  • trunk/lib/midcom/helper/datamanager/datatype_image.php

    r752 r1011  
    133133    } 
    134134     
    135     function _handle_upload ($params, $meta) { 
     135    function _handle_upload ($params, $meta)  
     136    { 
    136137        debug_push("midcom_helper_datamanager_datatype_image::_handle_upload"); 
    137138         
     
    152153        $convert_options_thumb = '+profile "*" -quality ' . (int) $this->_thumbjpegqual; 
    153154 
    154         /* check if "convert" is installed */ 
    155         $convert = exec("which convert"); 
    156         if ($convert == "") { 
    157             $midcom_errstr = "Could not find image convert utility in path."; 
    158             debug_add($midcom_errstr, MIDCOM_LOG_ERROR); 
    159             debug_add("Leaving"); 
    160             debug_pop(); 
    161             return false; 
    162         } 
    163  
    164155        $id = parent::_handle_upload($params, $meta); 
    165156        if (!$id) { 
     
    179170        /* store image as-is, no thumbnail etc. */ 
    180171         
    181         if ($thumbtype == "asis") { 
     172        if ($thumbtype == "asis")  
     173        { 
    182174            debug_add("Storing thumbnail as-is, no further conversion.", MIDCOM_LOG_DEBUG); 
    183175            /* thumbguid == attachment guid 
     
    192184        /* get mimetype and mimetype to convert to */ 
    193185        $file_orig = $params["tmp_name"]; 
    194         $orig_mimetype = exec("file -ib $file_orig 2>/dev/null"); 
    195         debug_add("original mimetype: $orig_mimetype", MIDCOM_LOG_DEBUG); 
     186        $orig_mimetype = exec("{$GLOBALS['midcom_config']['utility_file']} -ib {$file_orig} 2>/dev/null"); 
     187        debug_add("original mimetype: {$orig_mimetype}", MIDCOM_LOG_DEBUG); 
    196188         
    197189        /* auto detect mime type and file extension */ 
    198         switch ($orig_mimetype) { 
     190        switch ($orig_mimetype)  
     191        { 
    199192            case "image/png": 
    200193            case "image/gif": 
     
    213206        debug_add("auto detected extension: " . $auto_ext, MIDCOM_LOG_DEBUG); 
    214207 
    215         switch ($storetype) { 
     208        switch ($storetype)  
     209        { 
    216210            case "": 
    217211                // auto detect 
     
    240234        debug_add("new extension: " . $new_ext, MIDCOM_LOG_DEBUG); 
    241235         
    242         switch ($thumbtype) { 
     236        switch ($thumbtype)  
     237        { 
    243238            case "": 
    244239                // auto detect 
     
    269264        $file_tmp_thumb = $tmp_dir."/thumb_".$file_base.".".$thumb_ext; 
    270265         
    271         if (($new_mimetype != $orig_mimetype) and ($storetype != "asis")) { 
     266        if (   $new_mimetype != $orig_mimetype 
     267            && $storetype != "asis") 
     268        { 
    272269            // convert original and overwrite attachment 
    273270            debug_add("convert original and overwrite attachment...", MIDCOM_LOG_DEBUG);            
    274271             
    275272            // the "[0]" in the command line makes convert take only the first frame of an animated gif file... 
    276             $cmd = "convert $convert_options ". escapeshellarg($file_orig."[0]") . " ".escapeshellarg($file_tmp_orig); 
     273            $cmd = "{$GLOBALS['midcom_config']['utility_imagemagick_base']}convert $convert_options ". escapeshellarg($file_orig."[0]") . " ".escapeshellarg($file_tmp_orig); 
    277274            debug_add("Executing: $cmd", MIDCOM_LOG_DEBUG); 
    278275            exec($cmd); 
    279276             
    280277            $dest = mgd_open_attachment($id); 
    281             if (!$dest) { 
     278            if (!$dest)  
     279            { 
    282280                $midcom_errstr = "Could not open File Attachment for writing: " . mgd_errstr(); 
    283281                debug_add($midcom_errstr, MIDCOM_LOG_ERROR); 
     
    286284            } 
    287285            $source = fopen($file_tmp_orig,"r"); 
    288             if (!$source) { 
     286            if (!$source)  
     287            { 
    289288                $midcom_errstr = "Could not open converted image for reading, aborting thumbnail creation."; 
    290289                debug_add($midcom_errstr, MIDCOM_LOG_ERROR); 
     
    294293                return false; 
    295294            } 
    296             while (! feof($source))  
     295            while (! feof($source)) 
     296            { 
    297297                fwrite($dest, fread($source, 100000)); 
     298            } 
    298299         
    299300            fclose($dest); 
     
    310311         
    311312        $size = getimagesize($file_orig); 
    312         if ($size) { 
     313        if ($size)  
     314        { 
    313315            $orig_x = $size[0]; 
    314316            $orig_y = $size[1]; 
    315317            debug_add("original size: ".$orig_x."x".$orig_y, MIDCOM_LOG_DEBUG); 
    316         } else { 
     318        }  
     319        else  
     320        { 
    317321            $orig_x = false; 
    318322            $orig_y = false; 
     
    321325   
    322326        /* create thumbnail */ 
    323         $cmd = "convert $convert_options_thumb -geometry '".$this->_thumbwidth."x".$this->_thumbheight.">' ". escapeshellarg($file_orig."[0]")." ".escapeshellarg($file_tmp_thumb); 
     327        $cmd = "{$GLOBALS['midcom_config']['utility_imagemagick_base']}convert $convert_options_thumb -geometry '".$this->_thumbwidth."x".$this->_thumbheight.">' ". escapeshellarg($file_orig."[0]")." ".escapeshellarg($file_tmp_thumb); 
    324328        debug_add("Executing: $cmd", MIDCOM_LOG_DEBUG); 
    325329        exec($cmd); 
    326330 
    327         /* run external filters  
    328          * 
    329          * Code disabled until the author takes a minute to think about uniform code formatting, 
    330          * things like indentation width and spaces between operators 
    331          *  
    332         if ($this->_filterfunction) { 
    333           $filter = $this->_filterfunction; 
    334           debug_add("Executing: ".$filter."(".$file_tmp_thumb.")", MIDCOM_LOG_DEBUG); 
    335           $filter($file_tmp_thumb); 
    336         } 
    337         */ 
    338          
    339331        $size = getimagesize($file_tmp_thumb); 
    340         if ($size) { 
     332        if ($size)  
     333        { 
    341334            $new_x = $size[0]; 
    342335            $new_y = $size[1]; 
    343336            debug_add("thumbnail size: ".$new_x."x".$new_y, MIDCOM_LOG_DEBUG); 
    344         } else { 
     337        }  
     338        else  
     339        { 
    345340            $new_x = false; 
    346341            $new_y = false; 
     
    352347        $filename = basename($file_tmp_thumb); 
    353348        
    354         if ($this->_storage->getattachment($filename)) { 
     349        if ($this->_storage->getattachment($filename))  
     350        { 
    355351            $midcom_errstr = "A file with the thumbnail file name already exists."; 
    356352            debug_add($midcom_errstr, MIDCOM_LOG_ERROR); 
     
    360356         
    361357        $thumb_id = $this->_storage->createattachment($filename, $att->title, $new_mimetype); 
    362         if (!$thumb_id) { 
     358        if (!$thumb_id)  
     359        { 
    363360            $midcom_errstr = "Could not create Thumbnail File Attachment: " . mgd_errstr(); 
    364361            debug_add($midcom_errstr, MIDCOM_LOG_ERROR); 
     
    368365         
    369366        $dest = mgd_open_attachment($thumb_id); 
    370         if (!$dest) { 
     367        if (!$dest)  
     368        { 
    371369            $midcom_errstr = "Could not open Thumbnail File Attachment for writing: " . mgd_errstr(); 
    372370            debug_add($midcom_errstr, MIDCOM_LOG_ERROR); 
     
    375373        } 
    376374        $source = fopen($file_tmp_thumb,"r"); 
    377         if (!$source) { 
     375        if (!$source)  
     376        { 
    378377            $midcom_errstr = "Could not open scaled image for reading, aborting thumbnail creation."; 
    379378            debug_add($midcom_errstr, MIDCOM_LOG_ERROR); 
     
    383382            return $id; 
    384383        } 
    385         while (! feof($source))  
     384        while (! feof($source)) 
     385        {  
    386386            fwrite($dest, fread($source, 100000)); 
     387        } 
    387388         
    388389        /* clean up */ 
     
    395396        $thumb_att = mgd_get_attachment($thumb_id); 
    396397        $thumb_att->parameter("midcom.helper.datamanager.datatype.image","parent_guid",$att->guid()); 
    397         if ($new_x && $new_y) { 
     398        if ($new_x && $new_y)  
     399        { 
    398400            $thumb_att->parameter("midcom.helper.datamanager.datatype.blob","size_x",$new_x); 
    399401            $thumb_att->parameter("midcom.helper.datamanager.datatype.blob","size_y",$new_y); 
  • trunk/lib/midcom/services/indexer/document_attachment.php

    r981 r1011  
    180180    function _process_mime_word() 
    181181    { 
    182         if (! @exec('which catdoc')) 
     182        if (is_null($GLOBALS['midcom_config']['utility_catdoc'])) 
    183183        { 
    184184            debug_add('Could not find catdoc, indexing as binary.', MIDCOM_LOG_INFO); 
     
    192192        $encoding = (strtoupper($this->_i18n->get_current_charset()) == 'UTF-8') ? 'utf-8' : '8859-1'; 
    193193         
    194         $command = "catdoc -d{$encoding} -a $wordfile > $txtfile"; 
     194        $command = "{$GLOBALS['midcom_config']['utility_catdoc']} -d{$encoding} -a $wordfile > $txtfile"; 
    195195        debug_add("Executing: {$command}"); 
    196196        exec ($command, $result, $returncode); 
     
    213213    function _process_mime_pdf() 
    214214    { 
    215         if (! @exec('which pdftotext')) 
     215        if (is_null($GLOBALS['midcom_config']['utility_pdftotext'])) 
    216216        { 
    217217            debug_add('Could not find pdftotext, indexing as binary.', MIDCOM_LOG_INFO); 
     
    225225        $encoding = (strtoupper($this->_i18n->get_current_charset()) == 'UTF-8') ? 'UTF-8' : 'Latin1'; 
    226226         
    227         $command = "pdftotext -enc {$encoding} -nopgbrk -eol unix $pdffile $txtfile 2>&1"; 
     227        $command = "{$GLOBALS['midcom_config']['utility_pdftotext']} -enc {$encoding} -nopgbrk -eol unix $pdffile $txtfile 2>&1"; 
    228228        debug_add("Executing: {$command}"); 
    229229        exec ($command, $result, $returncode); 
     
    244244    function _process_mime_richtext() 
    245245    { 
    246         if (! @exec('which unrtf')) 
     246        if (is_null($GLOBALS['midcom_config']['utility_unrtf'])) 
    247247        { 
    248248            debug_add('Could not find unrtf, indexing as binary.', MIDCOM_LOG_INFO); 
     
    257257         
    258258        // Kill the first five lines, they are crap from the converter. 
    259         $command = "unrtf --nopict --text $rtffile | sed '1,5d' > $txtfile"; 
     259        $command = "{$GLOBALS['midcom_config']['utility_unrtf']} --nopict --text $rtffile | sed '1,5d' > $txtfile"; 
    260260        debug_add("Executing: {$command}"); 
    261261        exec ($command, $result, $returncode); 
  • trunk/lib/net/siriux/photos/Photo.php

    r949 r1011  
    567567        $geometry = escapeshellarg("{$width}x{$height}"); 
    568568        $tmpfile = tempnam ("/tmp", "net_siriux_photos"); 
    569         $cmd = "convert " . escapeshellarg($filename)  
     569        $cmd = "{$GLOBALS['midcom_config']['utility_imagemagick_base']}convert " . escapeshellarg($filename)  
    570570            . " -geometry $geometry -quality 80 +profile '*' -colorspace RGB" 
    571571            . ' -unsharp 1x5 -modulate 100,110,100 -gamma 0.95' 
     
    726726     * @return bool Indicating success. 
    727727     */ 
    728     function _getExifData($file = FALSE) { 
    729         if (! $file) { 
     728    function _getExifData($file = FALSE)  
     729    { 
     730        if (! $file)  
     731        { 
    730732            $tmpfile = $this->_writeFullscaleFile(); 
    731733            if (! $tmpfile) 
    732734                return FALSE; 
    733         } else { 
     735        }  
     736        else  
     737        { 
    734738            $tmpfile = $file; 
    735739        } 
    736740         
    737741        $mode = "timestamp"; 
    738         $have_jhead = @exec("which jhead"); 
     742        $have_jhead = ! is_null($GLOBALS['midcom_config']['utility_jhead']); 
    739743        $have_php = function_exists("read_exif_data"); 
    740744         
    741745        /* Detect operation mode, first check for PHP Version */ 
    742         if (substr(phpversion(),3) == "4.0" || version_compare(phpversion(), "4.2.0", "<")) { 
     746        if (substr(phpversion(),3) == "4.0" || version_compare(phpversion(), "4.2.0", "<"))  
     747        { 
    743748            /* We have a version piror 4.2.0. exifread might be broken there. */ 
    744749            if ($have_jhead) 
     750            { 
    745751                $mode = "jhead"; 
     752            } 
    746753            else if ($have_php) 
     754            { 
    747755                $mode = "php"; 
    748         } else { 
     756            } 
     757        }  
     758        else  
     759        { 
    749760            if ($have_php) 
     761            { 
    750762                $mode = "php"; 
     763            } 
    751764            else if ($have_jhead) 
     765            { 
    752766                $mode = "jhead"; 
     767            } 
    753768        } 
    754769         
     
    758773        $force = $this->_config->get("force_exif_reader"); 
    759774        if ($force == "php" && $have_php) 
     775        { 
    760776            $mode = "php"; 
     777        } 
    761778        else if ($force == "jhead" && $have_jhead) 
     779        { 
    762780            $mode = "jhead"; 
     781        } 
    763782         
    764783        debug_add("Trying EXIF Reader mode $mode"); 
     
    769788                $this->_getExifDataPHP($tmpfile); 
    770789                if (! $this->_config->get("disable_autosync_created")) 
     790                { 
    771791                    $this->syncArticleCreated(); 
     792                } 
    772793                break; 
     794                 
    773795            case "jhead": 
    774796                $this->_getExifDataJhead($tmpfile); 
    775797                if (! $this->_config->get("disable_autosync_created")) 
     798                { 
    776799                    $this->syncArticleCreated(); 
     800                } 
    777801                break; 
     802                 
    778803            case "timestamp": 
    779804                $this->_getExifDataTimestamp($tmpfile); 
    780805                break; 
     806                 
    781807            default: 
    782808                die ("Unknown EXIF Reader selected, this should not happen."); 
     
    831857     * @param string file The file from which to get the EXIF data. 
    832858     */ 
    833     function _getExifDataJhead($file) { 
    834         exec("jhead ".escapeshellarg($file), $return, $status); 
    835         $exif = Array ();       
    836         if ($status == 0) { 
    837             while (list($key,$value) = each ($return)) { 
    838                 if (trim($value)) { 
     859    function _getExifDataJhead($file)  
     860    { 
     861        exec("{$GLOBALS['midcom_config']['utility_jhead']} " . escapeshellarg($file), $return, $status); 
     862        $exif = Array (); 
     863        if ($status == 0)  
     864        { 
     865            foreach ($return as $key => $value) 
     866            { 
     867                if (trim($value))  
     868                { 
    839869                    $explodeReturn = explode(':', $value, 2); 
    840                     if (isset($exif[trim($explodeReturn[0])])) { 
    841                         $exif[trim($explodeReturn[0])] .= "<br>" . 
    842                                 trim($explodeReturn[1]); 
    843                     } else { 
    844                         $exif[trim($explodeReturn[0])] = 
    845                                 trim($explodeReturn[1]); 
     870                    if (isset($exif[trim($explodeReturn[0])]))  
     871                    { 
     872                        $exif[trim($explodeReturn[0])] .= "<br>" . trim($explodeReturn[1]); 
     873                    }  
     874                    else  
     875                    { 
     876                        $exif[trim($explodeReturn[0])] = trim($explodeReturn[1]); 
    846877                    } 
    847878                } 
    848879            }             
    849         } else { 
     880        }  
     881        else  
     882        { 
    850883            debug_add("No EXIF Data found, so not setting anything."); 
    851884            $taken = 0; 
    852885        } 
    853886 
    854         if (! array_key_exists("Date/Time", $exif)) { 
     887        if (! array_key_exists("Date/Time", $exif))  
     888        { 
    855889            debug_add("EXIF Date not found, so not setting it."); 
    856890            $taken = 0; 
     
    859893        // replace silly colons in EXIF dates 
    860894        $date = preg_replace("/(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})/", '\1-\2-\3 \4:\5:\6', $exif["Date/Time"]); 
    861         if (! $date) {   // unkown date format 
     895        if (! $date)  
     896        { 
     897            // unkown date format 
    862898            debug_add("Could not transform EXIF-Date into an ISO Timestamp, ignoring it."); 
    863899            $taken = 0; 
  • trunk/lib/net/siriux/photos/admin.php

    r936 r1011  
    8686 
    8787 
    88     function can_handle($argc, $argv) {   
     88    function can_handle($argc, $argv)  
     89    { 
    8990        debug_push($this->_debug_prefix."can_handle"); 
    9091         
     
    9495        $GLOBALS["midcom"]->set_custom_context_data("errstr", $this->errstr); 
    9596         
    96         /* check if "convert" is installed */ 
    97         $convert = exec("which convert"); 
    98         if ($convert == "")  
    99         { 
    100             $midcom_errstr = "Could not find image convert utility in path."; 
    101             debug_add($midcom_errstr, MIDCOM_LOG_ERROR); 
    102             debug_add("Leaving"); 
    103             debug_pop(); 
    104             $GLOBALS["midcom"]->generate_error(MIDCOM_ERRCRIT, "Could not find image convert utility in path. Aborting."); 
    105             return false; 
    106         } 
    107          
    10897        debug_pop(); 
    10998         
     
    113102        } 
    114103         
    115         switch ($argv[0]) { 
     104        switch ($argv[0])  
     105        { 
    116106            case "upload": 
    117107            case "config": 
     
    137127 
    138128 
    139     function handle($argc, $argv) { 
     129    function handle($argc, $argv)  
     130    { 
    140131        debug_push($this->_debug_prefix . "handle"); 
    141132         
     
    454445        $uploadtempdir = "/tmp/net.siriux.photos.upload." . time() . "." . getmypid(); 
    455446         
    456         switch ($file_extension) { 
     447        switch ($file_extension)  
     448        { 
    457449            case "zip": 
    458450                $compressed = true; 
    459451                $unzipcmd =  
    460                     'unzip ' 
     452                    "{$GLOBALS['midcom_config']['utility_unzip']} " 
    461453                    . escapeshellarg($file['tmp_name']) 
    462454                    . " -d{$uploadtempdir}"; 
     
    467459                $compressed = true; 
    468460                $unzipcmd = 
    469                     "cd {$uploadtempdir} ; tar -xzf " 
     461                    "cd {$uploadtempdir} ; {$GLOBALS['midcom_config']['utility_tar']} -xzf " 
    470462                    . escapeshellarg($file["tmp_name"]); 
    471463                break; 
     
    501493             
    502494            $find_result = Array(); 
    503             exec("find {$uploadtempdir} -type f", $find_result); 
     495            exec("{$GLOBALS['midcom_config']['utility_find']} {$uploadtempdir} -type f", $find_result); 
    504496            foreach ($find_result as $fullname) 
    505497            { 
     
    691683    function check_uploaded_image ($filename) 
    692684    { 
    693         $cmd = 'identify ' . escapeshellarg($filename) . ' 2>&1'; 
     685        $cmd = "{$GLOBALS['midcom_config']['utility_imagemagick_base']}identify "  
     686            . escapeshellarg($filename) . ' 2>&1'; 
    694687        debug_add("Executing: {$cmd}"); 
    695688        exec ($cmd, $output, $return); 
    696         if ($return != 0) 
     689        if ($return !== 0) 
    697690        { 
    698691            debug_add("Check of image {$filename} failed, imagemagick return exit code {$return}", MIDCOM_LOG_INFO); 
  • trunk/lib/net/siriux/photos/filter.php

    r762 r1011  
    1111 
    1212/** 
    13  * Image Filter Class 
     13 * Image Filter Class, used to post process all images, check the documentation 
     14 * of the individual methods. 
     15 *  
     16 * @todo Document 
    1417 *  
    1518 * @author Eero af Heurlin, Nemein Oy <eero.afheurlin@nemein.com> 
     
    1922 * @package net.siriux.photos 
    2023 */ 
    21 class net_siriux_photos_imagefilter { 
     24class net_siriux_photos_imagefilter  
     25
    2226     
    2327    /** 
     
    4246     * The constructor does a bit of variable initialization 
    4347     */ 
    44     function net_siriux_photos_imagefilter() { 
     48    function net_siriux_photos_imagefilter()  
     49    { 
    4550        $this->_filename = null; 
    4651        $this->_quality = "-quality 90"; 
     
    6166     * @returns true, if the file is valid, false otherwise. 
    6267     */ 
    63     function set_file($filename) { 
    64         if (! is_writeable($filename)) { 
     68    function set_file($filename)  
     69    { 
     70        if (! is_writeable($filename))  
     71        { 
    6572            debug_add("The File {$filename} is not writeable.", MIDCOM_LOG_INFO); 
    6673            return false; 
     
    8087     * @returns true, if all filters have been successfully applied, false otherwise. 
    8188     */ 
    82     function process_chain($chain) { 
     89    function process_chain($chain)  
     90    { 
    8391        $filters = explode(";", $chain); 
    8492         
    85         foreach ($filters as $cmd) { 
    86             if (!$this->process_command($cmd)) { 
     93        foreach ($filters as $cmd)  
     94        { 
     95            if (!$this->process_command($cmd))  
     96            { 
    8797                debug_add("Execution of {$cmd} failed, aborting now."); 
    8898                return false; 
     
    108118     * @returns true, if the filter executed successfully, false otherwise. 
    109119     */ 
    110     function process_command($cmd) { 
     120    function process_command($cmd)  
     121    { 
    111122        debug_add("Parsing command {$cmd}"); 
    112123        $i = preg_match('/([a-z_]*)\(([^)]*)\)/', $cmd, $matches); 
    113         if (! $i) { 
     124        if (! $i)  
     125        { 
    114126            debug_add("Failed to parse command {$cmd}, aborting.", MIDCOM_LOG_INFO); 
    115127            return false; 
     
    120132        debug_print_r("Have to execute {$command} with these arguments:", $args); 
    121133         
    122         switch ($command) { 
     134        switch ($command)  
     135        { 
    123136            case "gamma": 
    124137                if (array_key_exists(0, $args) && $args[0] > 0) 
     
    165178     * the transformed image to. Has to be managed by the callee. 
    166179     */ 
    167     function _get_tempfile() { 
     180    function _get_tempfile()  
     181    { 
    168182        return tempnam("/tmp", "net_siriux_photos"); 
    169183    } 
     
    173187     * of $tmpfile, deleting the temporary file afterwards. 
    174188     */ 
    175     function _process_tempfile($tmpname) { 
     189    function _process_tempfile($tmpname)  
     190    { 
    176191        $src = fopen($tmpname, "r"); 
    177192        $dst = fopen($this->_filename, "w+"); 
    178         while (! feof($src)) { 
     193        while (! feof($src))  
     194        { 
    179195            $buffer = fread($src, 131072); /* 128 kB */ 
    180196            fwrite($dst, $buffer, 131072); 
     
    231247     * @returns true on success. 
    232248     */ 
    233     function gamma($gamma) { 
    234         $cmd = "mogrify {$this->_quality} -gamma " . escapeshellarg($gamma) 
    235             . " " . escapeshellarg($this->_filename); 
     249    function gamma($gamma)  
     250    { 
     251        $cmd .= "{$GLOBALS['midcom_config']['utility_imagemagick_base']}mogrify {$this->_quality} -gamma "  
     252            . escapeshellarg($gamma) . " " . escapeshellarg($this->_filename); 
    236253         
    237254        exec($cmd, $output, $exit_code); 
    238         debug_print_r("Imagemagick returned with {$exit_code} and produced this output:", $output); 
    239          
    240         if ($exit_code === 0)
     255         
     256        if ($exit_code === 0)  
     257       
    241258            return true; 
    242         } else { 
    243             debug_add("ImageMagick failed to convert the image, see LOG_DEBUG for details.", MIDCOM_LOG_ERROR); 
    244             debug_add("Command was: {$cmd}"); 
     259        }  
     260        else  
     261        { 
     262            debug_add("ImageMagick failed to convert the image, it returned with {$exit_code}, see LOG_DEBUG for details.", MIDCOM_LOG_ERROR); 
     263            debug_print_r('The generated output was:', $output); 
     264            debug_add("Command was: [{$cmd}]"); 
    245265            return false; 
    246266        } 
     
    258278     * @returns true on success. 
    259279     */ 
    260     function exifrotate() { 
    261         if (! function_exists("read_exif_data")) { 
     280    function exifrotate()  
     281    { 
     282        if (! function_exists("read_exif_data"))  
     283        { 
    262284            debug_add("read_exif_data required for exifrotate.", MIDCOM_LOG_ERROR); 
    263285            return false; 
    264286        } 
    265287        $exif = read_exif_data($this->_filename); 
    266         if (! array_key_exists("Orientation", $exif)) { 
     288        if (! array_key_exists("Orientation", $exif))  
     289        { 
    267290            debug_add("EXIF information misses the orientation tag. Skipping.", MIDCOM_LOG_INFO); 
    268291            return true; 
    269292        } 
    270         if (! $exif || $exif["Orientation"] == 1) { 
     293        if (! $exif || $exif["Orientation"] == 1)  
     294        { 
    271295            debug_add("No rotation neccessary."); 
    272296            return true; 
     
    276300        $imagesize = getimagesize($this->_filename); 
    277301         
    278         if ($imagesize[2] == 2 && @exec("which jpegtran")) { 
     302        if (   $imagesize[2] == 2  
     303            && ! is_null($GLOBALS['midcom_config']['utility_jpegtran'])) 
     304        { 
    279305            /* jpegtran */ 
    280             switch ($exif["Orientation"]) { 
     306            switch ($exif["Orientation"])  
     307            { 
    281308                case 2: $rotate = "-flip horizontal"; break; 
    282309                case 3: $rotate = "-rotate 180"; break; 
     
    294321            $tmpfile = $this->_get_tempfile(); 
    295322            $do_unlink = true; 
    296             $cmd = "jpegtran -copy all {$rotate} -outfile {$tmpfile} " . escapeshellarg($this->_filename); 
    297         } else { 
     323            $cmd = "{$GLOBALS['midcom_config']['utility_jpegtran']} -copy all {$rotate} -outfile {$tmpfile} " . escapeshellarg($this->_filename); 
     324        }  
     325        else  
     326        { 
    298327            /* Mogrify */ 
    299328            debug_add("jpegtran not found, falling back to mogrify."); 
    300329             
    301             switch ($exif["Orientation"]) { 
     330            switch ($exif["Orientation"])  
     331            { 
    302332                case 2: $rotate = "-flip"; break; 
    303333                case 3: $rotate = "-rotate 180"; break; 
     
    313343            } 
    314344             
    315             $cmd = "mogrify {$this->_quality} {$rotate} " 
     345            $cmd = "{$GLOBALS['midcom_config']['utility_imagemagick_base']}mogrify {$this->_quality} {$rotate} " 
    316346                . escapeshellarg($this->_filename); 
    317347        } 
    318348         
    319349        exec($cmd, $output, $exit_code); 
    320         debug_print_r("Imagemagick/jpegtran returned with {$exit_code} and produced this output:", $output); 
    321          
    322         if ($exit_code === 0) { 
    323             if ($do_unlink) 
    324                 $this->_process_tempfile($tmpfile); 
    325             return true; 
    326         } else { 
     350         
     351        if ($exit_code !== 0)  
     352        { 
    327353            debug_add("ImageMagick/jpegtran failed to convert the image, see LOG_DEBUG for details.", MIDCOM_LOG_ERROR); 
    328             debug_add("Command was: {$cmd}"); 
    329             if ($do_unlink) 
    330                 unlink($tmpfile); 
    331             return false; 
    332         } 
    333     } 
    334      
    335     /** 
    336      * Manual image rotation 
    337      *  
    338      * Filter Syntax: rotate($rotate) 
    339      * 
    340      * Where $gamma is a positive floating point number greater then 0 
    341      * and less then 360; if omitted, a NULL operation is done. 
    342      * 
    343      * @param $rotate Degrees of rotation clockwise, negative amounts possible 
    344      * @returns true on success. 
    345      */ 
    346     function rotate($rotate) { 
    347         // Do some normalizing on the argument 
    348         while ($rotate < 0) 
    349         { 
    350             $rotate += 360; 
    351         } 
    352         while ($rotate > 360) 
    353         { 
    354             $rotate -= 360; 
    355         } 
    356         if ($rotate == 0 || $rotate == 360) 
    357         { 
    358             debug_add("Rotate is {$rotate}, we're happy as-is."); 
    359             // We're happy as-is :) 
    360             return true;  
    361         } 
    362          
    363         $do_unlink = false; 
    364         $imagesize = getimagesize($this->_filename); 
    365          
    366         // Try lossless jpegtran rotation if possible 
    367         if (   $imagesize[2] == 2  
    368             && ($rotate == 90 || $rotate == 180 || $rotate == 270)  
    369             && @exec("which jpegtran")) 
    370         { 
    371             $tmpfile = $this->_get_tempfile(); 
    372             $do_unlink = true; 
    373             $cmd = "jpegtran -copy all -rotate {$rotate} -outfile {$tmpfile} " . escapeshellarg($this->_filename); 
    374         } else { 
    375             /* Mogrify */ 
    376             debug_add("jpegtran not found or rotation incompatible, falling back to mogrify."); 
    377              
    378             $cmd = "mogrify {$this->_quality} -rotate {$rotate} " . escapeshellarg($this->_filename); 
    379         } 
    380          
    381         debug_add("We have to rotate clockwise by {$rotate} degrees, do_unlink: {$do_unlink}"); 
    382         debug_add("Executing: {$cmd}"); 
    383         exec($cmd, $output, $exit_code); 
    384         debug_print_r("Imagemagick/jpegtran returned with {$exit_code} and produced this output:", $output); 
    385          
    386         if ($exit_code === 0)  
    387         { 
    388             if ($do_unlink) 
    389             { 
    390                 $this->_process_tempfile($tmpfile); 
    391             } 
    392             return true; 
    393         }  
    394         else  
    395         { 
    396             debug_add("ImageMagick/jpegtran failed to convert the image, see LOG_DEBUG for details.", MIDCOM_LOG_ERROR); 
     354            debug_print_r("Imagemagick/jpegtran returned with {$exit_code} and produced this output:", $output); 
    397355            debug_add("Command was: {$cmd}"); 
    398356            if ($do_unlink) 
     
    402360            return false; 
    403361        } 
     362         
     363        if ($do_unlink) 
     364        { 
     365            $this->_process_tempfile($tmpfile); 
     366        } 
     367        return true; 
     368    } 
     369     
     370    /** 
     371     * Manual image rotation 
     372     *  
     373     * Filter Syntax: rotate($rotate) 
     374     * 
     375     * Where $gamma is a positive floating point number greater then 0 
     376     * and less then 360; if omitted, a NULL operation is done. 
     377     * 
     378     * @param $rotate Degrees of rotation clockwise, negative amounts possible 
     379     * @returns true on success. 
     380     */ 
     381    function rotate($rotate) { 
     382        // Do some normalizing on the argument 
     383        while ($rotate < 0) 
     384        { 
     385            $rotate += 360; 
     386        } 
     387        while ($rotate > 360) 
     388        { 
     389            $rotate -= 360; 
     390        } 
     391        if ($rotate == 0 || $rotate == 360) 
     392        { 
     393            debug_add("Rotate is {$rotate}, we're happy as-is."); 
     394            // We're happy as-is :) 
     395            return true;  
     396        } 
     397         
     398        $do_unlink = false; 
     399        $imagesize = getimagesize($this->_filename); 
     400         
     401        // Try lossless jpegtran rotation if possible 
     402        if (   $imagesize[2] == 2  
     403            && ($rotate == 90 || $rotate == 180 || $rotate == 270)  
     404            && ! is_null($GLOBALS['midcom_config']['utility_jpegtran'])) 
     405        { 
     406            $tmpfile = $this->_get_tempfile(); 
     407            $do_unlink = true; 
     408            $cmd = "{$GLOBALS['midcom_config']['utility_jpegtran']} -copy all -rotate {$rotate} -outfile {$tmpfile} " . escapeshellarg($this->_filename); 
     409        }  
     410        else  
     411        { 
     412            /* Mogrify */ 
     413            debug_add("jpegtran not found or rotation incompatible, falling back to mogrify."); 
     414             
     415            $cmd = "{$GLOBALS['midcom_config']['utility_imagemagick_base']}mogrify {$this->_quality} -rotate {$rotate} " . escapeshellarg($this->_filename); 
     416        } 
     417         
     418        debug_add("We have to rotate clockwise by {$rotate} degrees, do_unlink: {$do_unlink}"); 
     419        exec($cmd, $output, $exit_code); 
     420         
     421        if ($exit_code !== 0)  
     422        { 
     423            debug_add("ImageMagick/jpegtran failed to convert the image, see LOG_DEBUG for details.", MIDCOM_LOG_ERROR); 
     424            debug_print_r("Imagemagick/jpegtran returned with {$exit_code} and produced this output:", $output); 
     425            debug_add("Command was: {$cmd}"); 
     426            if ($do_unlink) 
     427            { 
     428                unlink($tmpfile); 
     429            } 
     430            return false; 
     431        } 
     432         
     433        if ($do_unlink) 
     434        { 
     435            $this->_process_tempfile($tmpfile); 
     436        } 
     437        return true; 
    404438    } 
    405439     
     
    421455