2 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
5 // using include because that file may be sourced even if config file has not
7 @include_once ("inc/settings.php");
9 function getthumbsdir () {
11 return rtrim (THUMBSDIR, "/");
13 $uploaddir = rtrim (UPLOADDIR, "/");
14 return $uploaddir . "/" . "_thumbs";
19 $host = $_SERVER ["HTTP_HOST"];
20 $colpos = strpos ($host, ':');
21 // some web clients add port informations in Host header
22 if ($colpos !== false) {
23 $host = substr ($host, 0, $colpos);
28 function ext_safe ($path) {
29 $basename = basename_safe ($path);
30 return end (explode (".", $basename));
33 function basename_safe ($path) {
34 return end (explode ("/", $path));
37 function unquote($gpc_str) {
38 if (!isset ($gpc_str)) {
41 if (get_magic_quotes_gpc ()) {
42 return stripslashes ($gpc_str);
48 function thumb_url_from_imgpath ($filename) {
49 if (defined ("THUMBSDIRURL") && (strlen (THUMBSDIRURL) != 0)) {
50 return rtrim (THUMBSDIRURL, '/') . "/mini_" . rawurlencode ($filename);
52 return full_url_from_path (getthumbsdir () . "/mini_" . rawurlencode ($filename));
55 function image_url_from_imgpath ($filename) {
56 if (defined ("IMGSDIRURL") && (strlen (IMGSDIRURL) != 0)) {
57 return rtrim (IMGSDIRURL, '/') . "/" . rawurlencode ($filename);
60 return full_url_from_path (UPLOADDIR . "/" . rawurlencode ($filename));
63 function full_url_from_path ($path) {
66 while (substr($rel_path, 0, 2) == "./") { // strips ./
67 $rel_path = substr ($rel_path, 2);
70 if ($rel_path [0] == "/") {
73 $script_dir = dirname ($_SERVER ["SCRIPT_NAME"]);
74 while ((substr ($rel_path, 0, 3) == "../") &&
75 (strlen($script_dir) != 0)) {
76 $rel_path = substr ($rel_path, 3);
77 while (substr($rel_path, 0, 2) == "./") {
78 $rel_path = substr ($rel_path, 2);
80 $script_dir = substr ($script_dir, 0, strrpos ($script_dir, "/"));
82 if ((strlen ($script_dir) == 0) && (substr ($rel_path, 0, 3) == "../")) {
85 $path = "$script_dir/$rel_path";
89 $port = $_SERVER ["SERVER_PORT"];
90 if ($_SERVER ["HTTPS"] == "on") {
96 if (($port == "80" && $proto == "http") ||
97 ($port == "443" && $proto == "https")) {
103 return "$proto://$host$port$path";
106 function create_thumbnail_or_copy ($filename, $destfile) {
108 $thumbnail_ok = create_thumbnail ($filename, $destfile);
109 } catch (Exception $e) {
110 $thumbnail_ok = false;
112 if (!$thumbnail_ok) {
113 if (!copy ($filename, $destfile)) {
120 function create_thumbnail ($filename, $destfile) {
121 if (!function_exists ("imagecreatefromjpeg")
122 || !function_exists ("imagecreatefrompng")) {
125 $ext = strtolower (ext_safe ($filename));
126 if ($ext == "jpg" || $ext == "jpeg") {
127 $image = imagecreatefromjpeg ($filename);
128 } else if ($ext == "png") {
129 $image = imagecreatefrompng ($filename);
134 if ($image === false) {
138 if (defined (THUMBSMAXSIZE) && (THUMBSMAXSIZE > 0)) {
139 $thumbsmaxsize = THUMBSMAXSIZE;
141 $thumbsmaxsize = 400; // default value;
144 $width = imageSX ($image);
145 $height = imageSY ($image);
146 if (($width <= $thumbsmaxsize) || ($height <= $thumbsmaxsize)) {
150 if ($width > $height) {
151 $thumb_width = $thumbsmaxsize;
152 $thumb_height = $height * ($thumbsmaxsize / $width);
153 } else if ($width < $height) {
154 $thumb_width = $width * ($thumbsmaxsize / $height);
155 $thumb_height = $thumbsmaxsize;
156 } else if ($width == $height) {
157 $thumb_width = $thumbsmaxsize;
158 $thumb_height = $thumbsmaxsize;
161 $thumb_image = ImageCreateTrueColor ($thumb_width, $thumb_height);
162 if ($thumb_image === false) {
165 if (!imagecopyresampled ($thumb_image, $image, 0, 0, 0, 0,
166 $thumb_width, $thumb_height, $width, $height)) {
170 if ($ext == "jpg" || $ext == "jpeg") {
171 if (!imagejpeg ($thumb_image, $destfile, 100)) {
174 } else if ($ext == "png") {
175 if (!imagepng ($thumb_image, $destfile)) {
180 imagedestroy ($image);
181 imagedestroy ($thumb_image);
186 function delete_from_ftp ($file) {
187 $ftp_conn = ftp_connect (FTPSERVER);
188 $login = ftp_login ($ftp_conn, FTPUSER, FTPPASS);
189 if ((!$ftp_conn) || (!$login)) {
192 $target = sprintf ("%s/%s", FTPROOTDIR, $file);
193 $deleted = ftp_delete ($ftp_conn, $target);
198 ftp_close ($ftp_conn);
201 function send_to_ftp ($file) {
202 $ftp_conn = ftp_connect (FTPSERVER);
203 $login = ftp_login ($ftp_conn, FTPUSER, FTPPASS);
204 if ((!$ftp_conn) || (!$login)) {
207 $dest = sprintf ("%s/%s", FTPROOTDIR, $file);
208 $upload = ftp_put ($ftp_conn, $dest, $file, FTP_BINARY);
213 ftp_close ($ftp_conn);