source: extensions/Ajax_Thumbnailer/main.inc.php @ 4576

Last change on this file since 4576 was 4549, checked in by Dsls, 14 years ago

Ajax_Thumbnailer 0.3

File size: 4.8 KB
Line 
1<?php
2/*
3Plugin Name: Ajax Thumbnailer
4Version: 0.3
5Description: gestion de miniatures en masse / mass thumbnail generator
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=327
7Author: Bruno Hondelatte
8Author URI: http://morefnu.org
9*/
10
11ini_set('error_reporting', E_ALL);
12ini_set('display_errors', true);
13
14if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
15
16class AjaxThumbnailer
17{
18        /* This function is almost the same as the ont in admin/thumbnail.php , tuned
19                for web-service purposes */
20        function RatioResizeImg($path, $newWidth, $newHeight, $tn_ext,$gd_version=2) {
21                global $conf, $lang, $page;
22               
23                $starttime = get_moment();
24
25                if (!function_exists('gd_info'))
26                        return new PwgError(WS_ERR_INVALID_PARAM,  'no gd');
27
28                if (!file_exists($path))
29                        return new PwgError(WS_ERR_INVALID_PARAM,  'file not found');
30
31                $filename = basename($path);
32                $dirname = dirname($path);
33
34                // extension of the picture filename
35                $extension = get_extension($filename);
36
37                if (in_array($extension, array('jpg', 'JPG', 'jpeg', 'JPEG'))) {
38                        $srcImage = @imagecreatefromjpeg($path);
39                }
40                elseif ($extension == 'png' or $extension == 'PNG') {
41                        $srcImage = @imagecreatefrompng($path);
42                } else {
43                        unset($extension);
44                }
45
46                if ( isset( $srcImage ) ) {
47                        // width/height
48                        $srcWidth    = imagesx( $srcImage ); 
49                        $srcHeight   = imagesy( $srcImage ); 
50                        $ratioWidth  = $srcWidth/$newWidth;
51                        $ratioHeight = $srcHeight/$newHeight;
52
53                        // maximal size exceeded ?
54                        if ( ( $ratioWidth > 1 ) or ( $ratioHeight > 1 ) ) {
55                                if ( $ratioWidth < $ratioHeight) { 
56                                        $destWidth = $srcWidth/$ratioHeight;
57                                        $destHeight = $newHeight; 
58                                } else { 
59                                        $destWidth = $newWidth; 
60                                        $destHeight = $srcHeight/$ratioWidth;
61                                }
62                        } else {
63                                $destWidth = $srcWidth;
64                                $destHeight = $srcHeight;
65                        }
66                        // according to the GD version installed on the server
67                        if ( $gd_version == 2 ) {
68                                // GD 2.0 or more recent -> good results (but slower)
69                                $destImage = imagecreatetruecolor( $destWidth, $destHeight); 
70                                imagecopyresampled( $destImage, $srcImage, 0, 0, 0, 0,
71                                        $destWidth,$destHeight,$srcWidth,$srcHeight );
72                        } else {
73                                // GD prior to version  2 -> pretty bad results :-/ (but fast)
74                                $destImage = imagecreate( $destWidth, $destHeight);
75                                imagecopyresized( $destImage, $srcImage, 0, 0, 0, 0,
76                                        $destWidth,$destHeight,$srcWidth,$srcHeight );
77                        }
78
79                        if (($tndir = mkget_thumbnail_dir($dirname)) == false) {
80                                return new PwgError(WS_ERR_INVALID_PARAM,  '['.$tndir.'] : '.l10n('no_write_access'));
81                        }
82
83                        $dest_file = $tndir.'/'.$conf['prefix_thumbnail'];
84                        $dest_file.= get_filename_wo_extension($filename);
85                        $dest_file.= '.'.$tn_ext;
86
87                        // creation and backup of final picture
88                        if (!is_writable($tndir))
89                                return new PwgError(WS_ERR_INVALID_PARAM,  '['.$tndir.'] : '.l10n('no_write_access'));
90                               
91                        imagejpeg($destImage, $dest_file, $conf['tn_compression_level']);
92                        // freeing memory ressources
93                        imagedestroy( $srcImage );
94                        imagedestroy( $destImage );
95
96                        list($tn_width, $tn_height) = getimagesize($dest_file);
97                        $tn_size = floor(filesize($dest_file) / 1024).' KB';
98
99                        $endtime = get_moment();
100                       
101                        $info = array( 'path'      => $path,
102                        'tn_file'   => $dest_file,
103                        'tn_width'  => $tn_width,
104                        'tn_height' => $tn_height,
105                        'tn_size'   => $tn_size,
106                        'tn_time'       => number_format(($endtime - $starttime) * 1000, 2, '.', ' ').' ms');
107                        return $info;
108                } else {
109                        // error
110                        $err=l10n('tn_no_support');
111                        if ( isset( $extension ) )
112                                $err .= l10n('tn_format').' '.$extension;
113                        else
114                                $err .= l10n('tn_thisformat');
115                        return new PwgError(WS_ERR_INVALID_PARAM, $err);
116                }
117        }
118
119        function plugin_admin_menu($menu) {
120        array_push($menu,
121            array(
122                'NAME' => 'Ajax Thumbnailer',
123                'URL' => get_admin_plugin_menu_link(dirname(__FILE__).'/admin/thumbnailer_admin.php')
124            )
125        );
126        return $menu;
127    }
128
129        function ws_methods($arr) {
130                $service = &$arr[0];
131                $service->addMethod('pwg.thumbnail.create', array($this,'create_thumb'),
132                  array(
133                        'picture'=>array(),
134                        'width'=>array('default'=>"128"),
135                        'height'=>array('default'=>"128"),
136                        'ext'=>array('default'=>"jpg")
137                  ),
138                  'Creates a thumbnail for a given image,
139                <br><b>picture</b> is the name of the picture to create thumbnail from.'
140                );
141               
142        }
143       
144        function create_thumb($params, $service) {
145                $picture = $params['picture'];
146                $width = (integer)$params['width'];
147                $height = (integer)$params['height'];
148                $gd_version = (integer)$params['gd_version'];           
149                $ext =$params['ext'];
150                return $this->RatioResizeImg($picture,$width,$height,$ext,$gd_version);
151        }
152       
153}
154$obj = new AjaxThumbnailer();
155
156add_event_handler('ws_add_methods', array(&$obj, 'ws_methods'));
157add_event_handler('get_admin_plugin_menu_links', array(&$obj, 'plugin_admin_menu') );
158set_plugin_data($plugin['id'], $obj);
159
160?>
Note: See TracBrowser for help on using the repository browser.