Location: PHPKode > projects > Slashster > slashster/html/editphotos.php
<?

# Slashster: Open Source Friend of a Friend Software
# Copyright 2004 Mark El-Wakil
# Contact: hide@address.com
# http://seventhcycle.net
#
# This file is part of Slashster.
#
# Slashster is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Slashster is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Slashster; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   require("PageClass.php");
   $mpage = new PageClass("authonly");
   $mpage->MembersOnly();
   $mpage->VerifiedOnly();


   $sql = "select Karma, UNIX_TIMESTAMP(DateJoined) as UT from UserTable where Id = $mpage->IdNum";
   $yu = $mpage->DoQuery($sql,1);
   $yu = $yu[0];

   $yourkarma = $mpage->CalcKarma($yu[Karma], $yu[UT]);

   $sql = "select Karma, UNIX_TIMESTAMP(DateJoined) as UT from UserTable where Referer = $mpage->IdNum";  
   $tu = $mpage->DoQuery($sql,1);

   $friendkarma = 0;
   foreach ($tu as $t)
   {
      $friendkarma += $mpage->CalcKarma($t[Karma], $t[UT]);
   }

   $yourkarma   = floor($yourkarma * 100000);
   $friendkarma = floor($friendkarma * 20000);

   $totspace = $mpage->user[PicQuota] + $yourkarma + $friendkarma;
   $totspace = floor(min(2000000, $totspace));

   

   $sql = "SELECT * FROM UserPics where Userid = $mpage->IdNum order by Id";
   $qu  = $mpage->DoQuery($sql,1); // qryUsers

   $szeused = 0;
   foreach ($qu as $q) $szeused += filesize("userpics/".$mpage->PicPr($q[Id], $q[Userid]) . "-a.jpg");

   $szeallow = ($totspace - $szeused);
   $szeallow = ($szeallow > 0) ? $szeallow : 0;
   
   if ($_FILES["pic"]["tmp_name"])
   {
        $pic_type  =$_FILES[pic][type];

        $allowed = array("image/pjpeg", "image/jpg", "image/jpeg");
        $SearchA = $mpage->FileUpload($_FILES['pic'][tmp_name], $_FILES['pic'][type], $allowed);

        if (strlen($SearchA) > $szeallow)
        {
             $errmsg .= "You cannot upload a file that large.";
             $errmsg .= "You have $szeallow bytes free.";

             $SearchA = "";
             $Add = "";
        }
        else
        {
             $SearchB = CreateThumb($SearchA, $crop, 80, 60);
             $SearchC = CreateThumb($SearchA, $crop, 48, 36);
        }
   }
   else
   {
        $Add = "";
   }

   if ($_GET["SetDefault"])
   {
       $sql = "select * from UserPics where Userid = $mpage->IdNum && Id = \"$_GET[SetDefault]\" ";
       $r = $mpage->DoQuery($sql,1);

       if (sizeof($r))
       {
           $sql = "update UserTable set PicDefault = '$_GET[SetDefault]' where Id = $mpage->IdNum";
           $mpage->DoQuery($sql);

           $mpage->user[PicDefault] = $_GET[SetDefault];
       }

   }
   elseif ($_GET[Delete])
   {
        $Delete = $_GET[Delete];

        $sql = "delete from UserPics where Userid = $mpage->IdNum && Id = '$Delete'";
        $mpage->DoQuery($sql);

        if (mysql_affected_rows())
        {
          $uid = $mpage->IdNum;

          @unlink("userpics/$Delete-$uid-a.jpg");
          @unlink("userpics/$Delete-$uid-b.jpg");
          @unlink("userpics/$Delete-$uid-c.jpg");

          if ($id == $mpage->nfo[PictureDefault])
          {
              $sql = "select Id from UserPics where Userid = $mpage->IdNum limit 1";
              $gt  = $mpage->DoQuery($sql);
              $gt = (sizeof($gt)) ? $gt[0][0] : 0;

              $sql = "update UserTable set PicDefault = $gt where Id = $mpage->IdNum";
              $mpage->DoQuery($sql);

              $mpage->user[PicDefault] = $gt;
          }  
        }

   }
   elseif ($_POST[Add] && $SearchA)
   {
        $descr = trim($descr);

        $sql = "insert into UserPics set Userid = $mpage->IdNum, Descr=\"$descr\"";
        $mpage->DoQuery($sql);
        $mid = mysql_insert_id();

        if (!strlen($descr))
        {
            $sql = "update UserPics set Descr = concat('Untitled #', Id) where Id = $mid";
            $mpage->DoQuery($sql);
        }

        $uid = $mpage->IdNum;

        $fp = @fopen("userpics/$mid-$uid-a.jpg", "w");
        fwrite($fp, $SearchA);
        fclose($fp);

        $fp = @fopen("userpics/$mid-$uid-b.jpg", "w");
        fwrite($fp, $SearchB);
        fclose($fp);

        $fp = @fopen("userpics/$mid-$uid-c.jpg", "w");
        fwrite($fp, $SearchC);
        fclose($fp);

        if (!$mpage->user[PicDefault])
        {
            $sql = "update UserTable set PicDefault = $mid where Id = $mpage->IdNum";
            $mpage->DoQuery($sql); 

            $mpage->user[PicDefault] = $mid;            
        }

   }

   $sql = "SELECT * FROM UserPics where Userid = $mpage->IdNum order by Id";
   $qu  = $mpage->DoQuery($sql,1); // qryUsers

   $szeused = 0;
   foreach ($qu as $q) $szeused += filesize("userpics/".$mpage->PicPr($q[Id], $q[Userid]) . "-a.jpg"); 
              
   $szeallow = ($totspace - $szeused);
   $szeallow = ($szeallow > 0) ? $szeallow : 0;


   $mpage->Initialize("authremainder");

