$max_file_size) throw new Exception("File over allowed size of {$max_filesize_msg}"); // Get the extension of the file $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); $file_id = rand_str(6); // Convert webp files into jpg image if ($type == 'image/webp') { $new_file_name = "{$file_id}.jpg"; $new_thumb_name = "{$file_id}_thumb.jpg"; // Convert webp to jpeg with 80% quality and save to save path $im = imagecreatefromwebp($tmp_name); imagejpeg($im, $image_path . $new_file_name, 80); imagedestroy($im); } // Resize image if it's JPEG and more then 1200px in any side elseif ($type == 'image/jpeg' && ($image_info[0] > $max_image_size || $image_info[1] > $max_image_size)) { $new_file_name = "{$file_id}.jpg"; $new_thumb_name = "{$file_id}_thumb.jpg"; // Resize image to a smaller size $source = $tmp_name; $dest = $image_path . $new_file_name; resizeAndSaveImage($source, $dest, $max_image_size); } // Move the uploaded file to save path else { $new_file_name = "{$file_id}.{$file_ext}"; $new_thumb_name = "{$file_id}_thumb.{$file_ext}"; // Move the uploaded file to save path (for all other formats) move_uploaded_file($tmp_name, $image_path . $new_file_name); } // Generate thumbnails $source = $image_path . $new_file_name; $dest = $thumb_path . $new_thumb_name; resizeAndSaveImage($source, $dest, 150); // Generate the file url and reply $url = $protocol . $domain . $image_url . $new_file_name; $out = array("status" => "OK", "url" => $url); echo json_encode($out); } // Catch and print the exception catch (Exception $e) { $out = array("status" => "FAIL", "msg" => $e->getMessage()); echo json_encode($out); } } else { $v = "?v=" . rand(1111, 9999); ?>