function gif_loadFileToGDimageResource($gifFilename, $bgColor = -1) { if ($gif = gif_loadFile($gifFilename)) { @set_time_limit(300); // general strategy: convert raw data to PNG then convert PNG data to GD image resource $PNGdata = $gif->getPng($bgColor); if ($img = @ImageCreateFromString($PNGdata)) { // excellent - PNG image data successfully converted to GD image return $img; } elseif ($img = $gif->getGD_PixelPlotterVersion()) { // problem: ImageCreateFromString() didn't like the PNG image data. // This has been known to happen in PHP v4.0.6 // solution: take the raw image data and create a new GD image and plot // pixel-by-pixel on the GD image. This is extremely slow, but it does // work and a slow solution is better than no solution, right? :) return $img; } } return false; }
function resizeImageFile($source, $destination, $max_width, $max_height, $preferred_format = 0) { global $sourcedir; // Nothing to do without GD if (!checkGD()) { return false; } static $default_formats = array('1' => 'gif', '2' => 'jpeg', '3' => 'png', '6' => 'bmp', '15' => 'wbmp'); require_once $sourcedir . '/Subs-Package.php'; @ini_set('memory_limit', '90M'); $success = false; // Get the image file, we have to work with something after all $fp_destination = fopen($destination, 'wb'); if ($fp_destination && substr($source, 0, 7) == 'http://') { $fileContents = fetch_web_data($source); fwrite($fp_destination, $fileContents); fclose($fp_destination); $sizes = @getimagesize($destination); } elseif ($fp_destination) { $sizes = @getimagesize($source); $fp_source = fopen($source, 'rb'); if ($fp_source !== false) { while (!feof($fp_source)) { fwrite($fp_destination, fread($fp_source, 8192)); } fclose($fp_source); } else { $sizes = array(-1, -1, -1); } fclose($fp_destination); } else { $sizes = array(-1, -1, -1); } // Gif? That might mean trouble if gif support is not available. if ($sizes[2] == 1 && !function_exists('imagecreatefromgif') && function_exists('imagecreatefrompng')) { // Download it to the temporary file... use the special gif library... and save as png. if ($img = @gif_loadFile($destination) && gif_outputAsPng($img, $destination)) { $sizes[2] = 3; } } // A known and supported format? if (isset($default_formats[$sizes[2]]) && function_exists('imagecreatefrom' . $default_formats[$sizes[2]])) { $imagecreatefrom = 'imagecreatefrom' . $default_formats[$sizes[2]]; if ($src_img = @$imagecreatefrom($destination)) { resizeImage($src_img, $destination, imagesx($src_img), imagesy($src_img), $max_width === null ? imagesx($src_img) : $max_width, $max_height === null ? imagesy($src_img) : $max_height, true, $preferred_format); $success = true; } } return $success; }
function resizeImageURL($sourceURL, $destinationFile, $max_width, $max_height) { global $sourcedir; static $default_formats = array('1' => 'gif', '2' => 'jpeg', '3' => 'png', '6' => 'bmp', '15' => 'wbmp'); require_once $sourcedir . '/Subs-Package.php'; $success = false; $sizes = url_image_size($sourceURL); $fp_destination = fopen($destinationFile, 'wb'); if ($fp_destination && substr($sourceURL, 0, 7) == 'http://') { $fileContents = fetch_web_data($sourceURL); fwrite($fp_destination, $fileContents); fclose($fp_destination); } elseif ($fp_destination) { $fp_source = fopen($sourceURL, 'rb'); if ($fp_source !== false) { while (!feof($fp_source)) { fwrite($fp_destination, fread($fp_source, 8192)); } fclose($fp_source); } else { $sizes = array(-1, -1, -1); } fclose($fp_destination); } else { $sizes = array(-1, -1, -1); } // Gif? That might mean trouble if gif support is not available. if ($sizes[2] == 1 && !function_exists('imagecreatefromgif') && function_exists('imagecreatefrompng')) { // Download it to the temporary file... use the special gif library... and save as png. if ($img = @gif_loadFile($destinationFile) && gif_outputAsPng($img, $destinationFile)) { $sizes[2] = 3; } } // A known and supported format? if (isset($default_formats[$sizes[2]]) && function_exists('imagecreatefrom' . $default_formats[$sizes[2]])) { $imagecreatefrom = 'imagecreatefrom' . $default_formats[$sizes[2]]; if ($src_img = @$imagecreatefrom($destinationFile)) { resizeImage($src_img, $destinationFile, imagesx($src_img), imagesy($src_img), $max_width === null ? imagesx($src_img) : $max_width, $max_height === null ? imagesy($src_img) : $max_height, true); $success = true; } } return $success; }
function createThumbnail($source, $max_width, $max_height) { global $modSettings, $db_prefix, $gd2; $default_formats = array('1' => 'gif', '2' => 'jpeg', '3' => 'png', '6' => 'bmp', '15' => 'wbmp'); // Is GD installed....? $testGD = get_extension_funcs('gd'); // No GD? Resizing to nothing? Time to bail! if (empty($testGD) || empty($max_width) && empty($max_height)) { return false; } // Do we have GD 2, even? $gd2 = in_array('imagecreatetruecolor', $testGD) && function_exists('imagecreatetruecolor'); unset($testGD); $destName = $source . '_thumb.tmp'; // Ask for more memory: we need it for this, and it'll only happen once! @ini_set('memory_limit', '90M'); $success = false; $sizes = getimagesize($source); if (empty($sizes)) { return false; } // If we have to handle a gif, we might be able to... but maybe not :/. if ($sizes[2] == 1 && !function_exists('imagecreatefromgif') && function_exists('imagecreatefrompng')) { // Try out a temporary file, if possible... if ($img = @gif_loadFile($source) && gif_outputAsPng($img, $destName)) { if ($src_img = imagecreatefrompng($destName)) { resizeImage($src_img, $destName, imagesx($src_img), imagesy($src_img), $max_width, $max_height); $success = true; } } } elseif (isset($default_formats[$sizes[2]]) && function_exists('imagecreatefrom' . $default_formats[$sizes[2]])) { $imagecreatefrom = 'imagecreatefrom' . $default_formats[$sizes[2]]; if ($src_img = @$imagecreatefrom($source)) { resizeImage($src_img, $destName, imagesx($src_img), imagesy($src_img), $max_width, $max_height); $success = true; } } // Okay, we're done with the temporary stuff. $destName = substr($destName, 0, -4); if ($success && @rename($destName . '.tmp', $destName)) { return true; } else { @unlink($destName . '.tmp'); @touch($destName); return false; } }