source: releases/0.91beta/plugins/ImageManager/Classes/NetPBM.php @ 652

Last change on this file since 652 was 652, checked in by ray, 12 years ago

release 0.91beta with changed namespace

File size: 8.4 KB
Line 
1<?php
2/***********************************************************************
3** Title.........:  NetPBM Driver
4** Version.......:  1.0
5** Author........:  Xiang Wei ZHUO <wei@zhuo.org>
6** Filename......:  NetPBM.php
7** Last changed..:  30 Aug 2003
8** Notes.........:  Orginal is from PEAR
9**/
10
11// +----------------------------------------------------------------------+
12// | PHP Version 4                                                        |
13// +----------------------------------------------------------------------+
14// | Copyright (c) 1997-2002 The PHP Group                                |
15// +----------------------------------------------------------------------+
16// | This source file is subject to version 2.02 of the PHP license,      |
17// | that is bundled with this package in the file LICENSE, and is        |
18// | available at through the world-wide-web at                           |
19// | http://www.php.net/license/2_02.txt.                                 |
20// | If you did not receive a copy of the PHP license and are unable to   |
21// | obtain it through the world-wide-web, please send a note to          |
22// | license@php.net so we can mail you a copy immediately.               |
23// +----------------------------------------------------------------------+
24// | Authors: Peter Bowyer <peter@mapledesign.co.uk>                      |
25// +----------------------------------------------------------------------+
26//
27// $Id: NetPBM.php 27 2004-04-01 08:31:57Z Wei Zhuo $
28//
29// Image Transformation interface using command line NetPBM
30
31require_once "Transform.php";
32
33Class Image_Transform_Driver_NetPBM extends Image_Transform
34{
35
36    /**
37     * associative array commands to be executed
38     * @var array
39     */
40    var $command = array();
41
42    /**
43     * Class Constructor
44     */
45    function Image_Transform_Driver_NetPBM()
46    {
47        $this->uid = md5($_SERVER['REMOTE_ADDR']);
48           
49        return true;
50    } // End function Image_NetPBM
51
52    /**
53     * Load image
54     *
55     * @param string filename
56     *
57     * @return mixed none or a PEAR error object on error
58     * @see PEAR::isError()
59     */
60    function load($image)
61    {
62        //echo $image;
63        $this->image = $image;
64        $this->_get_image_details($image);
65    } // End load
66
67    /**
68     * Resizes the image
69     *
70     * @return none
71     * @see PEAR::isError()
72     */
73    function _resize($new_x, $new_y)
74    {
75        // there's no technical reason why resize can't be called multiple
76        // times...it's just silly to do so
77
78        $this->command[] = IMAGE_TRANSFORM_LIB_PATH .
79                           "pnmscale -width $new_x -height $new_y";
80
81        $this->_set_new_x($new_x);
82        $this->_set_new_y($new_y);
83    } // End resize
84
85    /**
86     * Crop the image
87     *
88     * @param int $crop_x left column of the image
89     * @param int $crop_y top row of the image
90     * @param int $crop_width new cropped image width
91     * @param int $crop_height new cropped image height
92     */
93    function crop($crop_x, $crop_y, $crop_width, $crop_height)
94    {
95        $this->command[] = IMAGE_TRANSFORM_LIB_PATH .
96                            "pnmcut -left $crop_x -top $crop_y -width $crop_width -height $crop_height";
97    }
98
99    /**
100     * Rotates the image
101     *
102     * @param int $angle The angle to rotate the image through
103     */
104    function rotate($angle)
105    {
106        $angle = -1*floatval($angle);
107
108        if($angle > 90)
109        {   
110            $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "pnmrotate -noantialias 90";
111            $this->rotate(-1*($angle-90));
112        }
113        else if ($angle < -90)
114        {
115            $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "pnmrotate -noantialias -90";
116            $this->rotate(-1*($angle+90));
117        }
118        else
119            $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "pnmrotate -noantialias $angle";
120    } // End rotate
121
122    /**
123     * Flip the image horizontally or vertically
124     *
125     * @param boolean $horizontal true if horizontal flip, vertical otherwise
126     */
127    function flip($horizontal)
128    {
129        if($horizontal)
130            $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "pnmflip -lr";
131        else
132            $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "pnmflip -tb";
133    }
134
135    /**
136     * Adjust the image gamma
137     *
138     * @param float $outputgamma
139     *
140     * @return none
141     */
142    function gamma($outputgamma = 1.0) {
143        $this->command[13] = IMAGE_TRANSFORM_LIB_PATH . "pnmgamma $outputgamma";
144    }
145
146    /**
147     * adds text to an image
148     *
149     * @param   array   options     Array contains options
150     *             array(
151     *                  'text'          // The string to draw
152     *                  'x'             // Horizontal position
153     *                  'y'             // Vertical Position
154     *                  'Color'         // Font color
155     *                  'font'          // Font to be used
156     *                  'size'          // Size of the fonts in pixel
157     *                  'resize_first'  // Tell if the image has to be resized
158     *                                  // before drawing the text
159     *                   )
160     *
161     * @return none
162     */
163    function addText($params)
164    {
165        $default_params = array('text' => 'This is Text',
166                                'x' => 10,
167                                'y' => 20,
168                                'color' => 'red',
169                                'font' => 'Arial.ttf',
170                                'size' => '12',
171                                'angle' => 0,
172                                'resize_first' => false);
173        // we ignore 'resize_first' since the more logical approach would be
174        // for the user to just call $this->_resize() _first_ ;)
175        extract(array_merge($default_params, $params));
176        $this->command[] = "ppmlabel -angle $angle -colour $color -size "
177                           ."$size -x $x -y ".$y+$size." -text \"$text\"";
178    } // End addText
179
180    function _postProcess($type, $quality, $save_type)
181    {
182        $type = is_null($type) || $type==''? $this->type : $type;
183        $save_type = is_null($save_type) || $save_type==''? $this->type : $save_type;
184        //echo "TYPE:". $this->type;
185        array_unshift($this->command, IMAGE_TRANSFORM_LIB_PATH
186                      . $type.'topnm '. $this->image);
187        $arg = '';
188        switch(strtolower($save_type)){
189            case 'gif':
190                $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "ppmquant 256";
191                $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "ppmto$save_type";
192                break;
193            case 'jpg':
194            case 'jpeg':
195                $arg = "--quality=$quality";
196                $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "ppmto$save_type $arg";
197                break;
198            default:
199                $this->command[] = IMAGE_TRANSFORM_LIB_PATH . "pnmto$save_type $arg";
200                break;
201        } // switch
202        return implode('|', $this->command);
203    }
204
205    /**
206     * Save the image file
207     *
208     * @param string $filename the name of the file to write to
209     * @param string $type (jpeg,png...);
210     * @param int $quality 75
211     * @return none
212     */
213    function save($filename, $type=null, $quality = 85)
214    {
215        $cmd = $this->_postProcess('', $quality, $type) . ">$filename";
216           
217                //if we have windows server
218        if(isset($_ENV['OS']) && eregi('window',$_ENV['OS']))
219                        $cmd = ereg_replace('/','\\',$cmd);
220        //echo $cmd."##";
221        $output = system($cmd);
222                error_log('NETPBM: '.$cmd);
223                //error_log($output);
224        $this->command = array();
225    } // End save
226
227
228    /**
229     * Display image without saving and lose changes
230     *
231     * @param string $type (jpeg,png...);
232     * @param int $quality 75
233     * @return none
234     */
235    function display($type = null, $quality = 75)
236    {
237        header('Content-type: image/' . $type);
238        $cmd = $this->_postProcess($type, $quality);
239       
240        passthru($cmd);
241        $this->command = array();
242    }
243
244    /**
245     * Destroy image handle
246     *
247     * @return none
248     */
249    function free()
250    {
251        // there is no image handle here
252        return true;
253    }
254
255
256} // End class NetPBM
257?>
Note: See TracBrowser for help on using the repository browser.