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
| <?php
session_start();
include "./config.php";
error_reporting(0);
$date = date('Y-m-d H:i:s');
$sujet = $_POST['sujet'];
$texte = $_POST['texte'];
$image = $_FILES['File1']["name"];
$statut = $_POST['statut'];
############ Configuration ##############
$config["image_max_size"] = 500; //Maximum image size (height and width)
$config["thumbnail_size"] = 200; //Thumbnails will be cropped to 200x200 pixels
$config["thumbnail_prefix"] = "thumb_"; //Normal thumb Prefix
$config["destination_folder"] = './uploads/'; //upload directory ends with / (slash)
$config["thumbnail_destination_folder"] = './uploads/'; //upload directory ends with / (slash)
$config["upload_url"] = "http://www.site.com/admin/uploads/";
$config["quality"] = 90; //jpeg quality
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
exit; //try detect AJAX request, simply exist if no Ajax
}
if(!isset($_FILES['File1']) || !is_uploaded_file($_FILES['File1']['tmp_name'][0])){
die('Image file is Missing!');
}
count total files in array
$file_count = count($_FILES["File1"]["name"]);
if($file_count > 0){ //there are more than one file? no problem let's handle multiple files
for ($x = 0; $x < $file_count; $x++){ //Loop through each uploaded file
//if there's file error, display it
if ($_FILES["File1"]['error'][$x] > 0) {
print get_upload_error($x);
exit;
}
//Get image info from a valid image file
$im_info = getimagesize($_FILES["File1"]["tmp_name"][$x]);
if($im_info){
$im["image_width"] = $im_info[0]; //image width
$im["image_height"] = $im_info[1]; //image height
$im["image_type"] = $im_info['mime']; //image type
}else{
die("Make sure image <b>".$_FILES["File1"]["name"][$x]."</b> is valid image file!");
}
//create image resource using Image type and set the file extension
switch($im["image_type"]){
case 'image/png':
$img_res = imagecreatefrompng($_FILES["File1"]["tmp_name"][$x]);
$file_extension = ".png";
break;
case 'image/gif':
$img_res = imagecreatefromgif($_FILES["File1"]["tmp_name"][$x]);
$file_extension = ".gif";
break;
case 'image/jpeg':
case 'image/pjpeg':
$img_res = imagecreatefromjpeg($_FILES["File1"]["tmp_name"][$x]);
$file_extension = ".jpg";
break;
default:
$img_res = 0;
}
//set our file variables
$unique_id = uniqid(); //unique id for random filename
$new_file_name = $unique_id . $file_extension;
$destination_file_save = $config["destination_folder"] . $new_file_name; //file path to destination folder
$destination_thumbnail_save = $config["thumbnail_destination_folder"] . $config["thumbnail_prefix"]. $new_file_name; //file path to destination thumb folder
if($img_res){
###### resize Image ########
//Construct a proportional size of new image
$image_scale = min($config["image_max_size"]/$im["image_width"], $config["image_max_size"]/$im["image_height"]);
$new_width = ceil($image_scale * $im["image_width"]);
$new_height = ceil($image_scale * $im["image_height"]);
//Create a new true color image
$canvas = imagecreatetruecolor($new_width, $new_height);
$resample = imagecopyresampled($canvas, $img_res, 0, 0, 0, 0, $new_width, $new_height, $im["image_width"], $im["image_height"]);
if($resample){
$save_image = save_image_file($im["image_type"], $canvas, $destination_file_save, $config["quality"]); //save image
if($save_image){
print '<img src="'.$config["upload_url"] . $new_file_name. '" />'; //output image to browser
print '<div id="n1">'.$new_file_name. '</div>'; //output image to browser
}
}
if(is_resource($canvas)){
imagedestroy($canvas); //free any associated memory
}
###### Generate Thumbnail ########
//Offsets
if( $im["image_width"] > $im["image_height"]){
$y_offset = 0;
$x_offset = ($im["image_width"] - $im["image_height"]) / 2;
$s_size = $im["image_width"] - ($x_offset * 2);
}else{
$x_offset = 0;
$y_offset = ($im["image_height"] - $im["image_width"]) / 2;
$s_size = $im["image_height"] - ($y_offset * 2);
}
//Create a new true color image
$canvas = imagecreatetruecolor($config["thumbnail_size"], $config["thumbnail_size"]);
$resample = imagecopyresampled($canvas, $img_res, 0, 0, $x_offset, $y_offset, $config["thumbnail_size"], $config["thumbnail_size"], $s_size, $s_size);
if($resample){
$query2 = "INSERT INTO weart_actu SET image ='$destination_file_save' , image_g = '$destination_thumbnail_save'";
$result2 = mysqli_query($db,$query2) or die(mysql_error());
$idimage = $db->insert_id;
$save_image = save_image_file($im["image_type"], $canvas, $destination_thumbnail_save, $config["quality"] );
if($save_image){
print '<img src="'.$config["upload_url"] . $config["thumbnail_prefix"]. $new_file_name. '" />';
print '<div id="n2">'.$config["thumbnail_prefix"]. $new_file_name. '</div>'; //output image to browser
}
}
if(is_resource($canvas)){
imagedestroy($canvas); //free any associated memory
}
}
}
}
//funcion to save image file
function save_image_file($image_type, $canvas, $destination, $quality){
switch(strtolower($image_type)){
case 'image/png':
return imagepng($canvas, $destination); //save png file
case 'image/gif':
return imagegif($canvas, $destination); //save gif file
case 'image/jpeg': case 'image/pjpeg':
return imagejpeg($canvas, $destination, $quality); //save jpeg file
default:
return false;
}
}
function get_upload_error($err_no){
switch($err_no){
case 1 : return 'The uploaded file exceeds the upload_max_filesize directive in php.ini.';
case 2 : return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.';
case 3 : return 'The uploaded file was only partially uploaded.';
case 4 : return 'No file was uploaded.';
case 5 : return 'Missing a temporary folder. Introduced in PHP 5.0.3';
case 6 : return 'Failed to write file to disk. Introduced in PHP 5.1.0';
}
}
$query = "INSERT INTO weart_actu SET image ='$image',titre ='$sujet', texte ='$texte', validite ='$statut', date='$date' ";
$result = mysqli_query($db,$query) or die(mysql_error());
?> |
Partager