2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
5 require_once ("inc/settings.php");
7 function getthumbsdir () {
9 return rtrim (THUMBSDIR, "/");
11 $uploaddir = rtrim (UPLOADDIR, "/");
12 return $uploaddir . "/" . "_thumbs";
17 $host = $_SERVER ["HTTP_HOST"];
18 $colpos = strpos ($host, ':');
19 // some web clients add port informations in Host header
20 if ($colpos !== false) {
21 $host = substr ($host, 0, $colpos);
26 function ext_safe ($path) {
27 $basename = basename_safe ($path);
28 return end (explode (".", $basename));
31 function basename_safe ($path) {
32 return end (explode ("/", $path));
35 function unquote($gpc_str) {
36 if (!isset ($gpc_str)) {
39 if (get_magic_quotes_gpc ()) {
40 return stripslashes ($gpc_str);
46 function thumb_url_from_imgpath ($filename) {
47 if (defined ("THUMBSDIRURL") && (strlen (THUMBSDIRURL) != 0)) {
48 return rtrim (THUMBSDIRURL, '/') . "/_mini" . rawurlencode ($filename);
50 return full_url_from_path (getthumbsdir () . "/mini_" . rawurlencode ($filename));
53 function image_url_from_imgpath ($filename) {
54 if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) {
55 return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename);
58 return full_url_from_path (UPLOADDIR . "/" . rawurlencode ($filename));
61 function full_url_from_path ($path) {
64 while (substr($rel_path, 0, 2) == "./") { // strips ./
65 $rel_path = substr ($rel_path, 2);
68 if ($rel_path [0] == "/") {
71 $script_dir = dirname ($_SERVER ["SCRIPT_NAME"]);
72 while ((substr ($rel_path, 0, 3) == "../") &&
73 (strlen($script_dir) != 0)) {
74 $rel_path = substr ($rel_path, 3);
75 while (substr($rel_path, 0, 2) == "./") {
76 $rel_path = substr ($rel_path, 2);
78 $script_dir = substr ($script_dir, 0, strrpos ($script_dir, "/"));
80 if ((strlen ($script_dir) == 0) && (substr ($rel_path, 0, 3) == "../")) {
83 $path = "$script_dir/$rel_path";
87 $port = $_SERVER ["SERVER_PORT"];
88 if ($_SERVER ["HTTPS"] == "on") {
94 if (($port == "80" && $proto == "http") ||
95 ($port == "443" && $proto == "https")) {
101 return "$proto://$host$port$path";
104 function create_thumbnail ($filename, $destfile) {
105 $ext = strtolower (ext_safe ($filename));
106 if ($ext == "jpg" || $ext == "jpeg") {
107 $image = imagecreatefromjpeg ($filename);
108 } else if ($ext == "png") {
109 $image = imagecreatefrompng ($filename);
114 if ($image === false) {
118 if (defined (THUMBSMAXSIZE) && (THUMBSMAXSIZE > 0)) {
119 $thumbsmaxsize = THUMBSMAXSIZE;
121 $thumbsmaxsize = 400; // default value;
124 $width = imageSX ($image);
125 $height = imageSY ($image);
126 if (($width <= $thumbsmaxsize) || ($height <= $thumbsmaxsize)) {
130 if ($width > $height) {
131 $thumb_width = $thumbsmaxsize;
132 $thumb_height = $height * ($thumbsmaxsize / $width);
133 } else if ($width < $height) {
134 $thumb_width = $width * ($thumbsmaxsize / $height);
135 $thumb_height = $thumbsmaxsize;
136 } else if ($width == $height) {
137 $thumb_width = $thumbsmaxsize;
138 $thumb_height = $thumbsmaxsize;
141 $thumb_image = ImageCreateTrueColor ($thumb_width, $thumb_height);
142 if ($thumb_image === false) {
145 if (!imagecopyresampled ($thumb_image, $image, 0, 0, 0, 0,
146 $thumb_width, $thumb_height, $width, $height)) {
150 if ($ext == "jpg" || $ext == "jpeg") {
151 if (!imagejpeg ($thumb_image, $destfile, 100)) {
154 } else if ($ext == "png") {
155 if (!imagepng ($thumb_image, $destfile)) {
160 imagedestroy ($image);
161 imagedestroy ($thumb_image);
166 function safe_create_dir ($dirname) {
167 if (is_dir ($dirname)) {
170 if (file_exists ($dirname)) {
171 par_error ($dirname . ": " . trans ('exist but is not a directory'));
173 if (!mkdir ($dirname)) {
174 par_error ($dirname . ": " . trans ('could not create directory'));
176 par_success ($dirname . ": " . trans ('directory created'));
180 function safe_create_writable_dir ($dirname) {
181 safe_create_dir ($dirname);
182 if (!is_writeable ($dirname) || !is_executable ($dirname)) {
183 par_error ($dirname . ": " . trans ('could not write in directory'));
188 exit ("\n</body></html>");
190 function par_success ($message) {
191 printf ("<p class=\"success center\">%s</p>", $message);
193 function par_error ($message) {
194 printf ("<p class=\"error center\">%s</p>", $message);
197 function par_warn ($message) {
198 printf ("<p class=\"warn center\">%s</p>", $message);