=======================================================================================================================================
Modification Title: Attachment Image Size Control

Modification Version: 1.0

Modification Author: John Briggs

Modification Contributor: Karl Davis

Modification Site: http://www.xmbgarage.com

Modification Description: This modification will provide controls in admin panel to set max width and height ratio for attachment images.

Modification Copyright:  2009 John Briggs. All rights reserved.

Modification Compatibility: XMB 1.9.8 SP3/SP4

Modification Install Note: Before adding this modification to your forum you should back up all files related to this modification.

Modification License Note: This modification is released under the GPL v3 License. A copy is provided with this software package.

Modification Author Note: This modification is developed and released for use with XMB 1.9.8 SP3/SP4 which is provided by XMBGarage.com.

=======================================================================================================================================
=======
Step 1:
=======
===================================
Go To Administration Panel --> Insert raw SQL
===================================

=========================
Paste In Code And Click Submit:
=========================

ALTER TABLE `$table_attachments` ADD `fileheight` varchar(5) NOT NULL default '';
ALTER TABLE `$table_attachments` ADD `filewidth` varchar(5) NOT NULL default '';
ALTER TABLE `$table_settings` ADD `max_attheight` smallint(5) NOT NULL default '640';
ALTER TABLE `$table_settings` ADD `max_attwidth` smallint(5) NOT NULL default '480';
UPDATE `$table_attachments` SET fileheight='640' WHERE fileheight='';
UPDATE `$table_attachments` SET filewidth='480' WHERE filewidth='';

=======================================================================================================================================
=======
Step 2:
=======
=======================
Edit File: lang/English.lang.php
=======================

============================
Add Code To End Of File Above ?>
============================

// Attachment Image Size Control Mod Begin
$lang['max_attheight'] = "Maximum height ration in pixels for attachments to display in posts.";
$lang['max_attwidth'] = "Maximum width ration in pixels for attachments to display in posts.";
$lang['thumbEnlarge'] = "Click Image To Enlarge";
// Attachment Image Size Control Mod End

=======================================================================================================================================
=======
Step 3:
=======
==============
Edit File: cp.php
==============
==========
Find Code:
==========

        printsetting1($lang['attachimginpost'], 'attachimgpostnew', $attachimgposton, $attachimgpostoff);

===============
Add Code Below:
===============

        // Attachment Image Size Control Mod Begin
        printsetting2($lang['max_attheight'], 'max_attheightnew', ((int)$SETTINGS['max_attheight']), 3);
        printsetting2($lang['max_attwidth'], 'max_attwidthnew', ((int)$SETTINGS['max_attwidth']), 3);
        // Attachment Image Size Control Mod End

==========
Find Code:
==========

        $onlinetoday_statusnew = formOnOff('onlinetoday_statusnew');

===============
Add Code Below:
===============

        $max_attheightnew = formInt('max_attheightnew');
        $max_attwidthnew = formInt('max_attwidthnew');

