<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][9623] trunk/src/bp-core: Extends the BP Attachment class for the avatars need</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/9623">9623</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/9623","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-03-17 18:20:07 +0000 (Tue, 17 Mar 2015)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Extends the BP Attachment class for the avatars need
`BP_Attachment_Avatar` is a new class that extends the `BP_Attachment` one to manage the avatar uploads.
It contains specific methods required by avatars, such as shrinking or cropping an image.
Props johnjamesjacoby, boonebgorges
See <a href="http://buddypress.trac.wordpress.org/ticket/6278">#6278</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcorebpcoreclassesphp">trunk/src/bp-core/bp-core-classes.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcbpcoreclassesclassbpattachmentavatarphp">trunk/src/bp-core/classes/class-bp-attachment-avatar.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcorebpcoreclassesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-classes.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-classes.php 2015-03-17 18:05:20 UTC (rev 9622)
+++ trunk/src/bp-core/bp-core-classes.php 2015-03-17 18:20:07 UTC (rev 9623)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,3 +22,4 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/classes/class-bp-recursive-query.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/classes/class-bp-media-extractor.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require dirname( __FILE__ ) . '/classes/class-bp-attachment.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require dirname( __FILE__ ) . '/classes/class-bp-attachment-avatar.php';
</ins></span></pre></div>
<a id="trunksrcbpcoreclassesclassbpattachmentavatarphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/bp-core/classes/class-bp-attachment-avatar.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/classes/class-bp-attachment-avatar.php (rev 0)
+++ trunk/src/bp-core/classes/class-bp-attachment-avatar.php 2015-03-17 18:20:07 UTC (rev 9623)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,255 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Core Avatars attachment class
+ *
+ * @package BuddyPress
+ * @subpackage Core
+ */
+
+// Exit if accessed directly
+defined( 'ABSPATH' ) || exit;
+
+/**
+ * BP Attachment Avatar class
+ *
+ * Extends BP Attachment to manage the avatar uploads
+ *
+ * @since BuddyPress (2.3.0)
+ */
+class BP_Attachment_Avatar extends BP_Attachment {
+
+ /**
+ * Construct Upload parameters
+ *
+ * @since BuddyPress (2.3.0)
+ *
+ * @see BP_Attachment::__construct() for list of parameters
+ * @uses bp_core_avatar_original_max_filesize()
+ * @uses BP_Attachment::__construct()
+ */
+ public function __construct() {
+ parent::__construct( array(
+ 'action' => 'bp_avatar_upload',
+ 'file_input' => 'file',
+ 'original_max_filesize' => bp_core_avatar_original_max_filesize(),
+
+ // Specific errors for avatars
+ 'upload_error_strings' => array(
+ 9 => sprintf( __( 'That photo is too big. Please upload one smaller than %s', 'buddypress' ), size_format( bp_core_avatar_original_max_filesize() ) ),
+ 10 => __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ),
+ ),
+ ) );
+ }
+
+ /**
+ * Set Upload Dir data for avatars
+ *
+ * @since BuddyPress (2.3.0)
+ *
+ * @uses bp_core_avatar_upload_path()
+ * @uses bp_core_avatar_url()
+ * @uses bp_upload_dir()
+ * @uses BP_Attachment::set_upload_dir()
+ */
+ public function set_upload_dir() {
+ if ( bp_core_avatar_upload_path() && bp_core_avatar_url() ) {
+ $this->upload_path = bp_core_avatar_upload_path();
+ $this->url = bp_core_avatar_url();
+ $this->upload_dir = bp_upload_dir();
+ } else {
+ parent::set_upload_dir();
+ }
+ }
+
+ /**
+ * Avatar specific rules
+ *
+ * Adds an error if the avatar size or type don't match BuddyPress needs
+ * The error code is the index of $upload_error_strings
+ *
+ * @since BuddyPress (2.3.0)
+ *
+ * @param array $file the temporary file attributes (before it has been moved)
+ * @uses bp_core_check_avatar_size()
+ * @uses bp_core_check_avatar_type()
+ * @return array the file with extra errors if needed
+ */
+ public function validate_upload( $file = array() ) {
+ // Bail if already an error
+ if ( ! empty( $file['error'] ) ) {
+ return $file;
+ }
+
+ // File size is too big
+ if ( ! bp_core_check_avatar_size( array( 'file' => $file ) ) ) {
+ $file['error'] = 9;
+
+ // File is of invalid type
+ } else if ( ! bp_core_check_avatar_type( array( 'file' => $file ) ) ) {
+ $file['error'] = 10;
+ }
+
+ // Return with error code attached
+ return $file;
+ }
+
+ /**
+ * Maybe shrink the attachment to fit maximum allowed width
+ *
+ * @since BuddyPress (2.3.0)
+ *
+ * @param string $file the absolute path to the file
+ * @uses bp_core_avatar_original_max_width()
+ * @uses wp_get_image_editor()
+ * @return mixed
+ */
+ public static function shrink( $file = '' ) {
+ // Get image size
+ $size = @getimagesize( $file );
+ $retval = false;
+
+ // Check image size and shrink if too large
+ if ( $size[0] > bp_core_avatar_original_max_width() ) {
+ $editor = wp_get_image_editor( $file );
+
+ if ( ! is_wp_error( $editor ) ) {
+ $editor->set_quality( 100 );
+
+ $resized = $editor->resize( bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false );
+ if ( ! is_wp_error( $resized ) ) {
+ $thumb = $editor->save( $editor->generate_filename() );
+ } else {
+ $retval = $resized;
+ }
+
+ // Check for thumbnail creation errors
+ if ( ( false === $retval ) && is_wp_error( $thumb ) ) {
+ $retval = $thumb;
+ }
+
+ // Thumbnail is good so proceed
+ if ( false === $retval ) {
+ $retval = $thumb;
+ }
+
+ } else {
+ $retval = $editor;
+ }
+ }
+
+ return $retval;
+ }
+
+ /**
+ * Check if the image dimensions are smaller than full avatar dimensions
+ *
+ * @since BuddyPress (2.3.0)
+ *
+ * @param string $file the absolute path to the file
+ * @uses bp_core_avatar_full_width()
+ * @uses bp_core_avatar_full_height()
+ * @return boolean
+ */
+ public static function is_too_small( $file = '' ) {
+ $uploaded_image = @getimagesize( $file );
+ $full_width = bp_core_avatar_full_width();
+ $full_height = bp_core_avatar_full_height();
+
+ if ( isset( $uploaded_image[0] ) && $uploaded_image[0] < $full_width || $uploaded_image[1] < $full_height ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Crop the avatar
+ *
+ * @since BuddyPress (2.3.0)
+ *
+ * @see BP_Attachment::crop for the list of parameters
+ * @param array $args
+ * @uses bp_core_fetch_avatar()
+ * @uses bp_core_delete_existing_avatar()
+ * @uses bp_core_avatar_full_width()
+ * @uses bp_core_avatar_full_height()
+ * @uses bp_core_avatar_dimension()
+ * @uses BP_Attachment::crop
+ * @return array the cropped avatars (full and thumb)
+ */
+ public function crop( $args = array() ) {
+ // Bail if the original file is missing
+ if ( empty( $args['original_file'] ) ) {
+ return false;
+ }
+
+ /**
+ * Original file is a relative path to the image
+ * eg: /avatars/1/avatar.jpg
+ */
+ $relative_path = $args['original_file'];
+ $absolute_path = $this->upload_path . $relative_path;
+
+ // Bail if the avatar is not available
+ if ( ! file_exists( $absolute_path ) ) {
+ return false;
+ }
+
+ if ( empty( $args['item_id'] ) ) {
+ $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', dirname( $absolute_path ), $args['item_id'], $args['object'], $args['avatar_dir'] );
+ } else {
+ $avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', $this->upload_path . '/' . $args['avatar_dir'] . '/' . $args['item_id'], $args['item_id'], $args['object'], $args['avatar_dir'] );
+ }
+
+ // Bail if the avatar folder is missing for this item_id
+ if ( ! file_exists( $avatar_folder_dir ) ) {
+ return false;
+ }
+
+ // Delete the existing avatar files for the object
+ $existing_avatar = bp_core_fetch_avatar( array(
+ 'object' => $args['object'],
+ 'item_id' => $args['item_id'],
+ 'html' => false,
+ ) );
+
+ /**
+ * Check that the new avatar doesn't have the same name as the
+ * old one before deleting
+ */
+ if ( ! empty( $existing_avatar ) && $existing_avatar !== $this->url . $relative_path ) {
+ bp_core_delete_existing_avatar( array( 'object' => $args['object'], 'item_id' => $args['item_id'], 'avatar_path' => $avatar_folder_dir ) );
+ }
+
+ // Make sure we at least have minimal data for cropping
+ if ( empty( $args['crop_w'] ) ) {
+ $args['crop_w'] = bp_core_avatar_full_width();
+ }
+
+ if ( empty( $args['crop_h'] ) ) {
+ $args['crop_h'] = bp_core_avatar_full_height();
+ }
+
+ // Get the file extension
+ $data = @getimagesize( $absolute_path );
+ $ext = $data['mime'] == 'image/png' ? 'png' : 'jpg';
+
+ $args['original_file'] = $absolute_path;
+ $args['src_abs'] = false;
+ $avatar_types = array( 'full' => '', 'thumb' => '' );
+
+ foreach ( $avatar_types as $key_type => $type ) {
+ $args['dst_w'] = bp_core_avatar_dimension( $key_type, 'width' );
+ $args['dst_h'] = bp_core_avatar_dimension( $key_type, 'height' );
+ $args['dst_file'] = $avatar_folder_dir . '/' . wp_hash( $absolute_path . time() ) . '-bp' . $key_type . '.' . $ext;
+
+ $avatar_types[ $key_type ] = parent::crop( $args );
+ }
+
+ // Remove the original
+ @unlink( $absolute_path );
+
+ // Return the full and thumb cropped avatars
+ return $avatar_types;
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>