And the class functions...
function check_type($type){
if ($type != "image/x-png" && $type != "image/pjpeg" && $type != "image/jpeg" && $type != "image/png") {
return "Error0";
}
return 1;
}//end function
//-----------------------------------------------------------------------------------
function check_size($size){
if ($size > $this->maxfilesize) {
return "Error1";
}
return 1;
}//end function
//------------------------------------------------------------------------------------
function check_upload($file){
echo ("Starting check_upload function in class<br>");
if (!is_uploaded_file($file)) {
echo ("No file uploaded, returning Error2<br>");
return "Error2";
}
echo ("File uploaded, returning 1<br>");
return 1;
}//end function
//------------------------------------------------------------------------------------
function add_photo($userid, $title, $phototext, $year, $school, &$db) {
$phototext = trim($phototext);
$title = trim($title);
//if text is blank...
if ($phototext == "") return "Error4";
if (!$phototext) return "Error4";
//if no recipient
if($title=="") return "Error5";
if(!$title) return "Error5";
//check year
if($year=="00") return "Error6";
if(!$year) return "Error6";
$date = $year . "-00-00";
// Add slashes to text
$title = addslashes($title);
$phototext = addslashes($phototext);
//get album id
$albumid = $this->checkalbum($userid, $db);
//now add to db
$messagesql = "INSERT INTO photo SET userid='$userid', schoolid='$school', title='$title', description='$phototext', recorddatetime=NOW(), photodate='$date', albumid='$albumid[id]'";
$messageresult = $db->query($messagesql);
//did it work?
if (!messageresult) return "Error3";
//check if user has photo flag set, if not set it
$photocheck = $db->getOne("SELECT flagphoto FROM user WHERE id='$userid'");
if ($photocheck == 0) {
//change flag
$update = $db->query("UPDATE user SET flagphoto='1' WHERE id='$userid'");
}
//echo "Photo added!<br>";
return 1;
}//end function
//-------------------------------------------------------------------------------------------------
//This function returns text of a given error number
function get_error ($error) {
//remove 'Error' from the front of the variable
$temp = ereg_replace("Error", "", $error);
//Return the text error
return $this->error[$temp];
}
//-----------------------------------------------------------------------------------------------
function process($imagefile, $imagename, $photoid, &$db)
{
$max_width = $this->max_image_width;
$max_height = $this->max_image_height;
if(eregi("\.png$",$imagename))
{
$img = ImageCreateFromPNG ($imagefile);
}
if(eregi("\.(jpg|jpeg)$",$imagename))
{
$img = ImageCreateFromJPEG ($imagefile);
}
if(eregi("\.gif$",$imagename))
{
$img = ImageCreateFromGif ($imagefile);
}
//get image size
$FullImage_width = imagesx ($img);
$FullImage_height = imagesy ($img);
//check if size is bigger than limit, if not then exit
if ($FullImage_width < $max_width) $max_width = 0;
if ($FullImage_height < $max_height) $max_height = 0;
//if finite size is set then do it
if(isset($max_width) && isset($max_height) && $max_width != 0 && $max_height != 0)
{
$new_width = $max_width;
$new_height = $max_height;
}
//else if width is set, work out new height with same ratio
else if(isset($max_width) && $max_width != 0)
{
$new_width = $max_width;
$new_height = ((int)($new_width * $FullImage_height) / $FullImage_width);
}
//else if height is set, work out new width with same ratio
else if(isset($max_height) && $max_height != 0)
{
$new_height = $max_height;
$new_width = ((int)($new_height * $FullImage_width) / $FullImage_height);
}
else
//else don't resize
{
$new_height = $FullImage_height;
$new_width = $FullImage_width;
}
//make the new image
$full_id = ImageCreateTrueColor ( $new_width , $new_height );
ImageCopyResampled ( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );
//all images are saved as jpg
$full = ImageJPEG( $full_id, $this->app_root.$this->photo_url."$photoid.jpg",$this->jpeg_quality);
//echo "Image processed!<br>";
//update db
$this->finaldata($photoid, $new_width, $new_height, $db);
ImageDestroy( $full_id );
unset($max_width);
unset($max_height);
//make thumbnail
$this->thumbnail($this->app_root.$this->photo_url."$photoid.jpg", $this->app_root.$this->thumb_url."$photoid.jpg", $this->thumb_width, $this->thumb_height, $this->jpeg_thumb_quality);
return 1;
}//end function
//--------------------------------------------------------------------------------------------------
function thumbnail($sourcefile, $destfile, $fw, $fh, $jpegquality = 60)
{
list($ow, $oh, $from_type) = getimagesize($sourcefile);
switch($from_type)
{
case 1: // GIF
$srcImage = imageCreateFromGif($sourcefile);
break;
case 2: // JPG
$srcImage = imageCreateFromJpeg($sourcefile);
break;
case 3: // PNG
$srcImage = imageCreateFromPng($sourcefile);
break;
}
$tempw = $fw;
$temph = number_format((($oh*$fw)/$ow), 0);
if($temph < $fh)
{
$tempw = number_format((($ow*$fh)/$oh), 0);
$temph = $fh;
}
$tempImage = imageCreateTrueColor($tempw, $temph);
//imageAntiAlias($tempImage, true);
imagecopyresampled($tempImage, $srcImage, 0, 0, 0, 0, $tempw, $temph, $ow, $oh);
// Calculate offsets
if($temph > $fh)
{
$offsety = number_format(($temph/2)-($fh/2), 0);
$offsetx = 0;
}
else
{
$offsety = 0;
$offsetx = number_format(($tempw/2)-($fw/2), 0);
}
$destImage = imageCreateTrueColor($fw, $fh);
imagecopyresampled($destImage, $tempImage, 0, 0, $offsetx, $offsety, $fw, $fh, $fw, $fh);
imageJpeg($destImage, $destfile, $jpegquality);
//echo "Thumbnail created!<br>";
} // End Function
//-------------------------------------------------------------------------------------------
function finaldata($id, $width, $height, &$db){
$filename = $id.".jpg";
$sql = "UPDATE photo SET filename='$filename', photowidth='$width', photoheight='$height' WHERE id='$id'";
$result = $db->query($sql);
$url = $this->fullsiteroot . "/module.admin/photo.display/?id=$id";
/*
require("../classes/oymailclass.php");
$oymail = new oymail;
@$oymail->sendadmincontent($url);
unset($oymail);
*/
//echo "Final data added!<br>";
return 1;
}//end function
//----------------------------------------------------------------------------------------------
function make_default($id, $userid, &$db){
$sql = "UPDATE user SET profilephotoid='$id' WHERE id='$userid'";
$result = $db->query($sql);
//echo "Made default!<br>";
return 1;
}//end function
#-----------------------------------------------------------------------------------------
function checkalbum($userid, &$db) {
echo ("Starting checkalbum function in class<br>");
// first we check if the user has an album already as we allow ONE album per person
$checkalbum['id'] = $db->getOne("SELECT id FROM photoalbum WHERE userid='$userid' LIMIT 1");
if($checkalbum['id']){
echo ("Album exists, getting number of photos<br>");
//get the number of photos in album
$checkalbum['count'] = $db->getOne("SELECT COUNT(id) FROM photo WHERE albumid='$checkalbum[id]' AND dbstatus < 3");
echo ("$checkalbum['count'] photos<br>");
//return id of album
echo ("Returning data array<br>");
return $checkalbum;
}else{
echo ("No album exists, returning 0<br>");
return 0;
}
} // end function
#-----------------------------------------------------------------------------------------
function createalbum($userid, $albumname, &$db) {
echo ("Starting createalbum funcion in class<br>");
$albumsql = "INSERT INTO photoalbum SET userid='$userid', name='$albumname', recorddatetime=NOW()";
$album = $db->query($albumsql);
$newalbumid = mysql_insert_id();
echo ("Album created, id=$newalbumid<br>");
//now, if the user has a profile photo already we include that photo in this new album.
$include = $db->query("UPDATE photo SET albumid='$newalbumid' WHERE userid='$userid'");
echo ("Existing photo added to album<br>");
//return the id of the album
echo ("Returning new album id $newalbumid<br>");
return $newalbumid;
} // end function
##-------------------------------------------------------------------------------------------
function checkfirstphoto($userid, &$db) {
echo ("Starting checkfirstphoto function in class<br>");
// first we check if the user has an album already as we allow ONE album per person
$result = $db->getOne("SELECT COUNT(id) FROM photo WHERE userid='$userid' AND dbstatus<>4");
if($result){
//user has photos
echo ("User has photo, returning 1<br>");
return 1;
}else{
//user has no photos
echo ("User has no photo, returning 0<br>");
return 0;
}
} // end function
Yes it's messy, but the logic is correct I am pretty sure...