From 83c602b18fa56abe07b4c6995100328b7e781021 Mon Sep 17 00:00:00 2001 From: champsupertramp Date: Tue, 5 Apr 2016 21:25:39 +0800 Subject: [PATCH] Fix image png upload transparency --- core/um-files.php | 94 ++++++++++++++----------------------- core/um-short-functions.php | 25 +++++----- 2 files changed, 48 insertions(+), 71 deletions(-) diff --git a/core/um-files.php b/core/um-files.php index 92e5320e..46ef1c2d 100644 --- a/core/um-files.php +++ b/core/um-files.php @@ -481,7 +481,7 @@ class UM_Files { $info = @getimagesize( $file ); - + if ( $info['mime'] == 'image/gif' ){ $img_r = imagecreatefromgif( $file ); @@ -491,12 +491,12 @@ class UM_Files { } else if ( $info['mime'] == 'image/png' ){ - $srcImage = imagecreatefrompng( $file ); - $targetImage = imagecreatetruecolor( $targ_x2, $targ_y2 ); - imagealphablending( $targetImage, false ); - imagesavealpha( $targetImage, true ); - imagecopyresampled( $targetImage, $srcImage, 0, 0, 0, 0, $targ_x1, $targ_y1, $targ_x2, $targ_y2 ); - imagepng( $srcImage, $this->path_only( $file ) . basename( $file ) ); + $img_r = imagecreatefrompng( $file ); + $dst_r = imagecreatetruecolor( $targ_x2, $targ_y2 ); + imagealphablending( $dst_r, false); + imagesavealpha( $dst_r, true); + imagecopy( $dst_r, $img_r, 0, 0, $targ_x1, $targ_y1, $targ_x2, $targ_y2 ); + imagepng( $dst_r, $this->path_only( $file ) . basename( $file ) ); }else{ @@ -539,19 +539,8 @@ class UM_Files { wp_die( __('Unauthorized to do this attempt.','ultimatemember') ); } - // name and extension stuff - $source_name = basename( $source ); - - if ( $key == 'profile_photo' ) { - $source_name = 'profile_photo.jpg'; - } - - if ( $key == 'cover_photo' ) { - $source_name = 'cover_photo.jpg'; - } - - $ext = '.' . pathinfo($source_name, PATHINFO_EXTENSION); - $name = str_replace( $ext, '', $source_name ); + $ext = '.' . pathinfo($source, PATHINFO_EXTENSION); + $name = $key; $filename = $name . $ext; // copy & overwrite file @@ -565,11 +554,14 @@ class UM_Files { // thumbs if ( $key == 'profile_photo' ) { - list($w, $h) = @getimagesize( $source ); + list($w, $h) = @getimagesize( $source ); $sizes = um_get_option('photo_thumb_sizes'); foreach( $sizes as $size ) { + + $ratio = round( $w / $h, 2 ); + $height = round( $size / $ratio, 2 ); if ( file_exists( $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext ) ) { unlink( $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext ); @@ -581,30 +573,21 @@ class UM_Files { $thumb_s = imagecreatefromjpeg( $source ); $thumb = imagecreatetruecolor( $size, $size ); imagecopyresampled( $thumb, $thumb_s, 0, 0, 0, 0, $size, $size, $w, $h ); - + imagejpeg( $thumb, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext, 100); + imagejpeg( $thumb, $this->upload_basedir . $user_id . '/' . $name . $ext, 100); + }else if ( $info['mime'] == 'image/png' ){ + $thumb_s = imagecreatefrompng( $source ); + $thumb = imagecreatetruecolor( $size, $size ); + imagealphablending( $thumb, false); + imagesavealpha( $thumb, true); + imagecopyresampled( $thumb, $thumb_s, 0, 0, 0, 0, $size, $size, $w, $h ); + imagepng( $thumb, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext ); }else if ( $info['mime'] == 'image/gif' ){ $thumb_s = imagecreatefromgif( $source ); $thumb = imagecreatetruecolor( $size, $size ); imagecopyresampled( $thumb, $thumb_s, 0, 0, 0, 0, $size, $size, $w, $h ); - - } - - - if ( $info['mime'] == 'image/jpeg' ){ - imagejpeg( $thumb, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext, 100); - }else if ( $info['mime'] == 'image/png' ){ - - $srcImage = imagecreatefrompng( $source ); - $targetImage = imagecreatetruecolor( $size, $size ); - imagealphablending( $targetImage, false ); - imagesavealpha( $targetImage, true ); - imagecopyresampled( $targetImage, $srcImage, 0, 0, 0, 0, $size, $size, $w, $h ); - imagepng( $srcImage, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext, 0 ,PNG_ALL_FILTERS); - - }else if ( $info['mime'] == 'image/gif' ){ - imagegif( $thumb, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext); - + imagegif( $thumb, $this->upload_basedir . $user_id . '/' . $name . $ext); } } @@ -632,28 +615,21 @@ class UM_Files { if ( $size < $w ) { if ( $info['mime'] == 'image/jpeg' ){ - $thumb = imagecreatetruecolor( $size, $height ); - imagecopyresampled( $thumb, $thumb_s, 0, 0, 0, 0, $size, $height, $w, $h ); - $thumb_s = imagecreatefromjpeg( $source ); - }else if ( $info['mime'] == 'image/gif' ){ - $thumb = imagecreatetruecolor( $size, $height ); - imagecopyresampled( $thumb, $thumb_s, 0, 0, 0, 0, $size, $height, $w, $h ); - $thumb_s = imagecreatefromgif( $source ); - } - - - if ( $info['mime'] == 'image/jpeg' ){ + $thumb = imagecreatetruecolor( $size, $height ); + imagecopyresampled( $thumb, $thumb_s, 0, 0, 0, 0, $size, $height, $w, $h ); + $thumb_s = imagecreatefromjpeg( $source ); imagejpeg( $thumb, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext, 100); }else if ( $info['mime'] == 'image/png' ){ - - $srcImage = imagecreatefrompng( $source ); - $targetImage = imagecreatetruecolor( $size, $height ); - imagealphablending( $targetImage, false ); - imagesavealpha( $targetImage, true ); - imagecopyresampled( $targetImage, $srcImage, 0, 0, 0, 0, $size, $height, $w, $h ); - - imagepng($srcImage, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext, 0 ,PNG_ALL_FILTERS); + $thumb_s = imagecreatefrompng( $source ); + $thumb = imagecreatetruecolor( $size, $height ); + imagealphablending( $thumb, false); + imagesavealpha( $thumb, true); + imagecopyresampled( $thumb, $thumb_s, 0, 0, 0, 0, $size, $height, $w, $h ); + imagepng( $thumb, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext ); }else if ( $info['mime'] == 'image/gif' ){ + $thumb = imagecreatetruecolor( $size, $height ); + imagecopyresampled( $thumb, $thumb_s, 0, 0, 0, 0, $size, $height, $w, $h ); + $thumb_s = imagecreatefromgif( $source ); imagegif( $thumb, $this->upload_basedir . $user_id . '/' . $name . '-' . $size . $ext); } } diff --git a/core/um-short-functions.php b/core/um-short-functions.php index ccc0d2da..c6872eb0 100644 --- a/core/um-short-functions.php +++ b/core/um-short-functions.php @@ -1192,11 +1192,12 @@ function um_fetch_user( $user_id ) { function um_get_cover_uri( $image, $attrs ) { global $ultimatemember; $uri = false; - if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/cover_photo.jpg' ) ) { - $uri = um_user_uploads_uri() . 'cover_photo.jpg?' . current_time( 'timestamp' ); + $ext = '.' . pathinfo($image, PATHINFO_EXTENSION); + if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/cover_photo'.$ext ) ) { + $uri = um_user_uploads_uri() . 'cover_photo'.$ext.'?' . current_time( 'timestamp' ); } - if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/cover_photo-' . $attrs. '.jpg' ) ){ - $uri = um_user_uploads_uri() . 'cover_photo-'.$attrs.'.jpg?' . current_time( 'timestamp' ); + if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/cover_photo-' .$attrs.$ext ) ){ + $uri = um_user_uploads_uri() . 'cover_photo-'.$attrs.$ext.'?' . current_time( 'timestamp' ); } return $uri; } @@ -1216,28 +1217,28 @@ function um_fetch_user( $user_id ) { global $ultimatemember; $uri = false; $find = false; + $ext = '.' . pathinfo($image, PATHINFO_EXTENSION); + if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/profile_photo-' . $attrs. $ext ) ) { - if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/profile_photo-' . $attrs. '.jpg' ) ) { - - $uri = um_user_uploads_uri() . 'profile_photo-'.$attrs.'.jpg?' . current_time( 'timestamp' ); + $uri = um_user_uploads_uri() . 'profile_photo-'.$attrs.$ext.'?' . current_time( 'timestamp' ); } else { $sizes = um_get_option('photo_thumb_sizes'); if ( is_array( $sizes ) ) $find = um_closest_num( $sizes, $attrs ); - if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/profile_photo-' . $find. '.jpg' ) ) { + if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/profile_photo-' . $find.$ext ) ) { - $uri = um_user_uploads_uri() . 'profile_photo-'.$find.'.jpg?' . current_time( 'timestamp' ); + $uri = um_user_uploads_uri() . 'profile_photo-'.$find.$ext.'?' . current_time( 'timestamp' ); - } else if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/profile_photo.jpg' ) ) { + } else if ( file_exists( $ultimatemember->files->upload_basedir . um_user('ID') . '/profile_photo'.$ext ) ) { - $uri = um_user_uploads_uri() . 'profile_photo.jpg?' . current_time( 'timestamp' ); + $uri = um_user_uploads_uri() . 'profile_photo'.$ext.'?' . current_time( 'timestamp' ); } if ( $attrs == 'original' ) { - $uri = um_user_uploads_uri() . 'profile_photo.jpg?' . current_time( 'timestamp' ); + $uri = um_user_uploads_uri() . 'profile_photo'.$ext.'?' . current_time( 'timestamp' ); } }