1 | <?php |
---|
2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | |
---|
5 | $service = &$arr[0]; |
---|
6 | $service->addMethod('pwg.image.rotate', 'ws_image_rotate', |
---|
7 | array( |
---|
8 | 'image_id'=>array(), |
---|
9 | 'angle'=>array('default'=>"90"), |
---|
10 | 'pwg_token' => array(), |
---|
11 | 'rotate_hd' => array('default'=>0) |
---|
12 | ), |
---|
13 | 'Rotates a given image' |
---|
14 | ); |
---|
15 | |
---|
16 | function ws_image_rotate($params, &$service) |
---|
17 | { |
---|
18 | global $conf; |
---|
19 | |
---|
20 | if (!is_admin()) |
---|
21 | { |
---|
22 | return new PwgError(401, 'Access denied'); |
---|
23 | } |
---|
24 | |
---|
25 | if (empty($params['image_id'])) |
---|
26 | { |
---|
27 | return new PwgError(403, "image_id or image_path is missing"); |
---|
28 | } |
---|
29 | |
---|
30 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
31 | { |
---|
32 | return new PwgError(403, 'Invalid security token'); |
---|
33 | } |
---|
34 | |
---|
35 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
36 | include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php'); |
---|
37 | |
---|
38 | $image_id=(int)$params['image_id']; |
---|
39 | |
---|
40 | $query=' |
---|
41 | SELECT |
---|
42 | id, |
---|
43 | path, |
---|
44 | representative_ext, |
---|
45 | rotation |
---|
46 | FROM '.IMAGES_TABLE.' |
---|
47 | WHERE id = '.$image_id.' |
---|
48 | ;'; |
---|
49 | $row = pwg_db_fetch_assoc(pwg_query($query)); |
---|
50 | if ($row == null) |
---|
51 | { |
---|
52 | return new PwgError(403, "image_id not found"); |
---|
53 | } |
---|
54 | |
---|
55 | $base_angle = pwg_image::get_rotation_angle_from_code($row['rotation']); |
---|
56 | |
---|
57 | if (get_boolean($params['rotate_hd'])) { |
---|
58 | if ('auto' == $angle) { |
---|
59 | $angle = pwg_image::get_rotation_angle($row['path']); |
---|
60 | $rotation_code = 0; |
---|
61 | } |
---|
62 | else { |
---|
63 | // the angle is based on what the user sees (the thumbnail) and not on |
---|
64 | // the original, which may have a different angle |
---|
65 | $angle = ($base_angle + $params['angle']) % 360; |
---|
66 | |
---|
67 | // the derivatives must not be rotated |
---|
68 | $rotation_code = 4; |
---|
69 | } |
---|
70 | |
---|
71 | if (isset($conf['rotate_image_jpegtran']) and $conf['rotate_image_jpegtran']) { |
---|
72 | $angle = ($angle + 180) % 360; |
---|
73 | $command = 'jpegtran -copy all -rotate '.$angle.' -outfile '.$row['path'].' '.$row['path']; |
---|
74 | exec($command); |
---|
75 | } |
---|
76 | else { |
---|
77 | $image = new pwg_image($row['path']); |
---|
78 | $image->set_compression_quality(98); |
---|
79 | $image->rotate($angle); |
---|
80 | $image->write($row['path']); |
---|
81 | } |
---|
82 | |
---|
83 | $conf['use_exif'] = false; |
---|
84 | $conf['use_iptc'] = false; |
---|
85 | sync_metadata(array($row['id'])); |
---|
86 | |
---|
87 | single_update( |
---|
88 | IMAGES_TABLE, |
---|
89 | array('rotation' => $rotation_code), |
---|
90 | array('id' => $row['id']) |
---|
91 | ); |
---|
92 | } |
---|
93 | elseif ('auto' != $params['angle']) { |
---|
94 | $new_angle = ($base_angle + $params['angle']) % 360; |
---|
95 | $rotation_code = pwg_image::get_rotation_code_from_angle($new_angle); |
---|
96 | |
---|
97 | // to show that it's a manual rotation, we use 4,5,6,7 instead of 0,1,2,3 |
---|
98 | $rotation_code+= 4; |
---|
99 | |
---|
100 | single_update( |
---|
101 | IMAGES_TABLE, |
---|
102 | array('rotation' => $rotation_code), |
---|
103 | array('id' => $row['id']) |
---|
104 | ); |
---|
105 | } |
---|
106 | |
---|
107 | delete_element_derivatives($row); |
---|
108 | |
---|
109 | return true; |
---|
110 | } |
---|
111 | |
---|
112 | ?> |
---|