exec('BEGIN TRANSACTION'); $post_id = uuid_v4(); $post_time = time(); $images = $_FILES['image']; $num_files = count($images['name']); for ($i = 0; $i < $num_files; $i++) { $name = $images['name'][$i]; $mime_type = $images['type'][$i]; $tmp_file = $images['tmp_name'][$i]; $error = $images['error'][$i]; if($error != '0') { http_response_code(400); die('Upload error'); } $size = $images['size'][$i]; // imagemagick manip code $img = new Imagick($tmp_file); // no compression $img->setImageCompressionQuality(100); $profiles = $img->getImageProfiles("icc", true); $exif_orientation = $img->getImageOrientation(); // STRIP EXIF DATA $img->stripImage(); // REAPPLY EXIF DATA if($exif_orientation === imagick::ORIENTATION_BOTTOMRIGHT) { $img->rotateImage("#000", 180); } elseif ($exif_orientation === imagick::ORIENTATION_RIGHTTOP) { $img->rotateImage("#000", 90); } elseif ($exif_orientation === imagick::ORIENTATION_LEFTBOTTOM) { $img->rotateImage("#000", -90); } $img->setImageOrientation(imagick::ORIENTATION_TOPLEFT); if(!empty($profiles)) { // keep ICC for quality $img->profileImage('icc', $profiles['icc']); } $file_b64 = base64_encode($img->getImageBlob()); $pic_id = uuid_v4(); $upload_stmt = $db->prepare('INSERT into pics values (:id, :post_id, :mime_type, :b64_bytes)'); $upload_stmt->bindValue(':id', $pic_id); $upload_stmt->bindValue(':post_id', $post_id); $upload_stmt->bindValue(':mime_type', $mime_type); $upload_stmt->bindValue(':b64_bytes', $file_b64); $upload_stmt->execute(); echo "$name"; } $post_stmt = $db->prepare('INSERT into posts VALUES(:id, :user, :time)'); $post_stmt->bindValue(':id', $post_id); $post_stmt->bindValue(':user', $user); $post_stmt->bindValue(':time', $post_time); $comment_stmt = $db->prepare('INSERT into comments VALUES (:id, :post_id, :body)'); $comment_stmt->bindValue(':id', uuid_v4()); $comment_stmt->bindValue(':post_id', $post_id); $comment_stmt->bindValue(':body', $_POST['comment'] ?? ''); $comment_stmt->execute(); $post_stmt->execute(); $db->exec('COMMIT'); } else { echo '


'; }