} } } // Either SVG was requested or we've cached a SVG version and are ready to serve a regular format. $imagefile = $theme->resolve_image_location($image, $component, $usesvg); if (empty($imagefile) or !is_readable($imagefile)) { if (!file_exists($candidatelocation)) { @mkdir($candidatelocation, $CFG->directorypermissions, true); } // Make note we can not find this file. $cacheimage = "{$candidatelocation}/{$image}.error"; $fp = fopen($cacheimage, 'w'); fclose($fp); image_not_found(); } $cacheimage = cache_image($image, $imagefile, $candidatelocation); if (connection_aborted()) { die; } // Make sure nothing failed. clearstatcache(); if (file_exists($cacheimage)) { send_cached_image($cacheimage, $etag); } send_uncached_image($imagefile); //================================================================================= //=== utility functions == // we are not using filelib because we need to fine tune all header // parameters to get the best performance. function send_cached_image($imagepath, $etag) {
<?php include "config.php"; include "pdo.class.php"; include "functions.php"; $pdo = Db::singleton(); $body = cleanVar($_POST['Body'], 'text'); $from = cleanVar($_POST['From'], 'phone'); $media = ''; $numMedia = $_POST['NumMedia']; if ($numMedia > 0) { for ($i = 1; $i <= $numMedia; $i++) { $key = 'MediaUrl' . $i; $media = $_POST[$key]; if (isset($media) && !empty($media)) { $media = cache_image($media, $id); $res = $pdo->query("INSERT INTO callog SET msg='{$body}',phonenumber='{$from}',photo='{$media}',type='s'"); } } }
/** * resize_image * * resizes images to a given width and height. if * width is omitted height will be auto calculated * caching is also performed if cache is set to * true * * @params GDResource $image * @params int $width * @params int $height optional * @params bool $cache optional * @params string $path * @return GDResource */ function resize_image($image, $width, $height = false, $cache = false, $path = false) { $name = 'RESIZE' . $width . $height; if ($cache && cache_exists_image($name, $path)) { return cache_get_image($name, $path); } $old_width = imagesx($image); $old_height = imagesy($image); if ($height == false) { // calculate height list($width, $height) = resize_dimensions($width, $width, $old_width, $old_height); } // resize image $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $old_width, $old_height); if ($cache) { // cache the image cache_image($name, $new_image, $path); } return $new_image; }
function block($params) { global $mysqli, $set_404, $final_size, $cache_final_image, $final_image_modid, $final_image_type, $final_image_item, $final_image_size; $item = $params[0]; if ($item == "") { $im = imagecreatetruecolor(2048, 2048); // Transparentbackground imagealphablending($im, true); imagesavealpha($im, true); $trans = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $trans); return $im; } $number = null; if (preg_match("/^\\d{1,2}x/", $item)) { list($number, $item) = explode("x", $item, 2); } if (strpos($item, ":") === false) { $modid = "minecraft"; } else { list($modid, $item) = explode(":", $item); } if (strpos($item, ";") === false) { $meta = 0; } else { list($item, $meta) = explode(";", $item); $meta = min(15, max(0, intval($meta))); } $result = $mysqli->query("SELECT `Meta`, `RenderAs`, (SELECT `File` FROM `RenderTypes` WHERE `ID` = `RenderType` LIMIT 1) AS `RenderFile`, `Textures` FROM `RenderData` WHERE `ModID` = (SELECT `ID` FROM `ModIDs` WHERE `ModID` = '" . $mysqli->real_escape_string($modid) . "' LIMIT 1) AND `Name` = '" . $mysqli->real_escape_string($item) . "' AND (`Meta` = '*' OR `Meta` = '{$meta}') ORDER BY `Meta` ASC LIMIT 1"); if ($result->num_rows) { $row = $result->fetch_assoc(); if ($row["Meta"] != "*") { $item .= "_" . $row["Meta"]; } switch ($row["RenderAs"]) { case "Block": $im = image_from_cache($modid, "blocks", $item, $final_size); if ($im === null) { require_once "renderers/block_renderer.php"; list($left, $top, $right) = explode(",", $row["Textures"]); $im = render_block($left, $top, $right); cache_image($modid, "blocks", $item, $im, "base"); } $final_image_modid = $modid; $final_image_type = "blocks"; $final_image_item = $item; $final_image_size = $final_size; break; case "Item": require_once "renderers/item_renderer.php"; $im = image_from_cache($modid, "items", $item, $final_size); if ($im === null) { $im = render_item($row["Textures"]); } $final_image_modid = $modid; $final_image_type = "items"; $final_image_item = $item; $final_image_size = $final_size; break; } /*require_once("new_renderer.php"); $render_file = "render_scripts/$modid/" . $row["RenderFile"]; $render_data = json_decode(file_get_contents($render_file)); $renderer = new Renderer($render_data, explode(",", $row["Textures"])); $im = $renderer->render(); $size = imagesx($im); if($render_data->cache != 0) { $cache_path = "../cache/render/$modid/" . $render_data->cache; $cache_file = "$cache_path/$item.png"; @mkdir($cache_path, 0775, true); imagepng($im, $cache_file); }*/ } else { require_once "renderers/block_renderer.php"; $set_404 = true; $im = render_block("", "", ""); } if ($number !== null && $number != 1) { $width = imagettfbbox(768, 0, "../includes/css/fonts/Minecraftia.ttf", $number)[2]; $white = imagecolorallocate($im, 255, 255, 255); $black = imagecolorallocate($im, 63, 63, 63); imagettftext($im, 768, 0, 2176 - $width, 2048, $black, "../includes/css/fonts/Minecraftia.ttf", $number); imagettftext($im, 768, 0, 2048 - $width, 1920, $white, "../includes/css/fonts/Minecraftia.ttf", $number); $cache_final_image = false; } return $im; }
function fetch_image($url) { if (function_exists('curl_init')) { $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_HEADER, 0); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); curl_setopt($c, CURLOPT_MAXREDIRS, 5); curl_setopt($c, CURLOPT_TIMEOUT, 10); $image_data = curl_exec($c); $status = curl_getinfo($c, CURLINFO_HTTP_CODE); $mime_type = curl_getinfo($c, CURLINFO_CONTENT_TYPE); $image_size = curl_getinfo($c, CURLINFO_SIZE_DOWNLOAD); curl_close($c); } // TODO: fallback for non-cURL-enabled servers if (!in_array($status, array(200, 301, 302))) { error('Invalid image.'); } if ($image_size > MAX_IMAGE_SIZE) { error('Image is too large.'); } if (empty($mime_type) || !preg_match('/^image\\//', $mime_type)) { error('Invalid image type.'); } if (empty($image_data)) { error('Invalid image content.'); } $checksum = calculate_checksum($url); cache_image($image_data, $mime_type, $checksum); }