Log in

View Full Version : Need Help with image Resize



captainjustin
08-25-2009, 10:22 PM
I have a script that works for my website but, I'm not sure how to resize the image. I have tried to figure this out for some time now, but I'm just not having any luck. I would like the image to be resized to 100X100... I have posted the code below and please keep in mind that I'm very new to php... Thanks in advance for any help!


<?

include_once "accesscontrol.php";


$bdir="../member_image/";


if (isset($submitok) && $_FILES[userfile][size] > '0')
{
if (empty($image_name))
{
echo "<center><span class=TNA>You left blank some of the required fields. </span></center>";
}
else
{
$image_id = time()."_".$_FILES[userfile][name];
copy($_FILES[userfile][tmp_name], "../member_image/$image_id");



$q2 = "update members_info set

image_name = \"$_POST[image_name]\",
image_id = \"$image_id\"

where uname = '$_SESSION[uname]' ";


$r2 = mysql_query($q2) or die(mysql_error());

header("location:s_upload.php");
exit();

}

}

unset($submitok);
unset($userfile);



?>

JShor
08-25-2009, 10:38 PM
You'd need to use the GD library for that. Be sure you have GD enabled/installed on your server, otherwise this will not work. Additionally, be sure that "accesscontrol.php" doesn't call any headers before hand, otherwise, the image resizing functinos for the imagejpeg header will not work either, and you'll return errors.

Code:



<?php

include_once "accesscontrol.php";


$bdir="../member_image/";


if (isset($submitok) && $_FILES[userfile][size] > '0')
{
if (empty($image_name))
{
echo "<center><span class=TNA>You left blank some of the required fields. </span></center>";
}
else
{
$image_id = time()."_".$_FILES[userfile][name];
copy($_FILES[userfile][tmp_name], "../member_image/$image_id");

$filename = $_FILES[userfile][tmp_name];
$newname = "thumb".$filename;

header('Content-type: image/jpeg');

list($width, $height) = getimagesize($filename);
$newwidth = 100;
$newheight = 100;

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $newname);
imagedestroy($thumb);

unlink($filename);
rename($newname, $filename);


$q2 = "update members_info set

image_name = \"$_POST[image_name]\",
image_id = \"$image_id\"

where uname = '$_SESSION[uname]' ";


$r2 = mysql_query($q2) or die(mysql_error());

header("location:s_upload.php");
exit();

}

}

unset($submitok);
unset($userfile);



?>


HTH:)

AdrielGreene
08-28-2009, 08:25 AM
where you display the code adjust the image source.

<img src="image.jpg" width="X" height="X">

Why include this line?
$bdir="../member_image/";

For one of my sites the image is called like this:


//If image display is confirmed
if ($_SESSION['imageconfirm'] == '1')
{
//display image with given size
echo '<img width="175" height="175" src="' . $_SESSION['image'] . '">';
}

I think this is the line your looking for:
echo '<img width="175" height="175" src="' . $_SESSION['image'] . '">';

There are nicer ways to do this that are beyond me. Ways to avoid image distortion by scaling the size of the image within certain limits. We'll need a better programmer then myself for that however.

Best of luck.