Skip to content

Commit

Permalink
Merge pull request Studio-42#382 from oyejorge/patch-2
Browse files Browse the repository at this point in the history
Fix Black Backgrounds
  • Loading branch information
dio-el-claire committed Sep 11, 2012
2 parents 14d6e17 + 7291266 commit cc36708
Showing 1 changed file with 78 additions and 109 deletions.
187 changes: 78 additions & 109 deletions php/elFinderVolumeDriver.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2811,45 +2811,17 @@ protected function imgResize($path, $width, $height, $keepProportions = false, $
break;

case 'gd':
if ($s['mime'] == 'image/jpeg') {
$img = imagecreatefromjpeg($path);
} elseif ($s['mime'] == 'image/png') {
$img = imagecreatefrompng($path);
} elseif ($s['mime'] == 'image/gif') {
$img = imagecreatefromgif($path);
} elseif ($s['mime'] == 'image/xbm') {
$img = imagecreatefromxbm($path);
}
$img = self::gdImageCreate($path,$s['mime']);

if ($img && false != ($tmp = imagecreatetruecolor($size_w, $size_h))) {

$bgcolor = $this->options['tmbBgColor'];

if ($bgcolor == 'transparent') {
list($r, $g, $b) = array(0, 0, 255);
} else {
list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
}

$bgcolor1 = imagecolorallocate($tmp, $r, $g, $b);

if ($bgcolor == 'transparent') {
$bgcolor1 = imagecolortransparent($tmp, $bgcolor1);
}

imagefill($tmp, 0, 0, $bgcolor1);

self::gdImageBackground($tmp,$this->options['tmbBgColor']);

if (!imagecopyresampled($tmp, $img, 0, 0, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
return false;
return false;
}

if ($destformat == 'jpg' || ($destformat == null && $s['mime'] == 'image/jpeg')) {
$result = imagejpeg($tmp, $path, 100);
} else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
$result = imagegif($tmp, $path, 7);
} else {
$result = imagepng($tmp, $path, 7);
}
$result = self::gdImage($tmp, $path, $destformat, $s['mime']);

imagedestroy($img);
imagedestroy($tmp);
Expand Down Expand Up @@ -2902,33 +2874,11 @@ protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
break;

case 'gd':
if ($s['mime'] == 'image/jpeg') {
$img = imagecreatefromjpeg($path);
} elseif ($s['mime'] == 'image/png') {
$img = imagecreatefrompng($path);
} elseif ($s['mime'] == 'image/gif') {
$img = imagecreatefromgif($path);
} elseif ($s['mime'] == 'image/xbm') {
$img = imagecreatefromxbm($path);
}
$img = self::gdImageCreate($path,$s['mime']);

if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {

$bgcolor = $this->options['tmbBgColor'];

if ($bgcolor == 'transparent') {
list($r, $g, $b) = array(0, 0, 255);
} else {
list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
}

$bgcolor1 = imagecolorallocate($tmp, $r, $g, $b);

if ($bgcolor == 'transparent') {
$bgcolor1 = imagecolortransparent($tmp, $bgcolor1);
}

imagefill($tmp, 0, 0, $bgcolor1);
self::gdImageBackground($tmp,$this->options['tmbBgColor']);

$size_w = $width;
$size_h = $height;
Expand All @@ -2942,13 +2892,7 @@ protected function imgCrop($path, $width, $height, $x, $y, $destformat = null) {
return false;
}

if ($destformat == 'jpg' || ($destformat == null && $s['mime'] == 'image/jpeg')) {
$result = imagejpeg($tmp, $path, 100);
} else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
$result = imagegif($tmp, $path, 7);
} else {
$result = imagepng($tmp, $path, 7);
}
$result = self::gdImage($tmp, $path, $destformat, $s['mime']);

imagedestroy($img);
imagedestroy($tmp);
Expand Down Expand Up @@ -3006,43 +2950,17 @@ protected function imgSquareFit($path, $width, $height, $align = 'center', $vali
break;

case 'gd':
if ($s['mime'] == 'image/jpeg') {
$img = imagecreatefromjpeg($path);
} elseif ($s['mime'] == 'image/png') {
$img = imagecreatefrompng($path);
} elseif ($s['mime'] == 'image/gif') {
$img = imagecreatefromgif($path);
} elseif ($s['mime'] == 'image/xbm') {
$img = imagecreatefromxbm($path);
}
$img = self::gdImageCreate($path,$s['mime']);

if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {

if ($bgcolor == 'transparent') {
list($r, $g, $b) = array(0, 0, 255);
} else {
list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
}

$bgcolor1 = imagecolorallocate($tmp, $r, $g, $b);

if ($bgcolor == 'transparent') {
$bgcolor1 = imagecolortransparent($tmp, $bgcolor1);
}

imagefill($tmp, 0, 0, $bgcolor1);
self::gdImageBackground($tmp,$bgcolor);

if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
return false;
}

if ($destformat == 'jpg' || ($destformat == null && $s['mime'] == 'image/jpeg')) {
$result = imagejpeg($tmp, $path, 100);
} else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
$result = imagegif($tmp, $path, 7);
} else {
$result = imagepng($tmp, $path, 7);
}
$result = self::gdImage($tmp, $path, $destformat, $s['mime']);

imagedestroy($img);
imagedestroy($tmp);
Expand Down Expand Up @@ -3088,28 +3006,14 @@ protected function imgRotate($path, $degree, $bgcolor = '#ffffff', $destformat =
break;

case 'gd':
if ($s['mime'] == 'image/jpeg') {
$img = imagecreatefromjpeg($path);
} elseif ($s['mime'] == 'image/png') {
$img = imagecreatefrompng($path);
} elseif ($s['mime'] == 'image/gif') {
$img = imagecreatefromgif($path);
} elseif ($s['mime'] == 'image/xbm') {
$img = imagecreatefromxbm($path);
}
$img = self::gdImageCreate($path,$s['mime']);

$degree = 360 - $degree;
list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
$bgcolor = imagecolorallocate($img, $r, $g, $b);
$tmp = imageRotate($img, $degree, (int)$bgcolor);

if ($destformat == 'jpg' || ($destformat == null && $s['mime'] == 'image/jpeg')) {
$result = imagejpeg($tmp, $path, 100);
} else if ($destformat == 'gif' || ($destformat == null && $s['mime'] == 'image/gif')) {
$result = imagegif($tmp, $path, 7);
} else {
$result = imagepng($tmp, $path, 7);
}
$result = self::gdImage($tmp, $path, $destformat, $s['mime']);

imageDestroy($img);
imageDestroy($tmp);
Expand Down Expand Up @@ -3184,6 +3088,71 @@ protected function rmTmb($stat) {
clearstatcache();
}
}

/**
* Create an gd image according to the specified mime type
*
* @param string $path image file
* @param string $mime
* @return gd image resource identifier
*/
protected function gdImageCreate($path,$mime){
switch($mime){
case 'image/jpeg':
return imagecreatefromjpeg($path);

case 'image/png':
return imagecreatefrompng($path);

case 'image/gif':
return imagecreatefromgif($path);

case 'image/xbm':
return imagecreatefromxbm($path);
}
return false;
}

/**
* Output gd image to file
*
* @param resource $image gd image resource
* @param string $filename The path to save the file to.
* @param string $destformat The Image type to use for $filename
* @param string $mime The original image mime type
*/
protected function gdImage($image, $filename, $destformat, $mime ){

if ($destformat == 'jpg' || ($destformat == null && $mime == 'image/jpeg')) {
return imagejpeg($image, $filename, 100);
}

if ($destformat == 'gif' || ($destformat == null && $mime == 'image/gif')) {
return imagegif($image, $filename, 7);
}

return imagepng($image, $filename, 7);
}

/**
* Assign the proper background to a gd image
*
* @param resource $image gd image resource
* @param string $bgcolor background color in #rrggbb format
*/
protected function gdImageBackground($image, $bgcolor){

if( $bgcolor == 'transparent' ){
imagesavealpha($image,true);
$bgcolor1 = imagecolorallocatealpha($image, 255, 255, 255, 127);

}else{
list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
$bgcolor1 = imagecolorallocate($image, $r, $g, $b);
}

imagefill($image, 0, 0, $bgcolor1);
}

/*********************** misc *************************/

Expand Down

0 comments on commit cc36708

Please sign in to comment.