From 5c880bc6cc34793c2eb87697e4ca66ca2e6e12f9 Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Wed, 4 Feb 2009 14:44:12 -0500 Subject: [PATCH] Fix for #1057; group logo transparecy (and pixilation) --- actions/grouplogo.php | 44 ++++++++++++++++++++++++++++-------------- classes/User_group.php | 27 ++++++++++++++++++++------ 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/actions/grouplogo.php b/actions/grouplogo.php index ba9cdfe2ab..d8f7a93532 100644 --- a/actions/grouplogo.php +++ b/actions/grouplogo.php @@ -364,7 +364,7 @@ class GrouplogoAction extends Action $this->mode = 'crop'; - $this->showForm(_('Pick a square area of the image to be your avatar'), + $this->showForm(_('Pick a square area of the image to be the logo.'), true); } @@ -377,7 +377,6 @@ class GrouplogoAction extends Action function cropLogo() { $user = common_current_user(); - $profile = $user->getProfile(); $filedata = $_SESSION['FILEDATA']; @@ -387,11 +386,6 @@ class GrouplogoAction extends Action return; } - $x = $this->arg('avatar_crop_x'); - $y = $this->arg('avatar_crop_y'); - $w = ($this->arg('avatar_crop_w')) ? $this->arg('avatar_crop_w') : $filedata['width']; - $h = ($this->arg('avatar_crop_h')) ? $this->arg('avatar_crop_h') : $filedata['height']; - $filepath = common_avatar_path($filedata['filename']); if (!file_exists($filepath)) { @@ -414,17 +408,39 @@ class GrouplogoAction extends Action return; } - $size = ($w > MAX_ORIGINAL) ? MAX_ORIGINAL : $w; + // If image is not being cropped assume pos & dimentions of original + $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0; + $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0; + $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width']; + $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height']; + $size = ($dest_w > MAX_ORIGINAL) ? MAX_ORIGINAL : $dest_w; + + common_debug("W = $dest_w, H = $dest_h, X = $dest_x, Y = $dest_y, size = $size"); $image_dest = imagecreatetruecolor($size, $size); + + if ($filedata['type'] == IMAGETYPE_GIF || $filedata['type'] == IMAGETYPE_PNG) { - $background = imagecolorallocate($image_dest, 0, 0, 0); - ImageColorTransparent($image_dest, $background); - imagealphablending($image_dest, false); + $transparent_idx = imagecolortransparent($image_src); - imagecopyresized($image_dest, $image_src, - 0, 0, $x, $y, - $size, $size, $w, $h); + if ($transparent_idx >= 0) { + + $transparent_color = imagecolorsforindex($image_src, $transparent_idx); + $transparent_idx = imagecolorallocate($image_dest, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); + imagefill($image_dest, 0, 0, $transparent_idx); + imagecolortransparent($image_dest, $transparent_idx); + + } elseif ($filedata['type'] == IMAGETYPE_PNG) { + + imagealphablending($image_dest, false); + $transparent = imagecolorallocatealpha($image_dest, 0, 0, 0, 127); + imagefill($image_dest, 0, 0, $transparent); + imagesavealpha($image_dest, true); + + } + } + + imagecopyresampled($image_dest, $image_src, 0, 0, $dest_x, $dest_y, $size, $size, $dest_w, $dest_h); $filename = common_avatar_filename($this->group->id, image_type_to_extension($filedata['type']), diff --git a/classes/User_group.php b/classes/User_group.php index 522dd81436..092c1bc347 100755 --- a/classes/User_group.php +++ b/classes/User_group.php @@ -132,14 +132,29 @@ class User_group extends Memcached_DataObject } $image_dest = imagecreatetruecolor($size, $size); + + if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_PNG) { - $background = imagecolorallocate($image_dest, 0, 0, 0); - ImageColorTransparent($image_dest, $background); - imagealphablending($image_dest, false); + $transparent_idx = imagecolortransparent($image_src); + + if ($transparent_idx >= 0) { + + $transparent_color = imagecolorsforindex($image_src, $transparent_idx); + $transparent_idx = imagecolorallocate($image_dest, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); + imagefill($image_dest, 0, 0, $transparent_idx); + imagecolortransparent($image_dest, $transparent_idx); + + } elseif ($type == IMAGETYPE_PNG) { + + imagealphablending($image_dest, false); + $transparent = imagecolorallocatealpha($image_dest, 0, 0, 0, 127); + imagefill($image_dest, 0, 0, $transparent); + imagesavealpha($image_dest, true); + + } + } - imagecopyresized($image_dest, $image_src, - 0, 0, 0, 0, - $size, $size, $info[0], $info[1]); + imagecopyresampled($image_dest, $image_src, 0, 0, 0, 0, $size, $size, $info[0], $info[1]); $outname = common_avatar_filename($this->id, image_type_to_extension($type),