Hello,
Here's some code I'm using to store user's avatars. I have the paths' mapped
to
defines. It removes the original image and converts to png (which is
hardwired)
and then saves JUST the filename to the database (since I know the path,
DEFINE).
I use the PEAR FileUpload( ) Class to take care of moving the files.
and the ImageTransform( ) (which is some kinda derivation of the PEAR
Image_Transform() class)
to resize the image.
Thatttsss it!
if ( $stateObject->Get('do_avatar') == TRUE )
{
$uploadObject = new FileUpload(0666);
$fileName = $uploadObject->MoveFile(USER_IMAGES_PATH,
'uploadImage');
if ( $fileName == PROC_FAILURE )
{ /* There has been an unrecoverable error. */
error_log(USER_IMAGES_PATH . ' directory does not exist');
}
$imageObject = new ImageTransform( );
$imageObject->sourceFile = USER_IMAGES_PATH . $fileName;
$newFileName = substr($fileName, 0, strrpos($fileName,"."));
$newFileName .= '.png';
$imageObject->targetFile = USER_IMAGES_PATH . $newFileName;
$imageObject->resizeToHeight = 90;
$imageObject->resizeToWidth = 110;
if ( !$imageObject->resize( ) )
{
error_log($imageObject->error);
}
/* Remove the uploaded image. */
unlink(USER_IMAGES_PATH . $fileName);
/* Update the users thumbnail in the database. */
$dbObject->DatabaseQuery('UPDATE ' . DATABASE_TABLE_USERS
. ' SET thumbnail = ' . $dbObject->Safe($newFileName)
. ' WHERE username = '
. $dbObject->Safe($sessionObject->Get(SESSION_USER))
. ' AND email = '
. $dbObject->Safe($sessionObject->Get(SESSION_EMAIL))
. ' AND id = '
. $dbObject->Safe($sessionObject->Get(SESSION_DBID)),
RETURN_NONE, LOG_LEVEL_DEBUG);
$avatarImage = $newFileName;
}
.....keeps going.....
- Ben
Ben Sgro, President
ProjectSkyLine - Defining New Horizons
+1 718.487.9368 (N.Y. Office)
Our company: www.projectskyline.com
Our products: www.project-contact.com
This e-mail is confidential information intended only for the use of the
individual to whom it is addressed.
----- Original Message -----
From: "Anthony Wlodarski" <[EMAIL PROTECTED]>
To: "'NYPHP Talk'" <[email protected]>
Sent: Friday, October 26, 2007 9:31 AM
Subject: RE: [nyphp-talk] opening files
I believe the simple way would to be to store a path in a database. Here
is
an example of an upload procedure to add files to the directory (must be
world writeable though and does not provide for mime types, just extension
checks since mime_content_type() and PEAR extensions might not be an
option
on shared hosting).
<?php
/*
* Created on Oct 14, 2007
*
* This application uploads a photo to be used for the profile. The image
* will be no larger than 250 x 250 pixels. If the image is not of type
gif/jpeg/png
* the user will be asked to upload a new photo that meets the
requirements,
additionally
* the max file size for a picture upload is two megabytes.
*/
// pre: cookie is set for user id, source image is uploaded and mime type
is
checked
// post: db is updated with link to profile photo
function buildImage($userId, $srcImage, $extension)
{
$uploadDir = './images/profilePhotos/';
// try to make sure no one has a collision of profile files
$uploadFile = $uploadDir . md5($_SESSION['id']);
// get our image dimensions so we can calculate a resize/aspect
ratio
list($width,$height) = getimagesize($srcImage);
// check what axis we are going to calculate our resize ratio from
if($width > $height)
{
$ratio = 250/$width;
$newWidth = 250;
$newHeight = $height * $ratio;
}
else
{
$ratio = 250/$height;
$newWidth = $width * $ratio;
$newHeight = 250;
}
// create an image resource from the temp uploaded file
if($extension == 'jpeg')
{
$image = imagecreatefromjpeg($srcImage);
}
elseif($extension == 'gif')
{
$image = imagecreatefromgif($srcImage);
}
elseif($extension == 'png')
{
$image = imagecreatefrompng($srcImage);
}
else
{
//output an error for extensions checks and die()
}
// create an image resource with the new dimensions
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
// copy the image with resampling
imagecopyresampled($resizedImage, $image, 0, 0, 0, 0, $newWidth,
$newHeight, $width, $height);
// save the resized image resource to a file
// create an image resource from the temp uploaded file
if($extension == 'jpeg')
{
imagejpeg($resizedImage, $uploadFile.".".$extension, 100);
}
elseif($extension == 'gif')
{
imagegif($resizedImage, $uploadFile.".".$extension);
}
else
{
imagepng($resizedImage, $uploadFile.".".$extension);
}
// update the database with the new image link
// query data from the database for use in building the page
$dbh = mysql_connect("localhostorIPofhost", "putyourusernamehere",
"putyourpasswordhere");
// select the database
mysql_select_db("anthonyw_mystory");
$query = sprintf("UPDATE `anthonyw_mystory`.`users` SET
`profilePhoto` = '%s' WHERE `users`.`id` ='%s' LIMIT
1",$uploadFile.".".$extension,$userId);
// make change in database
$results = mysql_query($query);
// close the database connection
mysql_close($dbh);
// return them back to the lobby;
include './lobby.php';
}
// the only way we can get to this script is when someone submits
// a form submission to this script
// copy their userID
session_start();
$userId = $_SESSION['id'];
// since mime_content_type or fileinfo are not available we must
rely on the browser
// and see if it has uploaded the mime type of the file
$mimeString = $_FILES['userfile']['type'];
// now test the mime string for varying different types
if(strpos($mimeString,"jpeg") || strpos($mimeString,"jpg"))
{
$mimeType = 'jpeg';
}
elseif(strpos($mimeString,"gif"))
{
$mimeType = 'gif';
}
elseif(strpos($mimeString,"png"))
{
$mimeType = 'png';
}
else
{
$mimeType = 'invalid';
}
buildImage($userId, $_FILES['userfile']['tmp_name'], $mimeType);
?>
And this is how I pull the file out for an image wrapped in an 'img' tag:
"<img src=\"".$row['profilePhoto']."\">"
But this assumes that $row is a successful mysql_query. There are
additional functions if you know that the extension they can display
images
or image resources: imagejpeg(), imagegif(), imagepng(). I did not
include
the actual HTML form as I am assuming you can google that or look it up on
PHP.net
Anthony Wlodarski
[EMAIL PROTECTED]
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php