Jump to content

Image Resize Problem


Ancoats

Recommended Posts

Hi all,

 

I have a page/form which is a member profile page, where the basic idea is that the user, once logged in, will be able to create a personal member profile, and have the option to select an image they wish to upload... all this works fine (the image selected gets uploaded and moved to the specified folder I have set for it.)

 

However, I am currently having problems getting the uploaded image to 'resize' to 150 px 150px (ignore the example in this code, I know its set to half the image size... but even this doesnt work) but this doesnt seem to be working... as the original image size is on the server. Been trying to work out where I am going wrong and assume that maybe its something to do with the overall code, not just the actual bit that resizes?

 

Anyway, any help or pointers would be helpful. (I am not a PHP guru so go easy, lol)

 

Code is

 

<?php
//start the session
session_start();

//connect to database
include ('includes/db.php');
?>


  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>


<?php

//FORM CODE

//function to generate errors
function errors($error){
if (!empty($error))
{
$i = 0;
while ($i < count($error)){
echo "<p><span class=\"warning\"><b>".$error[$i]."</b></span></p>\n";
$i ++;}

} // close if empty errors
} //close function



//code that runs if the form has been submitted
if (isset($_POST['submit']))
{

//check fields are not empty
$firstname = trim($_POST['firstname']);
if (strlen($firstname) < 2) {
$error[] = 'first name must be between 2 and 20 chars';
}

$lastname = trim($_POST['lastname']);
if (strlen($lastname) < 3) {
$error[] = 'last name must be between 3 and 20 chars';
}

$userDOB = trim($_POST['userDOB']);
if (strlen($userDOB) < 3) {
$error[] = 'date of birth must be between 3 and 20 chars';
}

$userGend = trim($_POST['userGend']);
if (strlen($userGend) < 3) {
$error[] = 'Gender must be between 3 and 20 chars';
}

$userLoc = trim($_POST['userLoc']);
if (strlen($userLoc) < 3) {
$error[] = 'Location must be between 3 and 255 chars';
}

$userInt = trim($_POST['userInt']);
if (strlen($userInt) < 3) {
$error[] = 'Interests must be more than 3 characters';
}

$userDesc = trim($_POST['userDesc']);
if (strlen($userDesc) < 3) {
$error[] = 'Description must be more than 3 characters';
}


//checks for allowed file type (jpg, gif or png)
switch ($_FILES['userPic']['type'])

{
case $_FILES['userPic']['type'] == 'image/jpg':
break;

case $_FILES['userPic']['type'] == 'image/jpeg':
break;


case $_FILES['userPic']['type'] == 'image/gif':
break;

case $_FILES['userPic']['type'] == 'image/png':
break;

default:
$error[] = 'wrong file type';

}

//checks if file size is over 2mb, and if it is, refuse
switch ($_FILES["userPic"]["size"]) {
case $_FILES["userPic"]["size"] > 2097152:
    $error[] = 'Image size cannot be bigger then 2MB!';	
    break;
}

if(!$error)
{


//moves uploaded file from temp folder to set folder (which will be ../images/userpics folder)
move_uploaded_file ($_FILES['userPic']['tmp_name'], "images/userpics/".$_FILES['userPic']['name']) 
or die ("Could not move");


//Assign the paths needed to variables for use in the script
$filePathFull = "images/userpics/".$_FILES["userPic"]["name"];
$filePathThumb = "images/userpics/";

//Assign the files TMP name and TYPE to variable for use in the script
$tmpName = $_FILES["userPic"]["tmp_name"];
$imageType = $_FILES["userPic"]["type"];

//Use a switch statement to check the extension of the file type. If file type is not valid echo an error
switch ($imageType) {
	case $imageType == "image/gif":
		move_uploaded_file($tmpName,$filePathFull);
		break;
	case $imageType == "image/jpeg":
		move_uploaded_file($tmpName,$filePathFull);		
		break;
	case $imageType == "image/pjpeg":
		move_uploaded_file($tmpName,$filePathFull);		
		break;	
	case $imageType == "image/png":
		move_uploaded_file($tmpName,$filePathFull);		
		break;
	case $imageType == "image/x-png":
		move_uploaded_file($tmpName,$filePathFull);		
		break;	
	default:
		echo 'Wrong image type selected. Only JPG, PNG or GIF formats accepted!.';
}

	// Get information about the image
        list($src_width, $src_height, $type, $attr) = getimagesize($filePathFull);

	//Create the correct file type based on imagetype
        switch( $type ) {
        case IMAGETYPE_JPEG:
                $starting_image = imagecreatefromjpeg( $filePathFull );
                break;
        case IMAGETYPE_PNG:
                $starting_image = imagecreatefrompng( $filePathFull );
                break;
        case IMAGETYPE_GIF:
                $starting_image = imagecreatefromgif( $filePathFull );
                break;
        default:
                return false;
        }


//Get the image to create thumbnail from
    $starting_image; 

//Get image height and width
$width = imagesx($starting_image);
    $height = imagesy($starting_image);

//Create the dimesnsions for the thumbnail
$thumb_width = $width/2;
$thumb_height = $height/2;

//Create the thumbnail with true colours
    $thumb_image = imagecreatetruecolor($thumb_width, $thumb_height);

//Generate the resized image image
imagecopyresized($thumb_image, $starting_image, 0,0,0,0,  $thumb_width, $thumb_height, $width, $height);	

//Generate a random number to append the filename.
$ran = "thumb_".rand () ;	
$thumb2 = $ran.".jpg";


//global $thumb_Add_thumb;

//Create the path to store the thumbnail in.
$thumb_Add_thumb = $filePathThumb;
$thumb_Add_thumb .= $thumb2;	

//Create the new image and put it into the thumbnails folder.
imagejpeg($thumb_image, "" .$filePathThumb. "$thumb2"); 







//escapes data is magic quotes are disabled on the server
if(!get_magic_quotes_gpc())
{
$firstname = addslashes($firstname);
$lastname = addslashes($lastname);
$userDOB = addslashes($userDOB);
$userGend = addslashes($userGend);
$userLoc = addslashes($userLoc);
$userInt = addslashes($userInt);
$userDesc = addslashes($userDesc);
$userPic = addslashes($userPic);
}


//escape any harmful code and prevent sql injection attacks
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$userDOB = mysql_real_escape_string($userDOB);
$userGend = mysql_real_escape_string($userGend);
$userLoc = mysql_real_escape_string($userLoc);
$userInt = mysql_real_escape_string($userInt);
$userDesc = mysql_real_escape_string($userDesc);
$userPic = mysql_real_escape_string($userPic);


//prevents escaped code showing
$firstname = strip_tags($firstname);
$lastname = strip_tags($lastname);
$userDOB = strip_tags($userDOB);
$userGend = strip_tags($userGend);
$userLoc = strip_tags($userLoc);
$userInt = strip_tags($userInt);
$userDesc = strip_tags($userDesc);
$userPic = strip_tags($userPic);


//reference the ismember session 
$memberID = $_SESSION['ismember'];


//insert data into the memberprofile table
$sql = mysql_query("INSERT INTO memberprofile (firstname, lastname, userDOB, userGend, userLoc, userInt, userDesc, userPic)
				VALUES('$firstname', '$lastname', '$userDOB', '$userGend', '$userLoc', '$userInt', '$userDesc', '".$_FILES['userPic']['name']."') 	WHERE memberID='$memberID'");


//if data has been inserted
echo "<p>Thank you. Your member profile has been created</p>";	

}//if no error
}//close form if sumbit			

//print any errors
errors($error);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.