i need help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

i need help!

Post by Maluendaster »

i've read all the tutorials regarding this, and i still cant get it to work, is out there any script that can :
upload an image, resize it (to my specs), and rename it (randomly, something like img9938kkkd3.jpg)

i havent found yet, and i've been searching a loooooot.....

thank you!
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

I JUST made this for my image upload feature.

it doesnt resize, but will restrict uploads to not
be larger then the size you set.

It will also rename images with the format of
month-day-year-hour-minute-second.original name
and should easily be able to be changed to name whatever you want.
I show below what could be used to make the code have
a random name for an image

Oh, it also does NOT CHMOD the image so that would probably
need to be added.

The below offers the chance for two image upload fields:

Code: Select all

//Edit allowed max attributes
    $maxwidth = 400;
    $maxheight = 400;
    $max_filesize = 102400;

//Edit folder being uploaded to and allowed types
    $uploads = './ratings';
    $types_array = array('image/gif','image/pjpeg','image/x-png');


//If upload form field empty, tell user to upload
//expic would be name of the form field
if($_FILES['expic']['name'] == "")
{
echo "<a href='javascript:history.go(-1)'>TRY AGAIN</a><br>";
 echo "Please upload a Pic Of Your Ex!\n";
 exit;
}
//Same as above
if($_FILES['yourpic']['name'] == "")
{
echo "<a href='javascript:history.go(-1)'>TRY AGAIN</a><br>";
 echo "Please upload a Pic Of Yourself!!</center></p>\n";
 exit;
}

//Now check to make sure both form
//uploads are of allowed file type
if(!in_array($_FILES['expic']['type'], $types_array))
{
    echo "That file type is not allowed!\n<br>";
     echo "Allowed File Types: <b>jpg, gif, png</b>\n<br>";
    exit;
}

if(!in_array($_FILES['yourpic']['type'], $types_array))
{
    echo "That file type is not allowed!\n<br>";
     echo "Allowed File Types: <b>jpg, gif, png</b>\n<br>";
    exit;
}

//Now check to make sure both uploads
//don't exceed the allowed size limit set above
    $max_filesize_kb = ($max_filesize / 1024);

if($_FILES['expic']['size'] && $_FILES['yourpic']['size'] > $max_filesize)
{
    echo "You image is too large, files may be up to ".$max_filesize_kb."kb\n";
    exit;
}

//Now check the upload width/height to make sure
//both are within the allowed range
    $imagesize = getimagesize($_FILES['expic']['tmp_name']);
    $imagewidth = $imagesize[0];
    $imageheight = $imagesize[1];
    $yimagesize = getimagesize($_FILES['yourpic']['tmp_name']);
    $yimagewidth = $yimagesize[0];
    $yimageheight = $yimagesize[1];

if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
    echo "Your image:<br /> ".$_FILES['expic']['name']." <br />is too large, <br />files may be up to ".$maxwidth."px x ".$maxheight."px in size<br /><a href=testu.php>Please Try Again</a>\n";
    exit;
}elseif($yimagewidth > $maxwidth || $yimageheight > $maxheight)
{
    echo "Your image:<br /> ".$_FILES['yourpic']['name']." <br />is too large, <br />files may be up to ".$maxwidth."px x ".$maxheight."px in size<br /><a href=testu.php>Please Try Again</a>\n";
    exit;
}else{ 

//If all is ok then lets now rename image
//can change $uniq to be anything you want
//this is what the image will be renamed to
//and still keep the original name after the new is created

//you could probably use something like
//$uniq = sha1(uniqid(rand(), true));
//to create a random name for each image
	$uniq = date( "m-d-Y-G-i-s" );
	$ext = strtolower($_FILES['expic']['name']);
	$ext = preg_replace('/\s\s+/', '', $ext);
	$ext = preg_replace('/\'/', '', $ext);

   move_uploaded_file($_FILES['expic']['tmp_name'], $uploads."/".$uniq."_".$ext )
   or die ("Couldn't upload ".$_FILES['expic']['name']."\n");
   $newexpicurl = "{$uniq}_{$ext}";

//And do it again for the second upload field
                $yuniq = date( "m-d-Y-G-i-s" );
	$yext = strtolower($_FILES['yourpic']['name']);
	$yext = preg_replace('/\s\s+/', '', $yext);
	$yext = preg_replace('/\'/', '', $yext);
   move_uploaded_file($_FILES['yourpic']['tmp_name'], $uploads."/".$yuniq."_".$yext )
   or die ("Couldn't upload ".$_FILES['yourpic']['name']."\n");
   $newyourpicurl = "{$yuniq}_{$yext}";
   }
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

Yeah - search the forums as well- I posted some code here that does *exactly* what they are looking for - takes an uploaded image as a stream, re-sizes it, and then saves it with an md5() hash as its name. if it isnt in these forums, its probably over at D2
Maluendaster
Forum Contributor
Posts: 124
Joined: Fri Feb 25, 2005 1:14 pm

Post by Maluendaster »

thank you both! , i'll try..

ps : what is D2?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

d2 = devnetwork2.net/
Post Reply