= filemtime($source_file)) { return $cache_file; } // modified, clear it unlink($cache_file); } return generateImage($source_file, $cache_file, $resolution); } /* generates the given cache file for the given source file with the given resolution */ function generateImage($source_file, $cache_file, $resolution) { global $sharpen, $jpg_quality; $extension = strtolower(pathinfo($source_file, PATHINFO_EXTENSION)); // Check the image dimensions $dimensions = GetImageSize($source_file); $width = $dimensions[0]; $height = $dimensions[1]; // Do we need to downscale the image? if ($width <= $resolution) { // no, because the width of the source image is already less than the client width return $source_file; } // We need to resize the source image to the width of the resolution breakpoint we're working with $ratio = $height/$width; $new_width = $resolution; $new_height = ceil($new_width * $ratio); $dst = ImageCreateTrueColor($new_width, $new_height); // re-sized image switch ($extension) { case 'png': $src = @ImageCreateFromPng($source_file); // original image break; case 'gif': $src = @ImageCreateFromGif($source_file); // original image break; default: $src = @ImageCreateFromJpeg($source_file); // original image ImageInterlace($dst, true); // Enable interlancing (progressive JPG, smaller size file) break; } if($extension=='png'){ imagealphablending($dst, false); imagesavealpha($dst,true); $transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127); imagefilledrectangle($dst, 0, 0, $new_width, $new_height, $transparent); } ImageCopyResampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // do the resize in memory ImageDestroy($src); // sharpen the image? // NOTE: requires PHP compiled with the bundled version of GD (see http://php.net/manual/en/function.imageconvolution.php) if($sharpen == TRUE && function_exists('imageconvolution')) { $intSharpness = findSharp($width, $new_width); $arrMatrix = array( array(-1, -2, -1), array(-2, $intSharpness + 12, -2), array(-1, -2, -1) ); imageconvolution($dst, $arrMatrix, $intSharpness, 0); } $cache_dir = dirname($cache_file); // does the directory exist already? if (!is_dir($cache_dir)) { if (!mkdir($cache_dir, 0755, true)) { // check again if it really doesn't exist to protect against race conditions if (!is_dir($cache_dir)) { // uh-oh, failed to make that directory ImageDestroy($dst); sendErrorImage("Failed to create cache directory: $cache_dir"); } } } if (!is_writable($cache_dir)) { sendErrorImage("The cache directory is not writable: $cache_dir"); } // save the new file in the appropriate path, and send a version to the browser switch ($extension) { case 'png': $gotSaved = ImagePng($dst, $cache_file); break; case 'gif': $gotSaved = ImageGif($dst, $cache_file); break; default: $gotSaved = ImageJpeg($dst, $cache_file, $jpg_quality); break; } ImageDestroy($dst); if (!$gotSaved && !file_exists($cache_file)) { sendErrorImage("Failed to create image: $cache_file"); } return $cache_file; } // check if the file exists at all if (!file_exists($source_file)) { header("Status: 404 Not Found"); exit(); } /* check that PHP has the GD library available to use for image re-sizing */ if (!extension_loaded('gd')) { // it's not loaded if (!function_exists('dl') || !dl('gd.so')) { // and we can't load it either // no GD available, so deliver the image straight up trigger_error('You must enable the GD extension to make use of Adaptive Images', E_USER_WARNING); sendImage($source_file, $browser_cache); } } /* Check to see if a valid cookie exists */ if (isset($_COOKIE['resolution'])) { $cookie_value = $_COOKIE['resolution']; // does the cookie look valid? [whole number, comma, potential floating number] if (! preg_match("/^[0-9]+[,]*[0-9\.]+$/", "$cookie_value")) { // no it doesn't look valid setcookie("resolution", "$cookie_value", time()-100); // delete the mangled cookie } else { // the cookie is valid, do stuff with it $cookie_data = explode(",", $_COOKIE['resolution']); $client_width = (int) $cookie_data[0]; // the base resolution (CSS pixels) $total_width = $client_width; $pixel_density = 1; // set a default, used for non-retina style JS snippet if (@$cookie_data[1]) { // the device's pixel density factor (physical pixels per CSS pixel) $pixel_density = $cookie_data[1]; } rsort($resolutions); // make sure the supplied break-points are in reverse size order $resolution = $resolutions[0]; // by default use the largest supported break-point // if pixel density is not 1, then we need to be smart about adapting and fitting into the defined breakpoints if($pixel_density != 1) { $total_width = $client_width * $pixel_density; // required physical pixel width of the image // the required image width is bigger than any existing value in $resolutions if($total_width > $resolutions[0]){ // firstly, fit the CSS size into a break point ignoring the multiplier foreach ($resolutions as $break_point) { // filter down if ($total_width <= $break_point) { $resolution = $break_point; } } // now apply the multiplier $resolution = $resolution * $pixel_density; } // the required image fits into the existing breakpoints in $resolutions else { foreach ($resolutions as $break_point) { // filter down if ($total_width <= $break_point) { $resolution = $break_point; } } } } else { // pixel density is 1, just fit it into one of the breakpoints foreach ($resolutions as $break_point) { // filter down if ($total_width <= $break_point) { $resolution = $break_point; } } } } } /* No resolution was found (no cookie or invalid cookie) */ if (!$resolution) { // We send the lowest resolution for mobile-first approach, and highest otherwise $resolution = $is_mobile ? min($resolutions) : max($resolutions); } /* if the requested URL starts with a slash, remove the slash */ if(substr($requested_uri, 0,1) == "/") { $requested_uri = substr($requested_uri, 1); } /* whew might the cache file be? */ $cache_file = $document_root."/$cache_path/$resolution/".$requested_uri; /* Use the resolution value as a path variable and check to see if an image of the same name exists at that path */ if (file_exists($cache_file)) { // it exists cached at that size if ($watch_cache) { // if cache watching is enabled, compare cache and source modified dates to ensure the cache isn't stale $cache_file = refreshCache($source_file, $cache_file, $resolution); } sendImage($cache_file, $browser_cache); } /* It exists as a source file, and it doesn't exist cached - lets make one: */ $file = generateImage($source_file, $cache_file, $resolution); sendImage($file, $browser_cache);