Jump to content

Archived

This topic is now archived and is closed to further replies.

desicube

Image Hosting Script Help ! Plz

Recommended Posts

Hi I am running an Image Hosting Script at [a href=\"http://www.imagefilez.com\" target=\"_blank\"]http://www.imagefilez.com[/a]

I am using Php Image Host Script v1.0

I have more than 750 members & 7000 images on my server. Everything is working fine.

The Only thing is that I want to customise my Script so that if give a code for the thumbnail & whenever user clicks on the thumbnail on the forums. He is able to view image on my custom page, which will have header and footer so that I can display google ads on them.

The Output of the Script should be like this... eg xs.to gives the code like this.

[a href=\"http://xs.to/xs.php?h=xs76&d=06151&f=karishma.jpg\" target=\"_blank\"]http://xs.to/xs.php?h=xs76&d=06151&f=karishma.jpg[/a]
& not like this which ImageFilez.com gives

[a href=\"http://imagefilez.com/images/amit/leech1.jpg\" target=\"_blank\"]http://imagefilez.com/images/amit/leech1.jpg[/a]

I am attaching some of the files and database.. plz suggest me what changes should I make or pm me.

The Sql database of Script can be downloaded at

[a href=\"http://fasterupload.com/1772532850\" target=\"_blank\"]http://fasterupload.com/1772532850[/a]

Here is my images.php Page.

[code]<?php
/*

*/

// include general site config / initialization stuff

require_once(dirname(__FILE__).'/includes/config.inc.php');

// include and initialize an instance of the images utility class

require_once(dirname(__FILE__).'/classes/images.class.php');
$images =& new images($ace);
$images->setuser($user);
$message = '';

// include the site header

require_once(dirname(__FILE__).'/header.php');

if( $banned ){
    $pagecontent->display('IP Banned');
    require_once(dirname(__FILE__).'/footer.php');
    exit();
}elseif( !$loggedin ){

    // the user must be logged in to access this page.
    // if not logged in, tell them so, display the login form and exit.

    $pagecontent->display('Images - Not Logged In');
    require(dirname(__FILE__).'/includes/notloggedin.inc.php');
}

// check if a gallery has been selected...

$gallery = 0;
$g = $ace->getint('g', $_REQUEST, -1);
if( !isset($user->galleries[$g]) ){
    if( $g > 0 ) $g = 0;
}else{
    $gallery = $user->galleries[$g];
}

// display the intro text for the managing your images page

$pagecontent->display('Images');

// initialize variables used to determine which images to list / order to list them in, which page of images to display, etc.

$orderby = $ace->getstr('o', $_REQUEST, 'date');
$orderdir = $ace->getstr('od', $_REQUEST, 'desc');

if( !in_array($orderby, array('name', 'uploaded') ) ) $orderby = 'uploaded';
if( !in_array($orderdir, array('asc', 'desc') ) ) $orderdir = 'desc';


$ids = array();
$msg = '';
$emails = array();
for( $i = 0; $i < $user->email_friends; $i++) $emails[$i] = '';

$modified = false;

if( $ace->getstr('delete', $_POST) != '' ){

    // user wants to delete some of their images

    $ids = isset($_POST['ids']) ? $_POST['ids'] : array();
    $deleted = $images->deleteimages($ids, $userid);
    if( $deleted ){
        $message = $deleted.' image(s) deleted.';
        $user->images -= $deleted;
        $modified = true;
        if( $user->images < 0 ) $user->images = 0;
    }
}elseif( $ace->getstr('send', $_POST) != '' ){

    // user wants to email links to their images to their friends..

    $ids = isset($_POST['ids']) ? $_POST['ids'] : array();
    $imgs = $images->getimages(array('ids'=>$ids, 'user_id'=>$userid));
    if( count($imgs) ){
        $emails = array();
        $sentto = array();
        for( $i = 0; $i < $user->email_friends; $i++){
//            $emails[] = $i;
            if( isset($_POST['to'][$i]) ){
                $email = trim(get_magic_quotes_gpc() == 1 ? stripslashes($_POST['to'][$i]) : $_POST['to'][$i]);
                $emails[$i] = $email;
                if( $email != '' ){
                    if( $ace->validateemail($email) ){
                        $sentto[] = $email;
                    }else{
                        $images->errors[] = 'The address "'.htmlspecialchars($email).'" is not a valid email address.';
                    }
                }
            }
        }
        if( count($sentto) > 0 ){
            $msg = $ace->getstr('message', $_POST);
            if( strlen($msg) > 255 ){
                $images->errors[] = 'Your message can only contain a maximum of 255 characters. It currently contains '.strlen($msg).'. Please reduce your message length.';
            }elseif( strlen($msg) == 0 ){
                $images->errors[] = 'You must enter a message to send.';
            }else{
                $headers = "From: {$user->email}\r\nReply-To: {$user->email}\r\nErrors-To: {$config->admin_email}\r\nReturn-Path: {$config->admin_email}";
                $tpl = $config->email_images_template;
                $imagelinks = array();
                foreach( $imgs as $i ){
                    $imagelinks[] = $i->image_url;
                }
                $s = array('{sitename}', '{siteurl}','{message}', '{imagelinks}');
                $r = array($config->sitename, $config->siteurl, $msg,join("\n\n", $imagelinks));
                $tpl = str_replace($s, $r, $tpl);
                foreach($sentto as $email ){
                    @mail($email, $config->email_images_subject, $tpl, $headers);
                }
                $msg = '';
                for( $i = 0; $i < $user->email_friends; $i++) $emails[$i] = '';
                $message = 'An email containing your message and links to your images has been sent to '.join(" and ", $sentto).'.';
            }
        }else{
            $images->errors[] = 'You must enter an email address to send the images to.';
        }
    }else{
        $images->errors[] = 'You need to check the checkboxes next to the images you want to send.';
    }
    if( count($images->errors) > 0 ) $images->errors[] = '<br />Please correct these errors and resubmit the <a href="#email">email form</a>.';
}elseif( $ace->getstr('addtogallery', $_POST) != '' ){
    $ids = isset($_POST['ids']) ? $_POST['ids'] : array();
    $added = $images->addtogallery($ids, $ace->getint('gallery_id', $_POST));
    if( count($images->errors) > 0 ){
        $errors= $images->errors;
    }else{
        $message = "$added image(s) have had their gallery changed.";
    }
}

if( $modified ) $user = $users->getuser(array('id'=>$user->user_id));

$criteria = array('user_id'=>$userid);
if( $gallery != 0 ){
    $criteria['galleryid'] = $gallery->gallery_id;
    $criteria['count'] = true;
    $user->images = $images->getimages($criteria);
    $criteria['count'] = false;
}
$page = $ace->getint('p', $_REQUEST, 1);
if( $page < 1 ) $page = 1;
$perpage = $user->images_per_page;
$totalpages = ceil($user->images / $perpage);
if( $page > $totalpages ) $page = $totalpages;
$first = ($page -1 ) * $perpage;
$last = min($first+$perpage,$user->images);
$imgs = $images->getimages($criteria, $orderby, $orderdir, $first, $perpage);

$purl = $_SERVER['SCRIPT_NAME'].'?o='.$orderby.'&od='.$orderdir.'&p={page}&g='.$g;

if( $message != '' ){
?>
<div class="message"><?php echo $message;?></div>
<?php
}

// any error messages? display them

if( count($images->errors) > 0 ){
?>
<div class="errors">Please correct the following errors:<br /><br />
<?php echo join("<br />\n", $images->errors);?></div>
<?php
}

if( count($imgs) > 0 ){

?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<div class="imagelist">
<?php

    // if the user has uploaded more images than their account allows (only possible if they previously
    // had a paid account which expired, or the admin has reduced the number of images allowed), then display a message

    if( $user->images > $user->max_images && $user->max_images > 0 ){
?>
<div class="errors">You are currently exceeding the uploaded image allowance for your account.
<br />Your <?php echo htmlspecialchars($user->type_name);?> account allows you to have
<?php echo $user->max_images;?> images at a time.
<br />Please delete at least <?php echo ($user->images-$user->max_images);?> of your images or
<a href="upgrade.php">upgrade your account</a>.
</div>
<?php
    }

    // now list all their images, x to a page, with options to order by name or date uploaded.
    // images are displayed with a thumbnail (click for full size), and urls to use to link to the image.

?>
<table class="imagetable" cellspacing="1" cellpadding="0" border="0" align="center">
<tr>
    <td class="imagetitle" colspan="3" style="text-align: center;">
    Images in order of
    <a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?o=name&od=asc&g=<?php echo $g;?>">Name (a-z)</a>
    | <a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?o=name&od=desc&g=<?php echo $g;?>">Name (z-a)</a>
    | <a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?o=uploaded&od=desc&g=<?php echo $g;?>">Newest first</a>
    | <a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?o=uploaded&od=asc&g=<?php echo $g;?>">Oldest first</a>
<?php
    if( count($user->galleries) > 0 ){
?>
<br />
Galleries:
<?php
        $ng = 0;
        foreach( $user->galleries as $id=>$gal ){
//            if(  $ng < count($user->galleries)) echo ', ';
            $ng++;
?><a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?g=<?php echo $id;?>"><?php echo $gal->gallery_name;?></a><?php
            echo ', ';
        }
?><a href="<?php echo $_SERVER['SCRIPT_NAME'];?>">All Images</a><?php
    }
?>
    </td>
</tr>
<tr>
    <td class="imagetitle">
    <script language="JavaScript" type="text/javascript">
<!--
document.writeln('<input type="checkbox" onClick="check(this.form, this.checked);" />');
//-->
</script>
<noscript> </noscript>
    </td>
    <td class="imagetitle" colspan="2" style="text-align: center;">
        Images <?php echo $first+1;?> to <?php echo $last;?> of <?php echo $user->images;?><?php if( $gallery ) echo ' in gallery "'.$gallery->gallery_name.'"';?>.
        Page <?php echo $page;?>
        of <?php echo $totalpages;?>
        <br />
<?php
    if( $page > 1 ){
?>
        <div style="float: left;">
            <a style="text-decoration: none;"  href="<?php echo str_replace('{page}', ($page-1), $purl);?>">< Previous</a>
        </div>
<?php
    }
    if( $page < $totalpages ){
?>
        <div style="float: right;">
            <a style="text-decoration: none;" href="<?php echo str_replace('{page}',($page+1), $purl);?>">Next ></a>
        </div>
<?php
    }
?>
    </td>
</tr>
<?php
$tr = 1;
foreach( $imgs as $i ){
?>
<tr class="tr<?php echo ($tr+1);?>">
    <td class="tdcheck"><input type="checkbox" name="ids[]" value="<?php echo $i->image_id;?>" <?php if( in_array($i->image_id, $ids) ) echo ' CHECKED ';?>/></td>
    <td class="tdthumb">
    <a onClick="return si('<?php echo $i->image_url;?>', <?php echo $i->width;?>, <?php echo $i->height;?>);"  target="_blank" href="<?php echo $i->image_url;?>"><img border="0" class="thumb" src="<?php echo $i->thumb_url;?>" alt="" /></a>
    <br />
    (<?php echo $i->width.'x'.$i->height;?>)
    <?php echo number_format($i->filesize/1024, 1).'k';?>
    <br />
    Bandwidth:
<?php
    if( $i->bandwidthr > (1024*1024) ){
        echo $i->bandwidth.'mb';
    }elseif( $i->bandwidthk > (1024*1024) ){
        echo number_format($i->bandwidthr/(1024*1024*1024),2).'gb';
    }else{
        echo $i->bandwidthk.'k';
    }
?>
<?php if( $user->resize_images || $user->rename_images ){ ?>
        <div class="infolabel" style="text-align: center;">
<?php if( $user->rename_images ){ ?>
            [<a href="rename.php?i=<?php echo $i->image_id;?>">Rename</a>]
<?php } ?>
<?php if( $user->resize_images ){ ?>
            [<a href="resize.php?i=<?php echo $i->image_id;?>">Resize</a>]
<?php } ?>
        </div>
<?php } ?>
    </td>
    <td class="tdinfo">
    <b><a onClick="return si('<?php echo $i->image_url;?>', <?php echo $i->width;?>, <?php echo $i->height;?>);" target="_blank" href="<?php echo $i->image_url;?>"><?php echo $i->name.'.'.$i->type;?></a></b>
    <br />
        <div class="infolabel">URL</div>
        <div class="infotext"><input onFocus="this.select();" class="urlinput" type="text" value="<?php echo $i->image_url;?>" /></div>
        <div class="infolabel">HTML <img></div>
        <div class="infotext"><input onFocus="this.select();" class="urlinput" type="text" value="<?php echo htmlentities('<img src="'.$i->image_url.'" alt="image hosting by '.$ace->config->siteurl.'" />');?>" /></div>
        <div class="infolabel">BB Code</div>
        <div class="infotext"><input onFocus="this.select();" class="urlinput" type="text" value="[img]<?php echo $i->image_url;?>[/img]" /></div>
        <div class="infolabel">Uploaded on the <?php echo date('jS F Y', strtotime($i->uploaded));?></div>
<?php if( $i->gallery_id != 0 ){?>
    <b>In Gallery: <a href="<?php echo $_SERVER['SCRIPT_NAME'].'?g='.$i->gallery_id;?>"><?php echo $i->gallery_name;?></a></b>
    [<a target="_blank" href="<?php echo $ace->config->siteurl.'gallery.php?g='.$i->gallery_name.'&u='.$user->username;?>">View</a>]
<?php } ?>
    </td>
</tr>
<?php
    $tr = 1 - $tr;
}
?>
<tr>
    <td class="imagetitle">
    <script language="JavaScript" type="text/javascript">
<!--
document.writeln('<input type="checkbox" onClick="check(this.form, this.checked);" />');
//-->
</script>
<noscript> </noscript>
    </td>
    <td class="imagetitle" colspan="2" style="text-align: center;">
<?php
    if( $page > 1 ){
?>
        <div style="float: left;">
            <a style="text-decoration: none;"  href="<?php echo str_replace('{page}', ($page-1), $purl);?>">< Previous</a>
        </div>
<?php
    }
    if( $page < $totalpages ){
?>
        <div style="float: right;">
            <a style="text-decoration: none;" href="<?php echo str_replace('{page}',($page+1), $purl);?>">Next ></a>
        </div>
<?php
    }
?>

    <br />
        Images <?php echo $first+1;?> to <?php echo $last;?> of <?php echo $user->images;?><?php if( $gallery ) echo ' in gallery "'.$gallery->gallery_name.'"';?>.
        Page <?php echo $page;?>
        of <?php echo $totalpages;?>
    </td>
</tr>
<?php if( $user->email_friends > 0 ){ ?>
<tr>
    <td colspan="3" class="imagetitle"><a name="email"></a> </td>
</tr>
<tr>
    <td class="imagetitle"> </td>
    <td class="imagetitle" colspan="2">
        <b>Email The Selected Images To Your Friends!</b>
    </td>
</tr>
<tr class="tr1">
    <td class="tdcheck"> </td>
    <td class="tdinfo" style="text-align: right;">Friend's Email:</td>
    <td class="tdinfo">
<?php
    for( $i = 0; $i < $user->email_friends; $i++){
?>
        <input type="text" name="to[]" value="<?php echo htmlspecialchars($emails[$i]);?>" class="urlinput" /><br />
<?php
    }
?>
    <br />
<?php if( $user->email_friends > 1 ){ ?>
Enter the email address of each friend you want to send these images to in one of the boxes above.
You can send the images to up to <?php echo $user->email_friends;?> email addresses at once.
<?php }else{ ?>
Enter the email address of the friend you want to send these images to.
<?php } ?>
<br />We respect your privacy. We do not store the email addresses submitted via this form.
    </td>
</tr>
<tr class="tr1">
    <td class="tdcheck"> </td>
    <td class="tdthumb" style="text-align: right;">Short Message<br />(max 255 characters)</td>
    <td class="tdinfo"><textarea name="message" class="urlinput" style="height: 60px;"><?php echo htmlspecialchars($msg);?></textarea></td>
</tr>
<tr class="tr1">
    <td class="tdcheck"> </td>
    <td class="tdthumb"> </td>
    <td class="tdinfo"><input type="submit" name="send" value="Email The Selected Images" /></td>
</tr>
<?php
}

if( count($user->galleries) > 0 ){
?>
<tr>
    <td class="imagetitle" colspan="3"> </td>
</tr>
<tr>
    <td class="imagetitle" colspan="3">Add The Selected Images To One Of Your Galleries</td>
</tr>
<tr class="tr1">
    <td class="tdcheck"> </td>
    <td class="tdthumb"> </td>
    <td class="tdinfo">
        <select name="gallery_id">
            <option value="0">No Gallery</option>
<?php
    foreach( $user->galleries as $id=>$g ){
        echo "<option value=\"$id\">{$g->gallery_name}</option>\n";
    }
?>
        </select>
        [<a href="galleries.php">Manage Galleries</a>]<br />
        <input type="submit" name="addtogallery" value="Add Selected Images To This Gallery" onClick="return confirm('Are you sure you want to add the selected images to the selected gallery?');" />
    </td>
</tr>
<?php
}
?>
<tr>
    <td class="imagetitle" colspan="3"> </td>
</tr>
<tr>
    <td class="imagetitle" colspan="3">Remove The Selected Images From Your Account</td>
</tr>
<tr class="tr1">
    <td class="tdcheck"> </td>
    <td class="tdthumb"> </td>
    <td class="tdinfo">
        <input type="submit" name="delete" value="Delete The Selected Images" onClick="return confirm('Are you sure you want to delete the selected images?');" />
    </td>
</tr>
</table>
</div>
</form>
<?php

}else{

    // if the user hasn't uploaded any images ,display a message telling them this.

    $pagecontent->display('No Images');

}

// include the site footer.

require_once(dirname(__FILE__).'/footer.php');

?>

[/code]

and this is my classes/images.class.php page.


[code]<?php
/*

*/
class images{

var $errors = array();
var $orderbys = array('name'=>'Name', 'uploaded'=>'Uploaded', 'size'=>'File Size', 'format'=>'Format', 'bandwidth'=>'Bandwidth');
var $imagetypes = array(IMAGETYPE_JPEG=>'jpg', IMAGETYPE_PNG=>'png', IMAGETYPE_GIF=>'gif');
var $user = 0;

function images(&$ace){
$this->ace =& $ace;
}

function setuser(&$user){
$this->user =& $user;
}

function resizeimage($src, $mwidth, $mheight, $destroy = false){
$w = imagesx($src);
$h = imagesy($src);
if( $w > $mwidth || $h > $mheight ){
$dx = (double)$w / (double)$mwidth;
$dy = (double)$h / (double)$mheight;
$d = $dx > $dy ? $dx : $dy;
$nw = (int)((double)$w / $d);
$nh = (int)((double)$h / $d);
$dest = imagecreatetruecolor($nw, $nh);
imagecopyresampled($dest, $src, 0, 0, 0, 0, $nw, $nh, $w, $h);
if( $destroy ) imagedestroy($src);
return $dest;
}
return $src;
}

function deleteimages($ids, $userid = 0){
$ids = $this->ace->getids($ids);
if( count($ids) > 0 ){
$criteria = array('ids'=>$ids);
if( $userid != 0 ) $criteria['userid'] = $userid;
$imgs = $this->getimages($criteria);
$iids = array();
if( count($imgs) > 0 ){
$userbandwidths = array();
foreach( $imgs as $i){
if( !isset($userbandwidths[$i->user_id]) ) $userbandwidths[$i->user_id] = 0;
$userbandwidths[$i->user_id] += $i->bandwidthr;
$iids[] = $i->image_id;
$fname = $this->ace->config->image_folder.$i->username.'/'.$i->name.'.'.$i->type;
$tname = $this->ace->config->thumb_folder.$i->username.'/'.$i->name.'.jpg';
if( @file_exists($fname) ){
unlink($fname);
}
if( @file_exists($tname) ){
unlink($tname);
}
}
$sql = "DELETE FROM images WHERE image_id IN (".join(",",$iids).") ";
$this->ace->query($sql, 'Delete Images');
$deleted = mysql_affected_rows();
$cnt = 0;
foreach( $userbandwidths as $id=>$bw ){
if( $bw > 0 ){
$sql = "UPDATE {pa_dbprefix}users SET deleted_images_bandwidth=deleted_images_bandwidth+ $bw WHERE user_id=$id ";
$this->ace->query($sql, 'Update Bandwidth Used');
if( $cnt % 5 ) sleep(1);
}
}
return $deleted;
}
}
return 0;
}

function getimages($criteria = array(), $orderby = 'name', $orderdir = 'asc', $first = 0, $limit = 0){
$justcount = isset($criteria['count']) && $criteria['count'] == true ? true : false;
if( $justcount ){
$sql = "SELECT COUNT(*) ";
}else{
$ipath = addslashes($this->ace->config->image_url);
$tpath = addslashes($this->ace->config->thumb_url);
$sql = "SELECT i.*, u.username, u.email, g.gallery_name, ";
$sql .="CONCAT('$ipath',u.username,'/', i.name, '.', type) AS image_url, ";
$sql .="CONCAT('$tpath',u.username,'/', i.name, '.jpg') AS thumb_url ";
}
$sql .="FROM {pa_dbprefix}images i LEFT OUTER JOIN {pa_dbprefix}galleries g ON i.gallery_id=g.gallery_id, ";
$sql .="{pa_dbprefix}users u ";
$wheres = array("i.user_id=u.user_id ");
foreach( $criteria as $c=>$v){
switch( $c ){
case 'ids': $ids = $this->ace->getids($v);$ids[] = 0; $wheres[] = " i.image_id IN (".join(",",$ids).") "; break;
case 'name': $wheres[] = " i.name LIKE '".str_replace("*", "%", addslashes($v))."' "; break;
case 'uploaded': $wheres[] = " TO_DAYS(i.uploaded)+$v>=TO_DAYS(NOW()) "; break;
case 'height': settype($v, 'integer'); $wheres[] = " i.height=$v "; break;
case 'width': settype($v, 'integer'); $wheres[] = " i.width=$v "; break;
case 'username': $wheres[] = " u.username LIKE '".str_replace("*", "%",addslashes($v))."' "; break;
case 'userid': case 'user_id': settype($v, 'integer'); $wheres[] = " i.user_id=$v "; break;
case 'format': $wheres[] = "i.type='".addslashes($v)."' "; break;
case 'minsize': settype($v, 'integer'); $wheres[] = "i.filesize>=".($v*1024)." "; break;
case 'maxsize': settype($v, 'integer'); $wheres[] = "i.filesize<=".($v*1024)." "; break;
case 'uploadedbefore': $wheres[] = "date_format(i.uploaded, '%Y-%m-%d')<='".addslashes($v)."' "; break;
case 'uploadedafter': $wheres[] = "date_format(i.uploaded, '%Y-%m-%d')>='".addslashes($v)."' "; break;
case 'ip': $wheres[] = "i.ip LIKE '".addslashes(str_replace('*', '%', $v))."' "; break;
case 'checked': settype($v, 'integer'); $wheres[] = "i.checked=$v "; break;
case 'public': settype($v, 'integer'); $wheres[] = "i.public=$v "; break;
case 'galleryid': settype($v, 'integer'); $wheres[] = "i.gallery_id=$v "; break;
case 'galleryname': $wheres[] = "g.gallery_name LIKE '".addslashes(str_replace('*', '%', $v))."' "; break;
case 'bandwidth': settype($v, 'integer'); $wheres[] = "i.bandwidth/(1024*1024)>=$v "; break;
}
}
if( count($wheres) > 0 ) $sql .= "WHERE ".join(" AND ", $wheres)." ";
if( !$justcount ){
$ob = 'i.name';
switch( $orderby ){
case 'bandwidth': $ob = 'i.bandwidth'; break;
case 'name': $ob = 'i.name'; break;
case 'uploaded': $ob = 'i.uploaded'; break;
case 'format': $ob = 'i.type'; break;
case 'size': $ob = 'i.filesize'; break;
case 'username': $ob = 'u.username'; break;
case 'random': $ob = 'RAND() '; $orderdir = ''; break;
}
if( $orderdir != 'desc' ) $orderdir = '';
$sql .= "ORDER BY $ob $orderdir ";
if($limit > 0 ) $sql .= "LIMIT $first,$limit ";
}
$res = $this->ace->query($sql, 'Get Images');
if( $justcount ){
return mysql_result($res,0,0);
}else{
$imgs = array();
while( $i = mysql_fetch_object($res) ){
$i->bandwidthk = number_format($i->bandwidth/1024,2);
$i->bandwidthr = $i->bandwidth;
$i->bandwidth = number_format($i->bandwidth/(1024*1024),2);
// $i->storage = number_format($i->storage/(1024*1024),2);
$imgs[] = $i;
}
return $imgs;
}
return 0;
}

function brand_image($img){
$font = '';//dirname(__FILE__).'/ARIAL.TTF';
if( $img == 0 || $this->user == 0 || $this->user->add_branding == 0 ) return false;
$fg = $this->hextorgb($this->ace->config->branding_color);
$bg = $this->hextorgb($this->ace->config->branding_bgcolor);
$bgcol = imagecolorallocatealpha($img,$bg['red'], $bg['green'], $bg['blue'],($this->ace->config->branding_transparency)*1.27);
$col = imagecolorallocate($img,$fg['red'], $fg['green'], $fg['blue']);
if( function_exists('imagettfbbox') && $font != '' && @file_exists($font) ){
$bbox = imagettfbbox ( $this->ace->config->branding_size, 0, $font, $this->ace->config->branding_text);
$width = $bbox[2] - $bbox[0];
$height = $bbox[1] - $bbox[7];
$xoff = $bbox[0];
$yoff = $bbox[1];
$x = imagesx($img)-$xoff-$width-5;
$y = imagesy($img)-$yoff;
imagefilledrectangle($img,0,imagesy($img)-$height, imagesx($img), imagesy($img), $bgcol);
imagettftext($img, $this->ace->config->branding_size, 0, $x, $y, $col, $font, $this->ace->config->branding_text);
}else{
$width = imagefontwidth(2)*strlen($this->ace->config->branding_text);
$height = imagefontheight(2)+4;
imagefilledrectangle($img,0,imagesy($img)-$height, imagesx($img), imagesy($img), $bgcol);
imagestring($img, 2, imagesx($img)-$width-4, imagesy($img)-$height,$this->ace->config->branding_text, $col);
}
return true;
}

function addimage($fname, $userid, $file, $gallery = 0, $checked = 0){
if( $this->user == 0 ){
$this->errors[] = 'You must specify a valid user account.';
return 0;
}elseif( $this->user->images >= $this->user->max_images && $this->user->max_images > 0 ){
$this->errors[] = 'You are already using all of your image storage allowance ('.$this->user->max_images.' images uploaded.)';
return 0;
}
settype($gallery, 'integer');
if( !isset($this->user->galleries[$gallery]) ) $gallery = 0;
settype($checked, 'integer');
$ip = isset($_SERVER['X_FORWARDED_FOR']) ? $_SERVER['X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$ip = addslashes($ip);
$userid = $this->user->user_id;
if( @is_uploaded_file($file) ){
if( $fname == '' ) $fname = $file;
$fname = strtolower($fname);
$fname = preg_replace('/^.*[\/\\\]/i','',$fname);
$fname = preg_replace('/\..*$/i', '', $fname);
$fname = preg_replace('/[^a-z0-9]/i', '', $fname);
if( $fname != '' ){
if( @filesize($file) <= $this->user->max_upload_size * 1024 ){
if( $this->user->storage == 0 || $this->user->storage_used < $this->user->storage ){

$image = $this->getimage(array('path'=>$this->user->username.'/'.$fname));

if( $image != 0 ){
$sql = "SELECT COUNT(*) FROM images WHERE user_id={$this->user->user_id} AND name LIKE '".$fname."_%' ";
$res = $this->ace->query($sql, 'Count Same Name Images');
$num = mysql_result($res,0,0);
$fname .= "_".($num+1);
}
$fname = strtolower($fname);
$name = $fname;
$info = @getimagesize($file);
if( $info ){
if( isset($this->imagetypes[$info[2]]) ){
$width = $info[0];
$height = $info[1];
$type = $this->imagetypes[$info[2]];
$size = @filesize($file);
$imgfuncs = array(IMAGETYPE_JPEG=>'imagecreatefromjpeg', IMAGETYPE_PNG=>'imagecreatefrompng',
IMAGETYPE_GIF=>'imagecreatefromgif');
$img = 0;
$modified = false;
// check dimensions... resize if neccessary
$func = $imgfuncs[$info[2]];
$img = @$func($file);
if( $img != 0 ){
if( $width > $this->user->max_image_width || $height > $this->user->max_image_height ){
if( $this->user->auto_resize == 1 ){
$img = $this->resizeimage($img, $this->user->max_image_width, $this->user->max_image_height, true);
$modified = true;
$width = imagesx($img);
$height = imagesy($img);
}else{
$this->errors[] = 'Image "'.htmlspecialchars($fname).'" is too large ('.$width.'x'.$height.'). Max size allowed is '.$this->user->max_image_width.'x'.$this->user->max_image_height.'.';
imagedestroy($img);
return false;
}
}
// create thumbnail
$thumb = $this->resizeimage($img, $this->ace->config->thumbnail_width, $this->ace->config->thumbnail_height, false);
}else{
$this->errors[] = 'Error reading image "'.htmlspecialchars($fname).'". ';
return 0;
}
if( $this->user->add_branding == 1 && $this->ace->config->min_branding_width < imagesx($img)
&& $this->ace->config->min_branding_height < imagesy($img)){
if( !imageistruecolor($img) ){
$tmp = imagecreatetruecolor(imagesx($img), imagesy($img));
imagecopy($tmp, $img, 0, 0, 0,0,imagesx($img), imagesy($img));
imagedestroy($img);
$img = $tmp;
}

$this->brand_image($img);
$modified = true;
}

if( $this->user->auto_jpeg ){
$type = 'jpg';
}elseif($type == 'gif' && $modified ){
$type = 'png';
}
$sql = "INSERT INTO images (name, user_id, type, width, height, ";
$sql .="uploaded, filesize, ip, checked, gallery_id) ";
$sql .="VALUES ('$fname', $userid,'$type', ";
$sql .="$width, $height, now(),$size, '$ip', $checked, $gallery) ";
$res = $this->ace->query($sql, 'Add Image');
$id = mysql_insert_id();
if( $id ){
if( $this->user->auto_jpeg == 1 ){
@imagejpeg($img, $this->ace->config->image_folder.$this->user->username.'/'.$fname.'.jpg', $this->user->jpeg_quality);
}else{
if( !$modified ){
@move_uploaded_file($file, $this->ace->config->image_folder.$this->user->username.'/'.$fname.'.'.$type);
}else{
if( $type == 'jpg' ){
@imagejpeg($img, $this->ace->config->image_folder.$this->user->username.'/'.$fname.'.jpg', $this->user->jpeg_quality);
}else{
@imagepng($img, $this->ace->config->image_folder.$this->user->username.'/'.$fname.'.png');
}
}
}
$iname = $this->ace->config->image_folder.$this->user->username.'/'.$fname.'.'.$type;
chmod($iname, 0666);
$fsize = @filesize($iname);
$sql = "UPDATE images SET filesize=$fsize WHERE image_id=$id ";
$this->ace->query($sql, 'Set Image File Size');
$tname = $this->ace->config->thumb_folder.$this->user->username.'/'.$fname.'.jpg';
@imagejpeg($thumb, $tname, $this->user->jpeg_quality);
chmod($tname, 0777);
return $id;
}else{
$this->errors[] = 'A database error occurred whilst attempting to add the image "'.htmlspecialchars($name).'". Please try again later.';
}
}else{
$this->errors[] = 'The file "'.htmlspecialchars($fname).'" is not a supported image format.';
}
}else{
$this->errors[] = 'The file "'.htmlspecialchars($fname).'" is not a supported image format.';
}
}else{
$this->errors[] = 'You do not have enough free storage space to upload this image. Please delete some existing images or upgrade your account.';
}

}else{
$this->errors[] = 'The size of the file you uploaded exceeds the maximum file size allowed ('.($this->user->max_upload_size*1024).' bytes). Please try resizing your image or saving as a compressed jpeg before uploading again.';
}
}else{
$this->errors[] = 'You must enter a name for your image. The name must only contain the characters a-z and 0-9.';
}
}else{

}
return 0;
}

function hextorgb($hex){
$hex = preg_replace('/[^0-9a-f]/i', '', $hex);
$hex = str_pad($hex,6,'0');
$ret = array( 'red' => hexdec(substr($hex, 0, 2)),
'green' => hexdec(substr($hex, 2, 2)),
'blue' => hexdec(substr($hex, 4, 2))
);
return $ret;
}

function getimage($criteria = array()){
$ipath = addslashes($this->ace->config->image_url);
$tpath = addslashes($this->ace->config->thumb_url);
$sql = "SELECT i.*, u.username, ";
$sql .="CONCAT('$ipath', u.username, '/',i.name, '.', type) AS image_url, ";
$sql .="CONCAT('$tpath', u.username, '/',i.name, '.jpg') AS thumb_url ";
$sql .="FROM images i, users u ";
$wheres = array('i.user_id=u.user_id');
foreach( $criteria as $c=>$v ){
switch( $c ){
case 'type': $wheres[] = "i.type='".addslashes($v)."' "; break;
case 'id': settype($v, 'integer'); $wheres[]=" i.image_id=$v "; break;
case 'name': $wheres[] = " i.name='".addslashes($v)."' "; break;
case 'userid': settype($v, 'integer'); $wheres[] = " i.user_id=$v "; break;
case 'path': $wheres[] = "CONCAT(u.username, '/', i.name)='".addslashes($v)."' "; break;
}
}
if( count($wheres) > 0 ) $sql .= "WHERE ".join(" AND ", $wheres)." ";
$res = $this->ace->query($sql, 'Get Image');
$img = mysql_fetch_object($res);
if( $img ){
$img->bandwidth = number_format($img->bandwidth/(1024*1024),2);
}
return $img;
}

function updateimages($ids, $vars){
$ids = $this->ace->getids($ids);
if( !is_array($vars) ) $vars = array($vars);
$ups = array();
foreach( $vars as $n=>$v ){
switch( $n ){
case 'status': $ups[] = "status=".(int)$v." "; break;
case 'checked': settype($v, 'integer'); $ups[] = "checked=$v "; break;
}
}
if( count($ids) && count($ups) ){
$sql = "UPDATE {pa_dbprefix}images SET ".join(",",$ups)." WHERE image_id IN (".join(",",$ids).") ";
$this->ace->query($sql, 'Update Images');
return mysql_affected_rows();
}else{
return 0;
}
}

function setchecked($ids, $checked = 1 ){
$ids = $this->ace->getids($ids);
settype($checked, 'integer');
if( count($ids) ){
$sql = "UPDATE {pa_dbprefix}images SET checked=$checked WHERE image_id IN (".join(",",$ids).") ";
$this->ace->query($sql, 'Set Image Status');
return mysql_affected_rows();
}else{
return 0;
}

}

function setpublic($ids, $public = 0){
$ids = $this->ace->getids($ids);
settype($public, 'integer');
if( count($ids) ){
$sql = "UPDATE {pa_dbprefix}images SET public=$public WHERE image_id IN (".join(",",$ids).") ";
if( $this->user != 0 ) $sql .= "AND user_id={$this->user->user_id} ";
$this->ace->query($sql, 'Set Image Public');
return mysql_affected_rows();
}else{
return 0;
}
}

function suspendusers($ids){
$ids = $this->ace->getids($ids);
if( count($ids) > 0 ){
$sql = "UPDATE {pa_dbprefix}users SET status=2 WHERE user_id IN (".join(",",$ids).") ";
$this->ace->query($sql, 'Suspend Users');
return mysql_affected_rows();
}
return 0;
}

function resizeexistingimage(&$image, $newwidth, $newheight, $copy = false){
if( $newwidth == $image->width && $newheight == $image->height || ($newwidth == 0 && $newheight == 0)){
$this->errors[] = 'You must enter a new width and / or height for this image.';
return false;
}
if( $newwidth > $this->user->max_image_width || $newheight > $this->user->max_image_height ){
$this->errors[] = 'The maximum width and height you are allowed is '.$this->user->max_image_width.'x'.$this->user->max_image_height.'.';
return false;
}
$imgfuncs = array(IMAGETYPE_JPEG=>'imagecreatefromjpeg', IMAGETYPE_PNG=>'imagecreatefrompng',
IMAGETYPE_GIF=>'imagecreatefromgif');
$iname= $this->ace->config->image_folder.$this->user->username.'/'.$image->name.'.'.$image->type;
$info = @getimagesize($iname);
if( $info != 0 ){
$width = $info[0];
$height = $info[1];

// get new width and height...
// and check new width and height are ok...

if( $newwidth == 0 ){
$d = (double)((double)$height / (double)$newheight);
$newwidth = (int)((double)$width / $d);
if( $newwidth > $this->user->max_image_width ){
$this->errors[] = 'The new height you entered results in a new width larger than your maximum allowed image width.';
return false;
}elseif( $newwidth < 1 ){
$newwidth = 1;
}
}elseif( $newheight == 0 ){
$d = (double)((double)$width / (double)$newwidth);
$newheight = (int)((double)$height / $d);
if( $newheight > $this->user->max_image_height ){
$this->errors[] = 'The new width you entered results in a new height larger than your maximum allowed image height.';
return false;
}elseif( $newheight < 1 ){
$newheight = 1;
}
}

$lfunc = $imgfuncs[$info[2]];
$img = @$lfunc($iname);
$dest = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dest, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagedestroy($img);
$savetype = $image->type;
if( $image->type == 'gif' ){
$savetype = 'jpg';
}
$name = $image->name;
if( $copy == true ){
$name.= '_'.$newwidth.'x'.$newheight;
}
// check that the name is ok (and change it if it isn't )

$ex = $this->getimage(array('userid'=>$this->user->user_id, 'name'=>$name, 'type'=>$savetype));

if( $ex != 0 && $ex->image_id != $image->image_id ){
$sql = "SELECT COUNT(*) FROM images WHERE user_id={$this->user->user_id} AND name LIKE '".$name."_%' AND type='$savetype' ";
$res = $this->ace->query($sql, 'Count Same Name Images');
$num = mysql_result($res,0,0);
$name .= "_".($num+1);
}

$tpath = $this->ace->config->thumb_folder.$this->user->username.'/';
$ipath = $this->ace->config->image_folder.$this->user->username.'/';

// if j

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.