==========
Find Code:
==========

        $db->query("UPDATE ".X_PREFIX."settings SET

==================
Replace Code With:
==================

        $db->query("UPDATE ".X_PREFIX."settings SET
            max_attheight='$max_attheightnew',
            max_attwidth='$max_attwidthnew',

=======================================================================================================================================
=======
Step 4:
=======
=======================
Edit File: include/functions.php
=======================
==========
Find Code:
==========

function get_attached_file($file, $attachstatus, $max_size=1000000) {
    global $lang, $filename, $filetype, $filesize;

    $filename = $filetype = '';
    $filesize = 0;

    if ($file['name'] != 'none' && !empty($file['name']) && $attachstatus != 'off' && is_uploaded_file($file['tmp_name'])) {
        $file['name'] = trim($file['name']);
        if (!isValidFilename($file['name'])) {
            error($lang['invalidFilename'], false, '', '', false, false, false, false);
            return false;
        }

        $filesize = intval(filesize($file['tmp_name'])); // fix bad filesizes
        if ($file['size'] > $max_size) {
            error($lang['attachtoobig'], false, '', '', false, false, false, false);
            return false;
        } else {
            $attachment = addslashes(fread(fopen($file['tmp_name'], 'rb'), filesize($file['tmp_name'])));
            $filename = addslashes($file['name']);
            $filetype = addslashes(preg_replace('#[\r\n%]#', '', $file['type']));

            if ($filesize == 0) {
                return false;
            } else {
                return $attachment;
            }
        }
    } else {
        return false;
    }
}

==================
Replace Code With:
==================

function get_attached_file($file, $attachstatus, $max_size=1000000) {
    global $db, $forum, $lang, $filename, $filetype, $filesize, $fileheight, $filewidth;

    $filename = '';
    $filetype = '';
    $fileheight = '';
    $filewidth = '';
    $filesize = 0;

    if (is_array($file) && $file['name'] != 'none' && !empty($file['name']) && $forum['attachstatus'] != 'off' && is_uploaded_file($file['tmp_name'])) {
        if (!isValidFilename($file['name'])) {
            error($lang['invalidFilename'], false, '', '', false, false, false, false);
            return false;
        }

        $filesize = intval(filesize($file['tmp_name'])); // fix bad filesizes
        if ($file['size'] > $max_size) {
            error($lang['attachtoobig'], false, '', '', false, false, false, false);
            return false;
        } else {
            $attachment = addslashes(fread(fopen($file['tmp_name'], 'rb'), filesize($file['tmp_name'])));
            $filename = checkInput($file['name']);
            $filetype = checkInput($file['type']);
            $filesize = checkInput($file['size']);

            $extention = strtolower(substr(strrchr($file['name'],'.'),1));
            if ($extention == 'jpg' || $extention == 'jpeg' || $extention == 'gif' || $extention == 'png' || $extention == 'bmp') {
                $exsize = getimagesize($file['tmp_name']);
                $fileheight = $exsize[1];
                $filewidth = $exsize[0];
            }

            if ($filesize == 0) {
                return false;
            } else {
                return $attachment;
            }
        }
    } else {
        return false;
    }
}

=======================================================================================================================================
=======
Step 5:
=======
================
Edit File: header.php
================
==========
Find Code:
==========

$filesize = 0;
$filename = '';
$filetype = '';

===============
Add Code Below:
===============

// Attachment Image Size Control Mod Begin
$fileheight = '';
$filewidth = '';
// Attachment Image Size Control Mod End

=======================================================================================================================================
=======
Step 6:
=======
===============
Edit File: post.php
===============
==========
Find Code:
==========

                if (isset($_FILES['attach']) && ($attachedfile = get_attached_file($_FILES['attach'], $forums['attachstatus'], $SETTINGS['maxattachsize'])) !== false) {
                    $db->query("INSERT INTO ".X_PREFIX."attachments (tid, pid, filename, filetype, filesize, attachment, downloads) VALUES ($tid, $pid, '$filename', '$filetype', '$filesize', '$attachedfile', 0)");
                }

================
Replace Code With:
================

                if (isset($_FILES['attach']) && ($attachedfile = get_attached_file($_FILES['attach'], $forums['attachstatus'], $SETTINGS['maxattachsize'])) !== false) {
                    $db->query("INSERT INTO ".X_PREFIX."attachments (tid, pid, filename, filetype, filesize, attachment, downloads, fileheight, filewidth) VALUES ($tid, $pid, '$filename', '$filetype', '$filesize', '$attachedfile', 0, '$fileheight', '$filewidth')");
                }

==========
Find Code:
==========

            if (isset($_FILES['attach']) && ($attachedfile = get_attached_file($_FILES['attach'], $forums['attachstatus'], $SETTINGS['maxattachsize'])) !== false) {
                $db->query("INSERT INTO ".X_PREFIX."attachments (tid, pid, filename, filetype, filesize, attachment, downloads) VALUES ($tid, $pid, '$filename', '$filetype', '$filesize', '$attachedfile', 0)");
            }

================
Replace Code With:
================

            if (isset($_FILES['attach']) && ($attachedfile = get_attached_file($_FILES['attach'], $forums['attachstatus'], $SETTINGS['maxattachsize'])) !== false) {
                $db->query("INSERT INTO ".X_PREFIX."attachments (tid, pid, filename, filetype, filesize, attachment, downloads, fileheight, filewidth) VALUES ($tid, $pid, '$filename', '$filetype', '$filesize', '$attachedfile', 0, '$fileheight', '$filewidth')");
            }

==========
Find Code:
==========

$query = $db->query("SELECT filename, filesize, downloads FROM ".X_PREFIX."attachments WHERE pid='$pid' AND tid='$tid'");

================
Replace Code With:
================

$query = $db->query("SELECT filename, filesize, downloads, fileheight, filewidth FROM ".X_PREFIX."attachments WHERE pid='$pid' AND tid='$tid'");

==========
Find Code:
==========

$query = $db->query("SELECT a.filename, a.filesize, a.downloads, p.* FROM ".X_PREFIX."posts p LEFT JOIN ".X_PREFIX."attachments a  ON (a.pid=p.pid) WHERE p.pid='$pid' AND p.tid='$tid' AND p.fid=".$forum['fid']);

================
Replace Code With:
================

$query = $db->query("SELECT a.filename, a.filesize, a.downloads, a.fileheight, a.filewidth, p.* FROM ".X_PREFIX."posts p LEFT JOIN ".X_PREFIX."attachments a ON (a.pid=p.pid) WHERE p.pid='$pid' AND p.tid='$tid' AND p.fid='$forum[fid]'");

==========
Find Code:
==========

                if (isset($_FILES['attach']) && ($file = get_attached_file($_FILES['attach'], $forums['attachstatus'], $SETTINGS['maxattachsize'])) !== false) {
                    $db->query("INSERT INTO ".X_PREFIX."attachments (tid, pid, filename, filetype, filesize, attachment, downloads) VALUES ($tid, $pid, '$filename', '$attach[type]', '$filesize', '$file', 0)");
                }

================
Replace Code With:
================

                if (isset($_FILES['attach']) && ($file = get_attached_file($_FILES['attach'], $forums['attachstatus'], $SETTINGS['maxattachsize'])) !== false) {
                    $db->query("INSERT INTO ".X_PREFIX."attachments (tid, pid, filename, filetype, filesize, attachment, downloads, fileheight, filewidth) VALUES ($tid, $pid, '$filename', '$attach[type]', '$filesize', '$file', 0, '$fileheight', '$filewidth')");
                }

==========
Find Code:
==========

                        case 'replace':
                            if (isset($_FILES['attachment_replace']) && ($file = get_attached_file($_FILES['attachment_replace'], $forums['attachstatus'], $SETTINGS['maxattachsize'])) !== false) {
                                $db->query("DELETE FROM ".X_PREFIX."attachments WHERE pid='$pid'");
                                $db->query("INSERT INTO ".X_PREFIX."attachments (tid, pid, filename, filetype, filesize, attachment, downloads) VALUES ($tid, $pid, '$filename', '$attachment_replace[type]', '$filesize', '$file', 0)");
                            }
                            break;

================
Replace Code With:
================

                        case 'replace':
                            if (isset($_FILES['attachment_replace']) && ($file = get_attached_file($_FILES['attachment_replace'], $forums['attachstatus'], $SETTINGS['maxattachsize'])) !== false) {
                                $db->query("DELETE FROM ".X_PREFIX."attachments WHERE pid='$pid'");
                                $db->query("INSERT INTO ".X_PREFIX."attachments (tid, pid, filename, filetype, filesize, attachment, downloads, fileheight, filewidth) VALUES ($tid, $pid, '$filename', '$attachment_replace[type]', '$filesize', '$file', 0, '$fileheight', '$filewidth')");
                            }
                            break;

=======================================================================================================================================
=======
Step 7:
=======
===================
Edit File: viewthread.php
===================
==========
Find Code:
==========

    $querypost = $db->query("SELECT a.aid, a.filename, a.filetype, a.filesize, a.downloads, p.*, m.*,w.time FROM ".X_PREFIX."posts p LEFT JOIN ".X_PREFIX."members m ON m.username=p.author LEFT JOIN ".X_PREFIX."attachments a ON a.pid=p.pid LEFT JOIN ".X_PREFIX."whosonline w ON w.username=p.author WHERE p.fid='$fid' AND p.tid='$tid' GROUP BY p.pid ORDER BY p.pid ASC LIMIT $start_limit, $ppp");

================
Replace Code With:
================

    $querypost = $db->query("SELECT a.aid, a.filename, a.filetype, a.filesize, a.downloads, a.fileheight, a.filewidth, p.*, m.*,w.time FROM ".X_PREFIX."posts p LEFT JOIN ".X_PREFIX."members m ON m.username=p.author LEFT JOIN ".X_PREFIX."attachments a ON a.pid=p.pid LEFT JOIN ".X_PREFIX."whosonline w ON w.username=p.author WHERE p.fid='$fid' AND p.tid='$tid' GROUP BY p.pid ORDER BY p.pid ASC LIMIT $start_limit, $ppp");

==========
Find Code:
==========

            $extention = strtolower(substr(strrchr($post['filename'],"."),1));
            if ($attachimgpost == 'on' && ($extention == 'jpg' || $extention == 'jpeg' || $extention == 'jpe' || $extention == 'gif' || $extention == 'png' || $extention == 'bmp')) {
                eval("\$post['message'] .= \"".template('viewthread_post_attachmentimage')."\";");
            } else {
                $downloadcount = $post['downloads'];
                if ($downloadcount == '') {
                    $downloadcount = 0;
                }
                eval("\$post['message'] .= \"".template('viewthread_post_attachment')."\";");
            }
        }

==================
Replace Code With:
==================

            $extention = strtolower(substr(strrchr($post['filename'],"."),1));
            if ($attachimgpost == 'on' && ($extention == 'jpg' || $extention == 'jpeg' || $extention == 'gif' || $extention == 'png' || $extention == 'bmp')) {
                // resize attachments
                if ($post['fileheight'] != '' && $post['filewidth'] != '') {
                    $SETTINGS['max_attheight'] = (int) $SETTINGS['max_attheight'];
                    $SETTINGS['max_attwidth'] = (int) $SETTINGS['max_attwidth'];
                    $h_ratio = $SETTINGS['max_attheight'] / $post['fileheight'];
                    $w_ratio = $SETTINGS['max_attwidth'] / $post['filewidth'];
                    if (($post['fileheight'] <= $SETTINGS['max_attheight']) && ($post['filewidth'] <= $SETTINGS['max_attwidth'])) {
                        $n_height = $post['fileheight'];
                        $n_width = $post['filewidth'];
                    } elseif (($w_ratio * $post['fileheight']) < $SETTINGS['max_attheight']) {
                        $n_height = ceil($w_ratio * $post['fileheight']);
                        $n_width = $SETTINGS['max_attwidth'];
                    } else {
                        $n_height = $SETTINGS['max_attheight'];
                        $n_width = ceil($h_ratio * $post['filewidth']);
                    }
                }
                eval("\$post['message'] .= \"".template('viewthread_post_attachmentimage')."\";");
            } else {
                $downloadcount = $post['downloads'];
                if ($downloadcount == '') {
                    $downloadcount = 0;
                }
                eval('$post['message'] .= "'.template('viewthread_post_attachment').'";');
            }
        }

=======================================================================================================================================
=======
Step 8:
=======
===============================
Go To Administration Panel --> Templates
===============================
====================================
Editi Template: viewthread_post_attachmentimage
====================================
==============
Replace All With:
==============

<br />
<br />
$post[author] $lang[textattachedimg]<br />
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-style: dashed; border-width: 1px; padding: 10px" bordercolor="$bordercolor" width="$n_width">
<tr>
<td>
<img src="viewthread.php?action=attachment&amp;tid=$tid&amp;pid=$post[pid]" title="$lang[thumbEnlarge]" alt="$lang[thumbEnlarge]" border="0px" width="$n_width" height="$n_height" onclick="window.open('viewthread.php?action=attachment&amp;tid=$tid&amp;pid=$post[pid]','ne','width=$post[filewidth],height=$post[fileheight],resizable=yes,scrollbars=yes')" />
</td>
</tr>
</table>
<br />

=======================================================================================================================================
Enjoy!