Question:
How to crop profile image automatically using php?
anonymous
2011-04-28 05:42:30 UTC
I'm making a user profile page using php. I wrote a script by which a user can upload his picture but the problem for me is when I echo these pictures on other side with width='120' height='200' , it stretches because all the images have not exactly the width I mentioned. Can any one tell me how to get the height and set width automatically according it or how can I crop image and store in MYSQL data?

Code for upload image file is here.

$_SESSION ['email'];

$email = $_SESSION['email'];

if (isset($_POST["submit"]))
{

//get file attributes
$name = $_FILES['myfile']['name'];
$tmp_name = $_FILES['myfile']['tmp_name'];

if ($name)
{

//start upload process

$location = "avatars/$name";
move_uploaded_file($tmp_name, $location);

$query = mysql_query("UPDATE images SET imagelocation='$location' WHERE email='$email' ");

die ("Your profile image has been uploaded! Profile ");


}
else
die("Please select a file!");

}
echo "Welcome, ".$email."!

";

echo "Upload your image:


File:

";
?>

and code for echo the image is here.


$error = "Unable to Connect.";
$connect = mysql_connect("localhost","root","") or die ($error);
mysql_select_db("phplogin") or die ($error);

$email = isset($_SESSION['email']);
$query = mysql_query("SELECT * FROM images WHERE email = '$email' ");

if(mysql_num_rows($query)==0)
die ("User not found!");
else
{

$row = mysql_fetch_assoc($query);
$location = $row['imagelocation'];

echo "";
}

?>

It's time consuming I know. But if any one have any tutorial on this topic please just send me link.
Thanks in advance.
Three answers:
just "JR"
2011-04-28 07:17:41 UTC
Your file upload is pretty poorly written...

Here is a neater one:

UPLOADING FILES



The type FILE will show an input field and a "browse" button.

Clicking the browse button will allow the user to select a file to

upload.

Clicking the submit will submit the form to this same file.



Your "upfiles.php":




function UploadOne($fname)

{

$uploaddir = 'uploadedfiles/';

if (is_uploaded_file($fname['tmp_name']))

{

$filname = basename($fname['name']);

$uploadfile = $uploaddir . basename($fname['name']);

if (move_uploaded_file ($fname['tmp_name'], $uploadfile))

$res = "File " . $filname . " was successfully uploaded and stored.
";

else

$res = "Could not move ".$fname['tmp_name']." to ".$uploadfile."
";

}

else

$res = "File ".$fname['name']." failed to upload.";

return ($res);

}

?>






if ($_FILES['picture']['name'] != "")

{

$res = UploadOne($_FILES['picture']);

$filname = $_FILES['picture']['name'];

echo ($res);

}

?>

UPLOADING FILES















Finally: you must create a directory where to put the uploaded files, and that

directory MUST have permissions set to 0777... (here called "uploadedfiles")

(This set of functions works, but is NOT secure against viruses or "injection":

the prevention of these is a much more complicated matter!)



Now, for the resize:

function make_thumb($srcpic,$dest) // will make image 180 x 180 MAX, keeping proportions...

{

$siz = getimagesize($srcpic);

$src_w = $siz[0]; // pxl

$src_h = $siz[1]; // pxl

$k = $src_w / 180; // max width

$dst_w = 180; // max height

$txt = "image size: ".$src_w." x ".$src_h."
";

if ($k == 0)

return ($txt . "
Error in image resizing: k=0");

$dst_h = $src_h / $k;

$dsti = imagecreatetruecolor ($dst_w,$dst_h);

if (!$dsti)

return($txt ."
ERROR: cannot create image true color dsti.
");

ImageColorTransparent($dsti, ImageColorAllocate($dsti, 0, 0, 0));

ImageAlphaBlending($dsti, false);

$srci = imagecreatefromjpeg($srcpic);

if (!$srci)

return($txt . "
ERROR: cannot create image from jpeg.
");

$dst_x = $src_x = $dst_y = $src_y = 0;

$res = imagecopyresampled ($dsti,$srci,$dst_x,$dst_y,$src_x,$src_y,$dst_w,$dst_h,$src_w,$src_h);

if (!$res)

return ($txt . "
Failing to create copyresampled.");

$res = imagejpeg($dsti,$dest,100);

imagedestroy($dsti);

if (!$res)

return($txt . "
ERROR: cannot create final image");

$txt .= "Thumb is made.
";

return($txt);

}
Growl
2011-04-28 12:53:49 UTC
Omit height or width



echo "";



will fix the width at 120 and scale the height proportionally
juliepelletier
2011-04-28 12:55:02 UTC
Check out PHP's GD module: http://www.php.net/manual/en/book.image.php



I did a quick search for more specific instructions and found http://icant.co.uk/articles/phpthumbnails/ which should be quite to the point.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...