Hello, there is a script for uploading images to the server, it works, but when I want to upload another copy of such pictures in a reduced form, it fails to load only regular images, I will lay out the entire script, and a part how I want to get a smaller copy, thanks in advance for your help.

$img_width = imageSX($_FILES["file"]["name"][$key]); $img_height = imageSY($_FILES["file"]["name"][$key]); $k = round($img_width/IMG_WIDTH,2); $img_mini_width = round($img_width/$k); $img_mini_height = round($img_height/$k); $img_dest_id = imagecreatetruecolor($img_mini_width, $img_mini_height); $mini = 'img'; $result = imagecopyresampled($img_dest_id, $img_id, 0, 0, 0, 0, $img_mini_width, $img_mini_height ); $mini = 'img/'; $img = imagejpeg($img_dest_id, $mini/$name, 100); 

and the whole script

 <?php require_once( $_SERVER['DOCUMENT_ROOT'] . '/registration/1/my_room/php/session.php'); require_once( $_SERVER['DOCUMENT_ROOT'] . '/bd.php'); header('Content-Type: application/json; charset=utf-8'); if(isset($_FILES)) { //пролистываСм вСсь массив ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡƒ $_FILES['file']['name'] as $k=>$v $error = array(); $success_list = []; $error_list = []; $uploads_dir = 'images/'; foreach ($_FILES['file']['error'] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["file"]["tmp_name"][$key]; $name = basename($_FILES["file"]["name"][$key]); $success = move_uploaded_file($tmp_name, "$uploads_dir/$name"); $img_width = imageSX($_FILES["file"]["name"][$key]); $img_height = imageSY($_FILES["file"]["name"][$key]); $k = round($img_width/IMG_WIDTH,2); $img_mini_width = round($img_width/$k); $img_mini_height = round($img_height/$k); $img_dest_id = imagecreatetruecolor($img_mini_width, $img_mini_height); $mini = 'img'; $result = imagecopyresampled($img_dest_id, $img_id, 0, 0, 0, 0, $img_mini_width, $img_mini_height ); $mini = 'img/'; $img = imagejpeg($img_dest_id, $mini/$name, 100); if ($success) { $success_list[] = $name; } else { $error_list[] = $name; } } else { $error_list[] = $_FILES['file']['name'][$key]; } } $error_list = implode(',', $error_list); $success_list = implode(',', $success_list); $sqlimage = "INSERT INTO images_big(login, catalog, filename) VALUES(:login, :catalog, :filename)"; $stmtimg = $pdo->prepare($sqlimage); $stmtimg->bindParam(':catalog', $uploads_dir, PDO::PARAM_STR); $stmtimg->bindParam(':filename', $success_list, PDO::PARAM_STR); $stmtimg->bindParam(':login', $_GET['login'], PDO::PARAM_STR); $stmtimg->execute(); } ?> 

    1 answer 1

    I can suggest using the following function:

     function resize($file_input, $file_output, $w_o, $h_o, $percent = false) { list($w_i, $h_i, $type) = getimagesize($file_input); if (!$w_i || !$h_i) { echo 'НСвозмоТно ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ Π΄Π»ΠΈΠ½Ρƒ ΠΈ ΡˆΠΈΡ€ΠΈΠ½Ρƒ изобраТСния'; return; } $types = array('','gif','jpeg','png'); $ext = $types[$type]; if ($ext) { $func = 'imagecreatefrom'.$ext; $img = $func($file_input); } else { echo 'НСкоррСктный Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ Ρ„Π°ΠΉΠ»Π°'; return; } if ($percent) { $w_o *= $w_i / 100; $h_o *= $h_i / 100; } if (!$h_o) $h_o = $w_o/($w_i/$h_i); if (!$w_o) $w_o = $h_o/($h_i/$w_i); $img_o = imagecreatetruecolor($w_o, $h_o); imagecopyresampled($img_o, $img, 0, 0, 0, 0, $w_o, $h_o, $w_i, $h_i); if ($type == 2) { return imagejpeg($img_o,$file_output,100); } else { $func = 'image'.$ext; return $func($img_o,$file_output); } } 

    In your case, the call will be something like this:

     resize($_FILES["file"]["name"], $_FILES["file"]["name"], 200, 200); 
    • Sorry for the annoying help, I’ll understand this variable a little bit more $ file_input, I’m sort of understood the size of the file, but I always wrote 'It’s impossible to get the length and width of the image' - Koly September
    • Do you pass $ _FILES ["file"] ["name"] [$ key] as the first and second parameter? - Neodev 2:49 pm
    • You need to transfer the original image as the first parameter and the result file as the second - Neodev
    • I do so pastebin.ru/46FWU0kd thanks for the help, sorry, if I do not understand - Koly
    • Try to use pastebin.ru/nyLGWLOM in this form, it should be saved in the same folder just before the name will be "small_". - Neodev