WordPress在BuddyPress上传并裁切PNG透明头像时背景变黑问题解决 作者: Xiaoge 所属分类: 隽永东方博客 发表于: 2012 年 7 月 20 日 17141 标签: BuddyPress PNG透明 wordpress 短链接: http://dfsj.net/Wi9QU 这两天一个客户的项目涉及BuddyPress中上传png透明头像时候,变成了黑色背景的问题,通过研究和google搜索找到了如下解决方案,原因是WordPress的裁切函数写得不够兼容,有效的代码如下: function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $destfilename = false ) { if ( is_numeric( $src_file ) ) // Handle int as attachment ID $src_file = get_attached_file( $src_file ); $src = wp_load_image( $src_file ); if ( !is_resource( $src )) return $src; $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); if ( $src_abs ) { $src_w -= $src_x; $src_h -= $src_y; } list($width, $height, $orig_type) = getimagesize( $src_file ); if (function_exists(‘imageantialias’)) imageantialias( $dst, true ); imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); imagedestroy( $src ); // Free up memory // if ( ! $dst_file ) // $dst_file = str_replace( basename( $src_file ), ‘cropped-‘ . basename( $src_file ), $src_file ); // convert from full colors to index colors, like original PNG. if ( IMAGETYPE_PNG == $orig_type && !imageistruecolor( $dst ) ) imagetruecolortopalette( $dst, false, imagecolorstotal( $dst ) ); if ( IMAGETYPE_GIF == $orig_type ) { if ( !imagegif( $dst, $destfilename ) ) return new WP_Error(‘resize_path_invalid’, __( ‘Resize path invalid’ )); } elseif ( IMAGETYPE_PNG == $orig_type ) { if ( !imagepng( $dst, $destfilename ) ) return new WP_Error(‘resize_path_invalid’, __( ‘Resize path invalid’ )); } else { if ( !imagejpeg( $dst, $destfilename, apply_filters( ‘jpeg_quality’, 90, ‘wp_crop_image’ ) ) ) return new WP_Error(‘resize_path_invalid’, __( ‘Resize path invalid’ )); } //$dst_file = preg_replace( ‘/\\.[^\\.]+$/’, ‘.jpg’, $dst_file ); //if ( imagejpeg( $dst, $dst_file, apply_filters( ‘jpeg_quality’, 90, ‘wp_crop_image’ ) ) ) // return $dst_file; // else // return false; }
作者: Xiaoge 所属分类: 隽永东方博客 发表于: 2012 年 7 月 20 日 17141 标签: BuddyPress PNG透明 wordpress 短链接: http://dfsj.net/Wi9QU 这两天一个客户的项目涉及BuddyPress中上传png透明头像时候,变成了黑色背景的问题,通过研究和google搜索找到了如下解决方案,原因是WordPress的裁切函数写得不够兼容,有效的代码如下: function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $destfilename = false ) { if ( is_numeric( $src_file ) ) // Handle int as attachment ID $src_file = get_attached_file( $src_file ); $src = wp_load_image( $src_file ); if ( !is_resource( $src )) return $src; $dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); if ( $src_abs ) { $src_w -= $src_x; $src_h -= $src_y; } list($width, $height, $orig_type) = getimagesize( $src_file ); if (function_exists(‘imageantialias’)) imageantialias( $dst, true ); imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); imagedestroy( $src ); // Free up memory // if ( ! $dst_file ) // $dst_file = str_replace( basename( $src_file ), ‘cropped-‘ . basename( $src_file ), $src_file ); // convert from full colors to index colors, like original PNG. if ( IMAGETYPE_PNG == $orig_type && !imageistruecolor( $dst ) ) imagetruecolortopalette( $dst, false, imagecolorstotal( $dst ) ); if ( IMAGETYPE_GIF == $orig_type ) { if ( !imagegif( $dst, $destfilename ) ) return new WP_Error(‘resize_path_invalid’, __( ‘Resize path invalid’ )); } elseif ( IMAGETYPE_PNG == $orig_type ) { if ( !imagepng( $dst, $destfilename ) ) return new WP_Error(‘resize_path_invalid’, __( ‘Resize path invalid’ )); } else { if ( !imagejpeg( $dst, $destfilename, apply_filters( ‘jpeg_quality’, 90, ‘wp_crop_image’ ) ) ) return new WP_Error(‘resize_path_invalid’, __( ‘Resize path invalid’ )); } //$dst_file = preg_replace( ‘/\\.[^\\.]+$/’, ‘.jpg’, $dst_file ); //if ( imagejpeg( $dst, $dst_file, apply_filters( ‘jpeg_quality’, 90, ‘wp_crop_image’ ) ) ) // return $dst_file; // else // return false; }