?>

<center>

<div style="width:480px" align="left">
<BR>
<span class="title1">Edit Your Pictures</span>
<BR>
<small>[<a href="photos.php?id=<?= $mpage->IdNum ?>">View Photos</a>]</small>
<small>[<a href="user.php">Go Back to Your User Page</a>]</small>
<BR><BR> 
</div>


<table border=1 width=480 bordercolor=black cellspacing=0>
    
<? for ($x = 0; $x < sizeof($qu); $x++)    
   {
        $q = $qu[$x];
        if ($x % 4 == 0) echo "<tr>";
      ?>

<td width=120 align=center>

<? if ($mpage->user[PicDefault] == $q[Id]) { ?>
<B>Primary Photo</b>

<? } else { ?>
[ <a href="editphotos.php?SetDefault=<?= $q[Id] ?>">Set as Primary</a> ] 

<? } ?>

<BR>
<B>"<?= $q[Descr] ?>"</b>

<BR><BR><small>[ <a href="editphotos.php?Delete=<?= $q[Id] ?>">Delete this photo</a> ]</small>
<BR><a href="/userpics/<?= $mpage->PicPr($q[Id], $q[Userid]) ?>-a.jpg" target="_blank"
><img width=80 height=60 border=0 src = "/userpics/<?= $mpage->PicPr($q[Id], $q[Userid]) ?>-b.jpg"></a>
<BR>
<small>[ <a href="/userpics/<?= $mpage->PicPr($q[Id], $q[Userid]) ?>-a.jpg" target="_blank" 
>View Full Photo</a> ]</small> 

</td>
      <?

        if ($x % 4 == 3) echo "</tr>";

   } 

   for ($x; $x % 4 != 0; $x++)
   { ?>
<td>&nbsp;</td>
   <? 
        if ($x % 4 == 3) echo "</tr>";
   }   



?>
</table>




<BR><BR>

<div style="width:480px" align=left>

<span class="title1">Add a Picture</span>
<BR>

<table>
<tr><td width=20>&nbsp;</td><td align=right><?= $mpage->user[PicQuota] ?></td>
    <td>Given by default</td>
</tr>
<tr><td>+</td><td align=right><?= $yourkarma ?></td>
    <td>bytes awarded for your karma
</tr>
<tr><td>+</td><td align=right><?= $friendkarma ?></td>
    <td>bytes awarded for karma of your friends (<a href="invite.php">Invite more friends</a>)</td>
