6 |
kaklik |
1 |
<?php
|
|
|
2 |
/*************************
|
|
|
3 |
Coppermine Photo Gallery
|
|
|
4 |
************************
|
|
|
5 |
Copyright (c) 2003-2005 Coppermine Dev Team
|
|
|
6 |
v1.1 originaly written by Gregory DEMAR
|
|
|
7 |
|
|
|
8 |
This program is free software; you can redistribute it and/or modify
|
|
|
9 |
it under the terms of the GNU General Public License as published by
|
|
|
10 |
the Free Software Foundation; either version 2 of the License, or
|
|
|
11 |
(at your option) any later version.
|
|
|
12 |
********************************************
|
|
|
13 |
Coppermine version: 1.3.3
|
|
|
14 |
$Source: /cvsroot/coppermine/stable/include/picmgmt.inc.php,v $
|
|
|
15 |
$Revision: 1.10 $
|
|
|
16 |
$Author: gaugau $
|
|
|
17 |
$Date: 2005/04/19 03:17:11 $
|
|
|
18 |
**********************************************/
|
|
|
19 |
|
|
|
20 |
// Add a picture to an album
|
|
|
21 |
function add_picture($aid, $filepath, $filename, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '',$iwidth=0,$iheight=0)
|
|
|
22 |
{
|
|
|
23 |
global $CONFIG, $ERROR, $USER_DATA, $PIC_NEED_APPROVAL;
|
|
|
24 |
global $lang_errors;
|
|
|
25 |
|
|
|
26 |
$image = $CONFIG['fullpath'] . $filepath . $filename;
|
|
|
27 |
$normal = $CONFIG['fullpath'] . $filepath . $CONFIG['normal_pfx'] . $filename;
|
|
|
28 |
$thumb = $CONFIG['fullpath'] . $filepath . $CONFIG['thumb_pfx'] . $filename;
|
|
|
29 |
|
|
|
30 |
if (!is_known_filetype($image)) {
|
|
|
31 |
return false;
|
|
|
32 |
} elseif (is_image($filename)) {
|
|
|
33 |
if (!file_exists($thumb)) {
|
|
|
34 |
if (!resize_image($image, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use']))
|
|
|
35 |
return false;
|
|
|
36 |
}
|
|
|
37 |
$imagesize = getimagesize($image);
|
|
|
38 |
if (max($imagesize[0], $imagesize[1]) > $CONFIG['picture_width'] && $CONFIG['make_intermediate'] && !file_exists($normal)) {
|
|
|
39 |
if (!resize_image($image, $normal, $CONFIG['picture_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use']))
|
|
|
40 |
return false;
|
|
|
41 |
}
|
|
|
42 |
} else {
|
|
|
43 |
$imagesize[0] = $iwidth;
|
|
|
44 |
$imagesize[1] = $iheight;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$image_filesize = filesize($image);
|
|
|
48 |
$total_filesize = is_image($filename) ? ($image_filesize + (file_exists($normal) ? filesize($normal) : 0) + filesize($thumb)) : ($image_filesize);
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
// Test if disk quota exceeded
|
|
|
52 |
if (!GALLERY_ADMIN_MODE && $USER_DATA['group_quota']) {
|
|
|
53 |
$result = db_query("SELECT sum(total_filesize) FROM {$CONFIG['TABLE_PICTURES']}, {$CONFIG['TABLE_ALBUMS']} WHERE {$CONFIG['TABLE_PICTURES']}.aid = {$CONFIG['TABLE_ALBUMS']}.aid AND category = '" . (FIRST_USER_CAT + USER_ID) . "'");
|
|
|
54 |
$record = mysql_fetch_array($result);
|
|
|
55 |
$total_space_used = $record[0];
|
|
|
56 |
mysql_free_result($result);
|
|
|
57 |
|
|
|
58 |
if ((($total_space_used + $total_filesize)>>10) > $USER_DATA['group_quota'] ) {
|
|
|
59 |
@unlink($image);
|
|
|
60 |
if (is_image($image)) {
|
|
|
61 |
@unlink($normal);
|
|
|
62 |
@unlink($thumb);
|
|
|
63 |
}
|
|
|
64 |
$msg = strtr($lang_errors['quota_exceeded'], array('[quota]' => ($USER_DATA['group_quota']),
|
|
|
65 |
'[space]' => ($total_space_used >> 10)));
|
|
|
66 |
cpg_die(ERROR, $msg, __FILE__, __LINE__);
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
// Test if picture requires approval
|
|
|
70 |
if (GALLERY_ADMIN_MODE) {
|
|
|
71 |
$approved = 'YES';
|
|
|
72 |
} elseif (!$USER_DATA['priv_upl_need_approval'] && $category == FIRST_USER_CAT + USER_ID) {
|
|
|
73 |
$approved = 'YES';
|
|
|
74 |
} elseif (!$USER_DATA['pub_upl_need_approval']) {
|
|
|
75 |
$approved = 'YES';
|
|
|
76 |
} else {
|
|
|
77 |
$approved = 'NO';
|
|
|
78 |
}
|
|
|
79 |
$PIC_NEED_APPROVAL = ($approved == 'NO');
|
|
|
80 |
// User ID is now recorded when in admin mode (casper)
|
|
|
81 |
$user_id = USER_ID;
|
|
|
82 |
$username= USER_NAME;
|
|
|
83 |
$query = "INSERT INTO {$CONFIG['TABLE_PICTURES']} (pid, aid, filepath, filename, filesize, total_filesize, pwidth, pheight, ctime, owner_id, owner_name, title, caption, keywords, approved, user1, user2, user3, user4, pic_raw_ip, pic_hdr_ip) VALUES ('', '$aid', '" . addslashes($filepath) . "', '" . addslashes($filename) . "', '$image_filesize', '$total_filesize', '{$imagesize[0]}', '{$imagesize[1]}', '" . time() . "', '$user_id', '$username','$title', '$caption', '$keywords', '$approved', '$user1', '$user2', '$user3', '$user4', '$raw_ip', '$hdr_ip')";
|
|
|
84 |
$result = db_query($query);
|
|
|
85 |
|
|
|
86 |
return $result;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
define("GIS_GIF", 1);
|
|
|
90 |
define("GIS_JPG", 2);
|
|
|
91 |
define("GIS_PNG", 3);
|
|
|
92 |
|
|
|
93 |
// Add 'edit' directory if it doesn't exist
|
|
|
94 |
// Set access to read+write only
|
|
|
95 |
if (!is_dir($CONFIG['fullpath'].'edit')) {
|
|
|
96 |
$cpg_umask = umask(0);
|
|
|
97 |
@mkdir($CONFIG['fullpath'].'edit',0777);
|
|
|
98 |
umask($cpg_umask);
|
|
|
99 |
unset($cpg_umask);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* resize_image()
|
|
|
104 |
*
|
|
|
105 |
* Create a file containing a resized image
|
|
|
106 |
*
|
|
|
107 |
* @param $src_file the source file
|
|
|
108 |
* @param $dest_file the destination file
|
|
|
109 |
* @param $new_size the size of the square within which the new image must fit
|
|
|
110 |
* @param $method the method used for image resizing
|
|
|
111 |
* @return 'true' in case of success
|
|
|
112 |
*/
|
|
|
113 |
function resize_image($src_file, $dest_file, $new_size, $method, $thumb_use)
|
|
|
114 |
{
|
|
|
115 |
global $CONFIG, $ERROR;
|
|
|
116 |
global $lang_errors;
|
|
|
117 |
|
|
|
118 |
$imginfo = getimagesize($src_file);
|
|
|
119 |
if ($imginfo == null)
|
|
|
120 |
return false;
|
|
|
121 |
// GD can only handle JPG & PNG images
|
|
|
122 |
if ($imginfo[2] != GIS_JPG && $imginfo[2] != GIS_PNG && ($method == 'gd1' || $method == 'gd2')) {
|
|
|
123 |
$ERROR = $lang_errors['gd_file_type_err'];
|
|
|
124 |
return false;
|
|
|
125 |
}
|
|
|
126 |
// height/width
|
|
|
127 |
$srcWidth = $imginfo[0];
|
|
|
128 |
$srcHeight = $imginfo[1];
|
|
|
129 |
if ($thumb_use == 'ht') {
|
|
|
130 |
$ratio = $srcHeight / $new_size;
|
|
|
131 |
} elseif ($thumb_use == 'wd') {
|
|
|
132 |
$ratio = $srcWidth / $new_size;
|
|
|
133 |
} else {
|
|
|
134 |
$ratio = max($srcWidth, $srcHeight) / $new_size;
|
|
|
135 |
}
|
|
|
136 |
$ratio = max($ratio, 1.0);
|
|
|
137 |
$destWidth = (int)($srcWidth / $ratio);
|
|
|
138 |
$destHeight = (int)($srcHeight / $ratio);
|
|
|
139 |
// Method for thumbnails creation
|
|
|
140 |
switch ($method) {
|
|
|
141 |
case "im" :
|
|
|
142 |
if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) {
|
|
|
143 |
// get the basedir, remove '/include'
|
|
|
144 |
$cur_dir = substr(dirname(__FILE__), 0, -8);
|
|
|
145 |
$src_file = '"' . $cur_dir . '\\' . strtr($src_file, '/', '\\') . '"';
|
|
|
146 |
$im_dest_file = str_replace('%', '%%', ('"' . $cur_dir . '\\' . strtr($dest_file, '/', '\\') . '"'));
|
|
|
147 |
} else {
|
|
|
148 |
$src_file = escapeshellarg($src_file);
|
|
|
149 |
$im_dest_file = str_replace('%', '%%', escapeshellarg($dest_file));
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
$output = array();
|
|
|
153 |
/*
|
|
|
154 |
* Hack for working with ImageMagick on WIndows even if IM is installed in C:\Program Files.
|
|
|
155 |
* By Aditya Mooley <aditya@sanisoft.com>
|
|
|
156 |
*/
|
|
|
157 |
if (eregi("win",$_ENV['OS'])) {
|
|
|
158 |
$cmd = "\"".str_replace("\\","/", $CONFIG['impath'])."convert\" -quality {$CONFIG['jpeg_qual']} {$CONFIG['im_options']} -geometry {$destWidth}x{$destHeight} ".str_replace("\\","/" ,$src_file )." ".str_replace("\\","/" ,$im_dest_file );
|
|
|
159 |
exec ("\"$cmd\"", $output, $retval);
|
|
|
160 |
} else {
|
|
|
161 |
$cmd = "{$CONFIG['impath']}convert -quality {$CONFIG['jpeg_qual']} {$CONFIG['im_options']} -geometry {$destWidth}x{$destHeight} $src_file $im_dest_file";
|
|
|
162 |
exec ($cmd, $output, $retval);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
if ($retval) {
|
|
|
167 |
$ERROR = "Error executing ImageMagick - Return value: $retval";
|
|
|
168 |
if ($CONFIG['debug_mode']) {
|
|
|
169 |
// Re-execute the command with the backtit operator in order to get all outputs
|
|
|
170 |
// will not work is safe mode is enabled
|
|
|
171 |
$output = `$cmd 2>&1`;
|
|
|
172 |
$ERROR .= "<br /><br /><div align=\"left\">Cmd line : <br /><font size=\"2\">" . nl2br(htmlspecialchars($cmd)) . "</font></div>";
|
|
|
173 |
$ERROR .= "<br /><br /><div align=\"left\">The convert program said:<br /><font size=\"2\">";
|
|
|
174 |
$ERROR .= nl2br(htmlspecialchars($output));
|
|
|
175 |
$ERROR .= "</font></div>";
|
|
|
176 |
}
|
|
|
177 |
@unlink($dest_file);
|
|
|
178 |
return false;
|
|
|
179 |
}
|
|
|
180 |
break;
|
|
|
181 |
|
|
|
182 |
case "gd1" :
|
|
|
183 |
if (!function_exists('imagecreatefromjpeg')) {
|
|
|
184 |
cpg_die(CRITICAL_ERROR, 'PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed', __FILE__, __LINE__);
|
|
|
185 |
}
|
|
|
186 |
if ($imginfo[2] == GIS_JPG)
|
|
|
187 |
$src_img = imagecreatefromjpeg($src_file);
|
|
|
188 |
else
|
|
|
189 |
$src_img = imagecreatefrompng($src_file);
|
|
|
190 |
if (!$src_img) {
|
|
|
191 |
$ERROR = $lang_errors['invalid_image'];
|
|
|
192 |
return false;
|
|
|
193 |
}
|
|
|
194 |
$dst_img = imagecreate($destWidth, $destHeight);
|
|
|
195 |
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
|
|
|
196 |
imagejpeg($dst_img, $dest_file, $CONFIG['jpeg_qual']);
|
|
|
197 |
imagedestroy($src_img);
|
|
|
198 |
imagedestroy($dst_img);
|
|
|
199 |
break;
|
|
|
200 |
|
|
|
201 |
case "gd2" :
|
|
|
202 |
if (!function_exists('imagecreatefromjpeg')) {
|
|
|
203 |
cpg_die(CRITICAL_ERROR, 'PHP running on your server does not support the GD image library, check with your webhost if ImageMagick is installed', __FILE__, __LINE__);
|
|
|
204 |
}
|
|
|
205 |
if (!function_exists('imagecreatetruecolor')) {
|
|
|
206 |
cpg_die(CRITICAL_ERROR, 'PHP running on your server does not support GD version 2.x, please switch to GD version 1.x on the config page', __FILE__, __LINE__);
|
|
|
207 |
}
|
|
|
208 |
if ($imginfo[2] == GIS_JPG)
|
|
|
209 |
$src_img = imagecreatefromjpeg($src_file);
|
|
|
210 |
else
|
|
|
211 |
$src_img = imagecreatefrompng($src_file);
|
|
|
212 |
if (!$src_img) {
|
|
|
213 |
$ERROR = $lang_errors['invalid_image'];
|
|
|
214 |
return false;
|
|
|
215 |
}
|
|
|
216 |
$dst_img = imagecreatetruecolor($destWidth, $destHeight);
|
|
|
217 |
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int)$destHeight, $srcWidth, $srcHeight);
|
|
|
218 |
imagejpeg($dst_img, $dest_file, $CONFIG['jpeg_qual']);
|
|
|
219 |
imagedestroy($src_img);
|
|
|
220 |
imagedestroy($dst_img);
|
|
|
221 |
break;
|
|
|
222 |
}
|
|
|
223 |
// Set mode of uploaded picture
|
|
|
224 |
chmod($dest_file, octdec($CONFIG['default_file_mode']));
|
|
|
225 |
// We check that the image is valid
|
|
|
226 |
$imginfo = getimagesize($dest_file);
|
|
|
227 |
if ($imginfo == null) {
|
|
|
228 |
$ERROR = $lang_errors['resize_failed'];
|
|
|
229 |
@unlink($dest_file);
|
|
|
230 |
return false;
|
|
|
231 |
} else {
|
|
|
232 |
return true;
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
?>
|