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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| <?php
/* Note: This thumbnail creation script requires the GD PHP Extension.
If GD is not installed correctly PHP does not render this page correctly
and SWFUpload will get "stuck" never calling uploadSuccess or uploadError
*/
// Get the session Id passed from SWFUpload. We have to do this to work-around the Flash Player Cookie Bug
if (isset($_POST["PHPSESSID"])) {
session_id($_POST["PHPSESSID"]);
}
session_start();
// Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
header("HTTP/1.1 500 Internal Server Error");
echo "invalid upload";
exit(0);
}
// Get the image and create a thumbnail
$img = @imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
if (!$img) {
header("HTTP/1.1 500 Internal Server Error");
echo "could not create image handle";
exit(0);
}
//Upload
require("includes/class.php");
$A = new CImage;
$time = time();
mysql_query("INSERT INTO `imageids` (`state`, `date`) VALUES('false', '$time')");
$id = mysql_insert_id();
$image_name = $id."-".basename($_FILES["Filedata"]["name"]);
mysql_query("UPDATE `imageids` SET `state` = 'true' WHERE `id` = '$id'");
$old = $time - 15 * 60;
mysql_query("DELETE FROM `imageids` WHERE `date` < '$old' AND `state` = 'false'");
$patterns[0] = '/ /';
$patterns[1] = '/\'/';
$patterns[2] = '/\"/';
$patterns[3] = '/\+/';
$patterns[4] = '/\-/';
$replacements[0] = '-';
$replacements[1] = '-';
$replacements[2] = '-';
$replacements[3] = '-';
$replacements[4] = '-';
$image_name = preg_replace($patterns, $replacements, $image_name);
move_uploaded_file($_FILES["Filedata"]["tmp_name"], "images/uploaded/".$image_name);
//End of upload
$width = imageSX($img);
$height = imageSY($img);
if (!$width || !$height) {
header("HTTP/1.1 500 Internal Server Error");
echo "Invalid width or height";
exit(0);
}
// Build the thumbnail
$target_width = 100;
$target_height = 133;
$target_ratio = $target_width / $target_height;
$img_ratio = $width / $height;
if ($target_ratio > $img_ratio) {
$new_height = $target_height;
$new_width = $img_ratio * $target_height;
} else {
$new_height = $target_width / $img_ratio;
$new_width = $target_width;
}
if ($new_height > $target_height) {
$new_height = $target_height;
}
if ($new_width > $target_width) {
$new_height = $target_width;
}
$conf_query = mysql_query("SELECT * FROM `configuration` WHERE `id` = '1'");
$conf = mysql_fetch_assoc($conf_query);
$new_img = ImageCreateTrueColor($new_width, $new_height);
$color = imagecolorallocate($new_img, hexdec('0x' . $conf['content_color']{0} . $conf['content_color']{1}), hexdec('0x' . $conf['content_color']{2} . $conf['content_color']{3}), hexdec('0x' . $conf['content_color']{4} . $conf['content_color']{5}));
if (!@imagefilledrectangle($new_img, 0, 0, $new_width-1, $new_height-1, $color)) { // Fill the image black
header("HTTP/1.1 500 Internal Server Error");
echo "Could not fill new image";
exit(0);
}
if (!@imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height)) {
header("HTTP/1.0 500 Internal Server Error");
echo "Could not resize image";
exit(0);
}
if (!isset($_SESSION["file_info"])) {
$_SESSION["file_info"] = array();
}
// Use a output buffering to load the image into a variable
ob_start();
imagejpeg($new_img);
$imagevariable = ob_get_contents();
ob_end_clean();
$file_id = md5($_FILES["Filedata"]["tmp_name"] + rand()*100000);
//upload the thumb
$image_name = $id."-".basename($_FILES["Filedata"]["name"]);
$patterns[0] = '/ /';
$patterns[1] = '/\'/';
$patterns[2] = '/\"/';
$patterns[3] = '/\+/';
$patterns[4] = '/\-/';
$replacements[0] = '-';
$replacements[1] = '-';
$replacements[2] = '-';
$replacements[3] = '-';
$replacements[4] = '-';
$image_name = preg_replace($patterns, $replacements, $image_name);
move_uploaded_file($new_img, "images/thumbs/".$image_name);
$myFile = "images/thumbs/".$image_name;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $imagevariable);
fclose($fh);
//end of upload the thumb
$_SESSION["file_info"][$file_id] = $imagevariable;
$time = time();
@mysql_query("INSERT INTO `images` (`image`, `date`) VALUES('$image_name', '$time')");
$image_id = mysql_insert_id();
if ($_SESSION['logged'] == "true")
{
$session_id = session_id();
$query = mysql_query("SELECT * FROM `gallery` WHERE `user_id` = '$_SESSION[user_id]' AND `session_id` = '$session_id' LIMIT 1");
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_assoc($query);
mysql_query("UPDATE `gallery` SET `images` = CONCAT(`images`, ',$image_id') WHERE `id` = '$row[id]' LIMIT 1");
$gallery_id = $row['id'];
mysql_query("UPDATE `images` SET `gallery_id` = '$gallery_id' WHERE `id` = '$image_id' LIMIT 1");
}
else
{
mysql_query("INSERT INTO `gallery` (`images`, `user_id`, `session_id`) VALUES ('$image_id', '$_SESSION[user_id]', '$session_id')");
$gallery_id = mysql_insert_id();
mysql_query("UPDATE `images` SET `gallery_id` = '$gallery_id' WHERE `id` = '$image_id' LIMIT 1");
}
}
else
{
$session_id = session_id();
$query = mysql_query("SELECT * FROM `gallery` WHERE `session_id` = '$session_id' LIMIT 1");
if (mysql_num_rows($query) > 0)
{
$row = mysql_fetch_assoc($query);
mysql_query("UPDATE `gallery` SET `images` = CONCAT(`images`, ',$image_id') WHERE `id` = '$row[id]' LIMIT 1");
$gallery_id = $row['id'];
mysql_query("UPDATE `images` SET `gallery_id` = '$gallery_id' WHERE `id` = '$image_id' LIMIT 1");
}
else
{
$session_id = session_id();
mysql_query("INSERT INTO `gallery` (`images`, `session_id`) VALUES ('$image_id', '$session_id')");
$gallery_id = mysql_insert_id();
mysql_query("UPDATE `images` SET `gallery_id` = '$gallery_id' WHERE `id` = '$image_id' LIMIT 1");
}
}
echo $file_id; // Return the file id to the script
?> |
Partager