...

View Full Version : Resizing code



graham23s
11-07-2007, 09:27 AM
Hi Guys,

i have made from a few tutorials code to resize uploaded images but it isn't making the thumbnails code:


#############################################
# Thumbnail code #
#############################################
## Find out the files extension
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];

## random numbers
$randomnumber = rand(0,999999999);

if($ext == "jpg" || $ext == "jpeg")

$image = imagecreatefromjpeg($filetemp);

elseif($ext == "png")

$image = imagecreatefrompng($filetemp);

elseif($ext == "gif")

$image = imagecreatefromgif($filetemp);

## save the file in %
$size = 0.50;

## rename the thumbnail
$newimagename_thumb = $var_loggedinuser. "-" .time();
$save = "thumbs/$newimagename_thumb.$ext";

## get the files dimensions
list($width,$height) = getimagesize($filetemp);

$modwidth = $width * $size;
$modheight = $height * $size;
$thumbnail = imagecreatetruecolor($modwidth, $modheight) ;
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
## imagejpeg($thumbnail, $save, 100);

if($ext == '.jpg' || $ext == '.jpeg')

imagejpeg($thumbnail, $save, 100);

if($ext == '.gif')

imagegif($thumbnail, $save, 100);

if($ext == '.png')

imagepng($thumbnail, $save, 100);
#############################################
# Thumbnail code #
#############################################

can anyone see whats wrong at all?

thanks guys

Graham

shyam
11-07-2007, 03:15 PM
while running the script does it throw any error messages?

CFMaBiSmAd
11-07-2007, 03:48 PM
A) Where are the variables $filename and $filetemp getting set at? Are you sure they contain what you expect?

B) All the image functions return a FALSE value when they fail. Where is your error checking, error reporting, and error recovery logic to test if any of the functions have failed, tell you what failed, and prevent the following code from blindly executing on non-existent data?

C) You are exploding the $filename using "." and then checking if $ext matches "jpg", "jpeg", "png", and "gif" at one point in your code and checking if $ext matches '.jpg', '.jpeg', '.gif', and '.png' later in your code. Don't you suppose that might be a reason why the conditional tests are not working.

graham23s
11-07-2007, 07:16 PM
Hi Guys,

sorry this is the full script:


<?php
## See if it's a first time user
$query_user = "SELECT `photo` FROM `users` WHERE `id`='$var_loggedinuserid'";
$results_user = mysql_query($query_user) or die ("Error getting photo 1");
$row = mysql_fetch_array($results_user) or die ("Error getting the photo array");
$photouploaded = $row['photo'];

## HTML form
echo ('<div style="border: 1px solid black;padding:10px; background: yellow; color: #000000; font-size: 12px;"><b>Upload your main profile headshot here.</b></div><br />');
echo ("<form action=\"uploadphoto.php\" method=\"post\" enctype=\"multipart/form-data\" />");
echo ("<table class=\"sub_table\" width=\"500\" border=\"1\" align=\"center\" cellpadding=\"5\" cellspacing=\"0\">");
echo ("<tr>");
echo ("<td colspan=\"2\" class=\"edit\" align=\"left\"><img src=\"images/upload_headshot.jpg\"></td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td align=\"center\"><b>Select a photo to upload</b></td><td align=\"center\"><input name=\"usersphoto\" type=\"file\" size=\"50\" /></td>");
echo ("</tr>");
echo ("<tr>");
echo ("<td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Upload Photo\" /></td>");
echo ("</tr>");
echo ("</table>");
## HTML form

## Deal with the submission
if($_POST['submit']) {

$filesize = $_FILES['usersphoto']['size'];
$filetype = $_FILES['usersphoto']['type'];
$filetemp = $_FILES['usersphoto']['tmp_name'];
$filename = $_FILES['usersphoto']['name'];

## vars
$maxheight = 500;
$maxwidth = 500;
$newimagename = $var_loggedinuser. "-" .time();

## Allowed file types
$allowed_types = array('image/pjpeg','image/gif','image/png','image/jpeg');

if($filesize == 0) {

stderr("Upload Failed","No file was uploaded.");
include("includes/footer.php");
exit;

}

if(!in_array($filetype, $allowed_types)) {

stderr("Upload Failed","The file you uploaded is not one of the allowed types only .gif and .jpg are allowed.");
include("includes/footer.php");
exit;

}

## Rename the file
$renamedimage = $newimagename.".".substr($_FILES["usersphoto"]["name"],strtolower(strlen($_FILES["usersphoto"]["name"]))-3,3);

#############################################
# Thumbnail code #
#############################################
## Find out the files extension
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];

## random numbers
$randomnumber = rand(0,999999999);

if($ext == "jpg" || $ext == "jpeg")

$image = imagecreatefromjpeg($filetemp);

elseif($ext == "png")

$image = imagecreatefrompng($filetemp);

elseif($ext == "gif")

$image = imagecreatefromgif($filetemp);

## save the file in %
$size = 0.50;

## rename the thumbnail
$newimagename_thumb = $var_loggedinuser. "-" .time();
$save = "thumbs/$newimagename_thumb.$ext";

## get the files dimensions
list($width,$height) = getimagesize($filetemp);

$modwidth = $width * $size;
$modheight = $height * $size;
$thumbnail = imagecreatetruecolor($modwidth, $modheight) ;
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
## imagejpeg($thumbnail, $save, 100);

if($ext == '.jpg' || $ext == '.jpeg')

imagejpeg($thumbnail, $save, 100);

if($ext == '.gif')

imagegif($thumbnail, $save, 100);

if($ext == '.png')

imagepng($thumbnail, $save, 100);
#############################################
# Thumbnail code #
#############################################

$uploaddirectory = "uploads/".$renamedimage;

## Upload code
if(move_uploaded_file($filetemp, $uploaddirectory)) {

## insert the photo in the database
$photoquery = mysql_query("UPDATE `users` SET `photo`='$renamedimage',`thumbnail`='$save' WHERE `id`='$var_loggedinuserid'");

stderr("Upload Successful","Your image has been uploaded successfully.");

include("includes/footer.php");
exit;

}

}
?>

the crazy thing is i get NO errors, just the thumbnail doesn't get created! weird

Graham

Inigoesdr
11-08-2007, 05:40 AM
C) You are exploding the $filename using "." and then checking if $ext matches "jpg", "jpeg", "png", and "gif" at one point in your code and checking if $ext matches '.jpg', '.jpeg', '.gif', and '.png' later in your code. Don't you suppose that might be a reason why the conditional tests are not working.

CFMaBiSmAd already told you the answer. Remove the periods in the conditionals for the thumb and it will work. Also, you're generating a random number, and not using it. Maybe you want to append that to the time?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum