-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitpic.module
executable file
·144 lines (125 loc) · 4.31 KB
/
twitpic.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Implement hook_help().
*/
function twitpic_help($path, $arg) {
if ($path == 'admin/help#twitpic_filter') {
return t('Creates an input filter suitable for user in the body text of nodes, which will include photos hosted on TwitPic. Photos will be cached locally and users can optionally apply an imagestyle setting to each photo.');
}
}
/**
* Implement hook_menu().
*/
function twitpic_menu() {
$items = array();
$items['twitpic/view'] = array(
'title' => 'View a Twitpic image',
'description' => 'Displays a Twitpic image, given an ID.',
'page callback' => 'twitpic_image_view',
'access arguments' => array('access_content'),
'type' => MENU_CALLBACK,
);
$items['twitpic/munge'] = array(
'title' => 'Munge a Twitpic image',
'description' => 'Displays a Twitpic image, munged in various ways (cropped, rotated, etc).',
'page callback' => 'twitpic_image_munge',
'access arguments' => array('access_content'),
'type' => MENU_CALLBACK,
);
$items['twitpic/style'] = array(
'title' => 'Munge a Twitpic image according to an image style',
'description' => 'Displays a Twitpic image, munged according to the specified image style.',
'page callback' => 'twitpic_image_style',
'access arguments' => array('access_content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implement hook_stream_wrappers().
*/
function twitpic_stream_wrappers() {
return array(
'twitpic' => array(
'name' => 'Twitpic photos',
'class' => 'TwitpicStreamWrapper',
'description' => t('Photos from the Twitpic hosting service.'),
'type' => STREAM_WRAPPERS_READ_VISIBLE,
),
);
}
/**
* View an image from Twitpic.
*
* Note this function does not return anything and does not display the image
* in the context of your Drupal site. It simply displays the image itself and
* nothing else. This is just used as a demonstration of how file_transfer() and
* stream wrappers work.
*
* @param $id
* The image's ID, as extracted from its original URL.
* @param $size
* The image's size. This can be either 'thumb' or 'mini'.
*/
function twitpic_image_view($id, $size = 'thumb') {
file_transfer('twitpic://' . $id . '/' . $size, array('content-type' => 'image/jpeg'));
}
/**
* Munge an image from Twitpic by applying a resize, crop or rotate to it.
*
* Note this function does not return anything and does not display the image
* in the context of your Drupal site. It simply displays the image itself and
* nothing else. This is just used as a demonstration of how file_transfer(),
* image api functions, and stream wrappers work.
*
* @param $id
* The image's ID, as extracted from its original URL.
* @param $operation
* An operation to perform on the image. Can be ‘rotate’,
* ‘scale’, or ‘desaturate’.
*/
function twitpic_image_munge($id, $operation = 'rotate') {
// If we get a disallowed operation, just return.
$operations = array('rotate', 'desaturate', 'scale');
if (!in_array($operation, $operations)) {
return;
}
$twitpic_uri = 'twitpic://' . $id . '/thumb';
$local_uri = 'public://' . $id . '.jpg';
if (!$twitpic_image = file_get_contents($twitpic_uri)) {
return;
}
$local_path = file_unmanaged_save_data($twitpic_image, $local_uri, FILE_EXISTS_REPLACE);
$local_image = image_load($local_path);
switch ($operation) {
case 'scale':
image_scale($local_image, NULL, 50, FALSE);
break;
case 'desaturate':
image_desaturate($local_image);
break;
case 'rotate':
image_rotate($local_image, 45, 0x7D26CD);
break;
}
$local_uri = drupal_tempnam('public://', $id);
image_save($local_image, $local_uri);
return theme('image', array('path' => $local_uri));
}
/**
* Munge an image from Twitpic by applying a premade image style to it.
*
* @param $id
* The image's ID, as extracted from its original URL.
*/
function twitpic_image_style($id, $style) {
$styles = image_styles();
if (!array_key_exists($style, $styles)) {
return;
}
$twitpic_uri = 'twitpic://' . $id . '/thumb';
$local_uri = 'public://' . $id . '_' . $style . '.jpg';
$twitpic_image = file_get_contents($twitpic_uri);
$local_path = file_unmanaged_save_data($twitpic_image, $local_uri, FILE_EXISTS_REPLACE);
return theme('image_style', array('path' => $local_path));
}