$API_URL = 'https://api.tinify.com/shrink'; /** * Parameters. */ $api_key = $argv[1] ?? null; $input = $argv[2] ?? dirname(__DIR__) . DIRECTORY_SEPARATOR . 'sprites' . DIRECTORY_SEPARATOR . 'raw'; $output = $argv[3] ?? dirname(__DIR__) . DIRECTORY_SEPARATOR . 'sprites' . DIRECTORY_SEPARATOR . 'compressed'; /** * Script. */ if (!is_null($api_key)) { // Defining the common cURL options $curl_options = [CURLOPT_BINARYTRANSFER => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_USERPWD => "api:" . $api_key, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_USERAGENT => "PHP/" . PHP_VERSION . " curl/" . curl_version()['version']]; // Obtaining and sorting all files located within the input path $files = glob($input . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . '*.png'); natSort($files); foreach ($files as $file) { // Creating the request $request = curl_init(); curl_setopt_array($request, $curl_options); curl_setopt($request, CURLOPT_URL, $API_URL); curl_setopt($request, CURLOPT_POSTFIELDS, file_get_contents($file)); // Executing it and saving the response $response = curl_exec($request); if (is_string($response)) { // Getting header size and ending the request $header_size = curl_getinfo($request, CURLINFO_HEADER_SIZE); curl_close($request); // Saving the decoded response body without headers $body = json_decode(subStr($response, $header_size)); if ($body) {
$string .= "\t\t\"presentation\" => " . (is_null($emoji['presentation']) ? "null" : "\"" . $emoji['presentation'] . "\"") . "," . PHP_EOL; $string .= "\t\t\"annotations\" => [" . PHP_EOL . "\t\t\t\"" . implode("\"," . PHP_EOL . "\t\t\t\"", $emoji['annotations']) . '"'; $string .= PHP_EOL . "\t\t]" . PHP_EOL; $string .= "\t]," . PHP_EOL; }); $string .= '];'; $string = str_replace("[\n\t\t\t\"\"\n\t\t]", '[]', $string); // Saving string to a file file_put_contents($output . 'emoji.php', $string); /** * Exporting to SQL. */ $values_emojis = []; $values_annotations = []; // Walking array to build SQL structure array_walk($emojis, function ($emoji) { global $values_emojis, $values_annotations; $values_emojis[] = "(" . $emoji['order'] . ", '" . implode('\\n', $emoji['code']) . "', '" . $emoji['value'] . "', '" . $emoji['name'] . "', " . $emoji['year'] . ", " . (is_null($emoji['presentation']) ? 'NULL' : "'" . $emoji['presentation'] . "'") . ")"; foreach ($emoji['annotations'] as $a) { $values_annotations[] = "('" . $a . "', '" . $emoji['value'] . "')"; } }); natSort($values_annotations); $string = "--\n-- Structure of the table `emojis`\n--\n\nCREATE TABLE `emojis` (\n\t`order` smallint(6) NOT NULL,\n\t`code` varchar(63) COLLATE utf8mb4_unicode_ci NOT NULL,\n\t`value` varchar(8) COLLATE utf8mb4_bin NOT NULL,\n\t`name` tinytext COLLATE utf8mb4_unicode_ci NOT NULL,\n\t`year` smallint(4) UNSIGNED NOT NULL,\n\t`presentation` set('emoji','text') COLLATE utf8mb4_unicode_ci NULL,\n\tPRIMARY KEY (`value`),\n\tUNIQUE KEY `code` (`code`)\n) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\n--\n-- Data of the table `emojis`\n--\n\nINSERT INTO `emojis` (`order`, `code`, `value`, `name`, `year`, `presentation`) VALUES" . PHP_EOL; $string .= implode(',' . PHP_EOL, $values_emojis); $string .= ';' . PHP_EOL . PHP_EOL; $string .= "--\n-- Structure of the table `annotations`\n--\n\nCREATE TABLE `annotations` (\n\t`annotation` varchar(30) COLLATE utf8mb4_bin NOT NULL,\n\t`emoji` varchar(8) COLLATE utf8mb4_bin NOT NULL,\n\tUNIQUE KEY `annotation` (`annotation`,`emoji`),\n\tFOREIGN KEY (`emoji`) REFERENCES emojis(`value`)\n) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;\n\n--\n-- Data of the table `annotations`\n--\n\nINSERT INTO `annotations` (`annotation`, `emoji`) VALUES" . PHP_EOL; $string .= implode(',' . PHP_EOL, $values_annotations); $string .= ';'; // Saving string to a file file_put_contents($output . 'emoji.sql', $string);