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

@ -33,9 +33,11 @@ if (preg_match("/.png$/i", $_GET['filename'])) header('Content-type: image/png')
function str_split_php4( $text, $split = 1 ) {
// place each character of the string into and array
$array = array();
for ( $i=0; $i < strlen( $text ); ){
for ( $i=0; $i < strlen( $text ); )
{
$key = NULL;
for ( $j = 0; $j < $split; $j++, $i++ ) {
for ( $j = 0; $j < $split; $j++, $i++ )
{
$key .= $text[$i];
}
array_push( $array, $key );
@ -54,7 +56,10 @@ return $fname;
}
// 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.
// (We always put thumbnails in jpg for simplification)
@ -90,21 +95,29 @@ else // otherwise, generate thumbnail, send it and save it to file.
$xoord = 0;
$yoord = 0;
if ($_GET['size'] == "") $_GET['size'] = 120; //
if ($_GET['size'] == "")
{
$_GET['size'] = 120;
}
$imgsize = GetImageSize($_GET['filename']);
$width = $imgsize[0];
$height = $imgsize[1];
if ($width > $height) { // If the width is greater than the height its a horizontal picture
if ($width > $height) // If the width is greater than the height its a horizontal picture
{
$xoord = ceil(($width-$height)/2);
$width = $height; // Then we read a square frame that equals the width
} else {
}
else
{
$yoord = ceil(($height-$width)/2);
$height = $width;
}
// Rotate JPG pictures
if (preg_match("/.jpg$|.jpeg$/i", $_GET['filename'])) {
if (function_exists('exif_read_data') && function_exists('imagerotate')) {
if (preg_match("/.jpg$|.jpeg$/i", $_GET['filename']))
{
if (function_exists('exif_read_data') && function_exists('imagerotate'))
{
$exif = exif_read_data($_GET['filename']);
$ort = $exif['IFD0']['Orientation'];
$degrees = 0;
@ -122,6 +135,14 @@ else // otherwise, generate thumbnail, send it and save it to file.
}
$target = ImageCreatetruecolor($_GET['size'],$_GET['size']);
//if the picture can be transparent, add a white background instead a black
if (preg_match("/.gif$/i", $_GET['filename']) || preg_match("/.png$/i", $_GET['filename']))
{
$backgroundColor = imagecolorallocate($target, 255, 255, 255);
imagefill($target, 0, 0, $backgroundColor);
}
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']);
@ -139,8 +160,11 @@ else // otherwise, generate thumbnail, send it and save it to file.
$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 ($fd)
{
fwrite($fd,$cachedImage);
fclose($fd);
}
}
?>