</tr>

</tr>
<tr><td>=</td><td align=right><B><?= $totspace ?></b></td>
    <td>total bytes of space allocated to you</td>
</tr>

<tr><td>-</td><td align=right><?= $szeused ?></td>  
    <td>Used Space in Pictures</td>
</tr>

<tr><td>=</td><td align=right><B style="color:red;"><?= $szeallow ?></b></td>
    <td>free bytes for uploading pictures</td>
</tr>

</table>

</div>

<form name=frm method=post action="editphotos.php" 
      ENCTYPE="multipart/form-data" onsubmit="addchng()">

<? if ($errmsg) { ?>

<P>
<table border=4 bordercolor=red cellspacing=0 cellpadding=5px>
<tr><td style="font: bold 16px arial; color:red;">
Error: <?= $errmsg ?>
</td></tr>
</table>
<P>

<? } ?>

<table cellpadding=0 cellspacing=0 width=480>
<tr>
<td class="stdtable" width=150><B>Your Photo</b></td>
<td class="stdfilling" width=330><input name="pic" type=file
    class="input">
<BR><small>(jpeg only)</small>

</td>
</tr>

<tr>        
<td class="stdtable" width=150><B>Small Description</b></td>
<td class="stdfilling" width=330><input name="descr" maxlength=20 
    class="input">
<BR><small>(20 characters max)</small>
    
</td>         
</tr>


<tr><td colspan=2 align=center>
<BR>
<input name="Add" type="hidden" value="1">
<input name="submit" type="submit" class="submit" value="Add Picture">
</td></tr>
</table>
</form>

<script>
  function addchng()
  {
     document.frm.submit.value="Submitting"
  }

</script>


</center>


<?

   $mpage->ClosePage();

function CreateThumb($src, $crop, $ima_w=80, $ima_h=60)
{
   $im = imagecreatefromstring($src);

   $im_h = ImageSY($im);
   $im_w = ImageSX($im);


   $aspect   = ($im_h / $im_w);
   $aspect_i = ($ima_h / $ima_w);


   $ima = ImageCreateTrueColor($ima_w, $ima_h);

   $dstw = ($im_h / $aspect_i);
   $dsth = ($im_w * $aspect_i);




   if ($crop)
   {

      if ($aspect > $aspect_i)
      {
         $isw = $ima_h / $aspect;
         $is  = ($ima_w - $isw) / 2;

         ImageCopyResampled($ima, $im,
                            $is, 0,                    // dstX, dstY  should be 0, 0
                            0, 0,  // srcX, srcY  0, eq
                            $isw, $ima_h,          // dstW, dstH  should be $ima_w, $ima_h
                            $im_w,  $im_h);          // srcW, srcH  0, eq
      }
      else
      {
         $ish = $ima_w * $aspect;
         $is  = ($ima_h - $ish) / 2;

         ImageCopyResampled($ima, $im,
                            0, $is,                    // dstX, dstY  should be 0, 0
                            0, 0,  // srcX, srcY  0, eq
                            $ima_w, $ish,          // dstW, dstH  should be $ima_w, $ima_h
                            $im_w,  $im_h);          // srcW, srcH  0, eq

      }


    
   } else { 




   if ($aspect > $aspect_i)
   {
      ImageCopyResampled($ima, $im,
                         0, 0,                    // dstX, dstY  should be 0, 0
                         0, ($im_h - $dsth) / 4,  // srcX, srcY  0, eq
                         $ima_w, $ima_h,          // dstW, dstH  should be $ima_w, $ima_h
                         $im_w,  $dsth);          // srcW, srcH  0, eq
   }
   else
   {
      ImageCopyResampled($ima, $im,
                         0, 0,
                         ($im_w - $dstw) / 2, 0,
                         $ima_w, $ima_h,
                         $dstw ,  $im_h);
   }

   }


   ob_start();
   ImageJPEG($ima);
   $php = ob_get_contents();
   ob_end_clean();

   ImageDestroy($im);
   ImageDestroy($ima);

   return $php;

}


?>
Return current item: Slashster