if the picture is transparent, bg is whiste instead of black

This commit is contained in:
tmos 2014-05-04 20:28:06 +02:00
parent 831ec2f00f
commit 5b5ce1faac

View file

@ -15,8 +15,8 @@ Community: www.minigal.dk/forum
Please enjoy this free script! Please enjoy this free script!
Version 0.3.5 modified by Sebastien SAUVAGE (sebsauvage.net): Version 0.3.5 modified by Sebastien SAUVAGE (sebsauvage.net):
- Added thumbnail cache (reduces server CPU load, server bandwith and speeds up client page display). - Added thumbnail cache (reduces server CPU load, server bandwith and speeds up client page display).
- Thumbnails are now always in JPEG even if the source image is PNG or GIF. - Thumbnails are now always in JPEG even if the source image is PNG or GIF.
USAGE EXAMPLE: USAGE EXAMPLE:
File: createthumb.php File: createthumb.php
@ -31,42 +31,47 @@ if (preg_match("/.png$/i", $_GET['filename'])) header('Content-type: image/png')
*/ */
function str_split_php4( $text, $split = 1 ) { function str_split_php4( $text, $split = 1 ) {
// place each character of the string into and array // place each character of the string into and array
$array = array(); $array = array();
for ( $i=0; $i < strlen( $text ); ){ for ( $i=0; $i < strlen( $text ); )
$key = NULL; {
for ( $j = 0; $j < $split; $j++, $i++ ) { $key = NULL;
$key .= $text[$i]; for ( $j = 0; $j < $split; $j++, $i++ )
} {
array_push( $array, $key ); $key .= $text[$i];
} }
return $array; array_push( $array, $key );
}
return $array;
} }
function sanitize($name) function sanitize($name)
{ {
// Sanitize image filename (taken from http://iamcam.wordpress.com/2007/03/20/clean-file-names-using-php-preg_replace/ ) // Sanitize image filename (taken from http://iamcam.wordpress.com/2007/03/20/clean-file-names-using-php-preg_replace/ )
$fname=$name; $fname=$name;
$replace="_"; $replace="_";
$pattern="/([[:alnum:]_\.-]*)/"; $pattern="/([[:alnum:]_\.-]*)/";
$fname=str_replace(str_split_php4(preg_replace($pattern,$replace,$fname)),$replace,$fname); $fname=str_replace(str_split_php4(preg_replace($pattern,$replace,$fname)),$replace,$fname);
return $fname; return $fname;
} }
// Make sure the "thumbs" directory exists. // Make sure the "thumbs" directory exists.
if (!is_dir('thumbs')) { mkdir('thumbs',0700); } if (!is_dir('thumbs'))
{
mkdir('thumbs',0700);
}
// Thumbnail file name and path. // Thumbnail file name and path.
// (We always put thumbnails in jpg for simplification) // (We always put thumbnails in jpg for simplification)
$thumbname = 'thumbs/'.sanitize($_GET['filename']).'.jpg'; $thumbname = 'thumbs/'.sanitize($_GET['filename']).'.jpg';
if (file_exists($thumbname)) // If thumbnail exists, serve it. if (file_exists($thumbname))// If thumbnail exists, serve it.
{ {
$fd = fopen($thumbname, "r"); $fd = fopen($thumbname, "r");
$cacheContent = fread($fd,filesize ($thumbname)); $cacheContent = fread($fd,filesize ($thumbname));
fclose($fd); fclose($fd);
header('Content-type: image/jpeg'); header('Content-type: image/jpeg');
echo($cacheContent); echo($cacheContent);
} }
else // otherwise, generate thumbnail, send it and save it to file. else // otherwise, generate thumbnail, send it and save it to file.
{ {
@ -90,57 +95,76 @@ else // otherwise, generate thumbnail, send it and save it to file.
$xoord = 0; $xoord = 0;
$yoord = 0; $yoord = 0;
if ($_GET['size'] == "") $_GET['size'] = 120; // if ($_GET['size'] == "")
$imgsize = GetImageSize($_GET['filename']); {
$width = $imgsize[0]; $_GET['size'] = 120;
$height = $imgsize[1]; }
if ($width > $height) { // If the width is greater than the height its a horizontal picture $imgsize = GetImageSize($_GET['filename']);
$xoord = ceil(($width-$height)/2); $width = $imgsize[0];
$width = $height; // Then we read a square frame that equals the width $height = $imgsize[1];
} else { if ($width > $height) // If the width is greater than the height its a horizontal picture
$yoord = ceil(($height-$width)/2); {
$height = $width; $xoord = ceil(($width-$height)/2);
} $width = $height; // Then we read a square frame that equals the width
}
else
{
$yoord = ceil(($height-$width)/2);
$height = $width;
}
// Rotate JPG pictures // Rotate JPG pictures
if (preg_match("/.jpg$|.jpeg$/i", $_GET['filename'])) { if (preg_match("/.jpg$|.jpeg$/i", $_GET['filename']))
if (function_exists('exif_read_data') && function_exists('imagerotate')) { {
if (function_exists('exif_read_data') && function_exists('imagerotate'))
{
$exif = exif_read_data($_GET['filename']); $exif = exif_read_data($_GET['filename']);
$ort = $exif['IFD0']['Orientation']; $ort = $exif['IFD0']['Orientation'];
$degrees = 0; $degrees = 0;
switch($ort) switch($ort)
{ {
case 6: // 90 rotate right case 6: // 90 rotate right
$degrees = 270; $degrees = 270;
break; break;
case 8: // 90 rotate left case 8: // 90 rotate left
$degrees = 90; $degrees = 90;
break; break;
} }
if ($degrees != 0) $target = imagerotate($target, $degrees, 0); if ($degrees != 0) $target = imagerotate($target, $degrees, 0);
} }
} }
$target = ImageCreatetruecolor($_GET['size'],$_GET['size']); $target = ImageCreatetruecolor($_GET['size'],$_GET['size']);
if (preg_match("/.jpg$/i", $_GET['filename'])) $source = ImageCreateFromJPEG($_GET['filename']);
if (preg_match("/.gif$/i", $_GET['filename'])) $source = ImageCreateFromGIF($_GET['filename']);
if (preg_match("/.png$/i", $_GET['filename'])) $source = ImageCreateFromPNG($_GET['filename']);
imagecopyresampled($target,$source,0,0,$xoord,$yoord,$_GET['size'],$_GET['size'],$width,$height);
imagedestroy($source);
//if (preg_match("/.jpg$/i", $_GET['filename'])) ImageJPEG($target,null,90); //if the picture can be transparent, add a white background instead a black
//if (preg_match("/.gif$/i", $_GET['filename'])) ImageGIF($target,null,90); if (preg_match("/.gif$/i", $_GET['filename']) || preg_match("/.png$/i", $_GET['filename']))
//if (preg_match("/.png$/i", $_GET['filename'])) ImageJPEG($target,null,90); // Using ImageJPEG on purpose {
ob_start(); // Start output buffering. $backgroundColor = imagecolorallocate($target, 255, 255, 255);
header('Content-type: image/jpeg'); // We always render the thumbnail in JPEG even if the source is GIF or PNG. imagefill($target, 0, 0, $backgroundColor);
ImageJPEG($target,null,80); }
imagedestroy($target);
$cachedImage = ob_get_contents(); // Get the buffer content.
ob_end_flush();// End buffering
$fd = fopen($thumbname, "w"); // Save buffer to disk
if ($fd) { fwrite($fd,$cachedImage); fclose($fd); }
if (preg_match("/.jpg$/i", $_GET['filename'])) $source = ImageCreateFromJPEG($_GET['filename']);
if (preg_match("/.gif$/i", $_GET['filename'])) $source = ImageCreateFromGIF($_GET['filename']);
if (preg_match("/.png$/i", $_GET['filename'])) $source = ImageCreateFromPNG($_GET['filename']);
imagecopyresampled($target,$source,0,0,$xoord,$yoord,$_GET['size'],$_GET['size'],$width,$height);
imagedestroy($source);
//if (preg_match("/.jpg$/i", $_GET['filename'])) ImageJPEG($target,null,90);
//if (preg_match("/.gif$/i", $_GET['filename'])) ImageGIF($target,null,90);
//if (preg_match("/.png$/i", $_GET['filename'])) ImageJPEG($target,null,90); // Using ImageJPEG on purpose
ob_start(); // Start output buffering.
header('Content-type: image/jpeg'); // We always render the thumbnail in JPEG even if the source is GIF or PNG.
ImageJPEG($target,null,80);
imagedestroy($target);
$cachedImage = ob_get_contents(); // Get the buffer content.
ob_end_flush();// End buffering
$fd = fopen($thumbname, "w"); // Save buffer to disk
if ($fd)
{
fwrite($fd,$cachedImage);
fclose($fd);
}
} }
?> ?>