...

View Full Version : Help with Functions/ Buggy Code



danielwarner
12-23-2006, 05:59 PM
I really cannot find the bug in this. The $imageid variable wont register with my function!

<?php

$dbhost = "localhost";
$dbname = "****";
$dbuser = "****";
$dbpass = "****";


//Connection handle
$db = @mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname") or die( "Unable to select database. Check your connection handle.");

session_start();

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

// $imageid = $_POST["ref"];
$imageid = "100";

$thumb_size = $_FILES['thumb']['size'];
$large_size = $_FILES['large']['size'];
$upload_size = $_FILES['upload']['size'];

function uploadImage($width,$height,$getfrom,$cat,$imageid) {

$queryorder = mysql_query("SELECT `order` FROM `images` WHERE ref ='".$imageid."' ORDER BY `order` DESC LIMIT 0,1");
$resultorder = mysql_num_rows($queryorder);if($resultorder == 0){ $neworder = 1; }
else {
$orderimgs = mysql_fetch_array($queryorder);
$oldorder = $orderimgs["order"];
$neworder = ($oldorder + 1);
}
$querysuffix = mysql_query("SELECT `suffix` FROM `images` WHERE ref ='".$imageid."' ORDER BY `suffix` DESC LIMIT 0,1");
$resultsuffix = mysql_num_rows($querysuffix);if($resultsuffix == 0){ $newsuffix = '_1'; }
else {
$suffiximgs = mysql_fetch_array($querysuffix);
$oldsuffix = $suffiximgs["suffix"];
$newsuffix = ($oldsuffix + 1);
$newsuffix = '_'.$newsuffix;
}

$image_name = "uploads/".$imageid.''.$newsuffix.".jpg";
$max_large_width = $width;
$max_large_height = $height;
$large_userfile_name = $_FILES[$getfrom]['name'];
$large_userfile_tmp = $_FILES[$getfrom]['tmp_name'];
move_uploaded_file($large_userfile_tmp,$image_name);
chmod ($image_name,octdec(644));
list($w1,$h1) = getimagesize($image_name);
if($w1 > $h1){
if($w1 > $max_large_width){
$aspect_ratio = $w1/$h1;
$new_large_width = $max_large_width;
$new_large_height = abs($new_large_width/$aspect_ratio);
$destimg=ImageCreateTrueColor($new_large_width,$new_large_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($image_name) or die('Problem In opening Source Image');
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),Imag eSY($srcimg)) or die('Problem In resizing');
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),ImageS Y($srcimg)) or die('Problem In resizing');
}
ImageJPEG($destimg,$image_name,100) or die('Problem In saving');
}
}
if($h1 > $w1){
if($h1 > $max_large_height){
$aspect_ratio = $h1/$w1;
$new_large_height = $max_large_height;
$new_large_width = abs($new_large_height/$aspect_ratio);
$destimg=ImageCreateTrueColor($new_large_width,$new_large_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($image_name) or die('Problem In opening Source Image');
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),Imag eSY($srcimg)) or die('Problem In resizing');
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),ImageS Y($srcimg)) or die('Problem In resizing');
}
ImageJPEG($destimg,$image_name,100) or die('Problem In saving');
}
}
mysql_query("INSERT INTO `images` (`ref`,`order`,`suffix`,`cat`)VALUES('$imageid','$neworder','$newsuffix','$cat')");
imagedestroy($destimg);

echo '<img src="http://www.caravansandtrailertents.co.uk/'.$image_name.'"><br/>'.$image_name.' '.$imageid.'<br/>';

}

//Large Images
uploadImage('560','400','large','1',$imageid);
uploadImage('70','55','large','2',$imageid);
//Thumbnail Images
uploadImage('150','120','thumb','3',$imageid);
uploadImage('70','55','thumb','4',$imageid);

}

else {

echo '


<form action="/imageupload.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Thumbnail:</td>
<td style="text-align:left"><input name="thumb" type="file" style="width:260px" value="" /></td>
</tr>
<tr>
<td>Large Image:</td>
<td style="text-align:left"><input name="large" type="file" style="width:260px" value="" /></td>
</tr>
</table>
<input type="submit" value="resize" name="upload">


';

}

?>

whizard
12-23-2006, 06:52 PM
Try this:



$queryorder = mysql_query("SELECT `order` FROM `images` WHERE ref ='".$imageid."' ORDER BY `order` DESC LIMIT 0,1");


and



$querysuffix = mysql_query("SELECT `suffix` FROM `images` WHERE ref ='".$imageid."' ORDER BY `suffix` DESC LIMIT 0,1");


Note the ". and ." around $imageid

Dan

Dan

meth
12-23-2006, 07:15 PM
Your problem lies with variable scope (http://devzone.zend.com/manual/view/page/language.variables.scope.html). It could also lie with $imageid being delineated with apostrophies. If the db table column datatype is numeric, then you need to pass $imageid as a numeric value, not a string.

danielwarner
12-24-2006, 05:24 PM
I edited my code but it still doesnt work. the script tries to echo the variable and wont display anything...im sure this has something to do with the way it is passed into the function. Is the layout of my function correct or is there a reason it isnt being passed?

koyama
12-24-2006, 09:06 PM
Could you please tell us what error message you get (or exactly what output you get). Are error messages on? It should be possible to debug your script by e.g. adding some 'var_dump' statements selected places to see where things go wrong. (Much easier than looking at the entire script without running it).

Are you sure $imageid isn't passed to the function??? Find out by putting a var_dump($imageid); in the first line of the function to display its value.

danielwarner
12-24-2006, 09:39 PM
This is the url with the code on it. >>http://www.caravansandtrailertents.co.uk/trialupload.php<< no error message is displayed and i do attempt to echo the variable name and image name. The name should come out to be uploads/100_1.jpg etc etc... but at the moment its just the ..._1 part of the file name that comes out. (which is a problem because the name links to the images id).

The var_dump hasnt shown anything.

I have read the tutorial on Variable scope, which i atchually found very interesting although irrelevent and i did atchually learn something from it which i didnt already know. I do believe i am passing the ID number into the function though (although this is the 3/ 4th time i have ever used a function before so it may be wrong!)

Thanks for showing interest!!

:D:D:D

koyama
12-25-2006, 12:51 AM
While I see the error when running your live version of the script, I tried to run your script on my own server, setting up a single table in the database too. But I couldn't reproduce the error. I got uploads/100_1.jpg. So I really have no clue what the problem could be?


The var_dump hasnt shown anything.

Can you then verify that you get dumped the value NULL? I believe that the var_dump should always outputs something... Did you try:


function uploadImage($width,$height,$getfrom,$cat,$imageid) {
var_dump($imageid);
// the rest of the function
}

This should definitely tell us whether $imageid is passed to the function or not.

danielwarner
12-25-2006, 02:31 PM
yup its in there...didnt show me anything though

Can you show me a dump of the table you used please....its a long shot but that may have something to do with it i guess...

CFMaBiSmAd
12-25-2006, 05:15 PM
Adding the following line after your first opening <?php tag might give more information about what is going on (you might also need to turn on display_errors to receive any output) -

error_reporting(E_ALL);

marek_mar
12-25-2006, 11:00 PM
The var_dump hasnt shown anything.
var_dump() if actually called, always prints something.

danielwarner
12-25-2006, 11:49 PM
Ok thanks for the help everyone i have got the ID number to display now somehow? My Problem is now that the images that i try to resize all come out at a fixed size and with the same suffix although they should increment.

The more uptodate code is posted below.

Thanks for everyones help.


<?php

$dbhost = "localhost";
$dbname = "****";
$dbuser = "****";
$dbpass = "****";


//Connection handle
$db = @mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname") or die( "Unable to select database. Check your connection handle.");

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

$imageid = $_POST["ref"];

$thumb_size = $_FILES['thumb']['size'];
$large_size = $_FILES['large']['size'];
$upload_size = $_FILES['upload']['size'];

function uploadImage($width,$height,$getfrom,$cat,$imageid) {

$queryorder = mysql_query("SELECT `order` FROM `images` WHERE ref ='".$imageid."' ORDER BY `order` DESC LIMIT 0,1");
$resultorder = mysql_num_rows($queryorder);if($resultorder == 0){ $neworder = 1; }
else {
$orderimgs = mysql_fetch_array($queryorder);
$oldorder = $orderimgs["order"];
$neworder = ($oldorder + 1);
}
$querysuffix = mysql_query("SELECT `suffix` FROM `images` WHERE ref ='".$imageid."' ORDER BY `suffix` DESC LIMIT 0,1");
$resultsuffix = mysql_num_rows($querysuffix);if($resultsuffix == 0){ $newsuffix = '_1'; }
else {
$suffiximgs = mysql_fetch_array($querysuffix);
$oldsuffix = $suffiximgs["suffix"];
$newsuffix = ($oldsuffix + 1);
$newsuffix = '_'.$newsuffix;
}

$image_name = "uploads/".$imageid.''.$newsuffix.".jpg";
$max_large_width = $width;
$max_large_height = $height;
$large_userfile_name = $_FILES[$getfrom]['name'];
$large_userfile_tmp = $_FILES[$getfrom]['tmp_name'];
move_uploaded_file($large_userfile_tmp,$image_name);
chmod ($image_name,octdec(644));
list($w1,$h1) = getimagesize($image_name);
if($w1 > $h1){
if($w1 > $max_large_width){
$aspect_ratio = $w1/$h1;
$new_large_width = $max_large_width;
$new_large_height = abs($new_large_width/$aspect_ratio);
$destimg=ImageCreateTrueColor($new_large_width,$new_large_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($image_name) or die('Problem In opening Source Image');
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),Imag eSY($srcimg)) or die('Problem In resizing');
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),ImageS Y($srcimg)) or die('Problem In resizing');
}
ImageJPEG($destimg,$image_name,100) or die('Problem In saving');
}
}
if($h1 > $w1){
if($h1 > $max_large_height){
$aspect_ratio = $h1/$w1;
$new_large_height = $max_large_height;
$new_large_width = abs($new_large_height/$aspect_ratio);
$destimg=ImageCreateTrueColor($new_large_width,$new_large_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($image_name) or die('Problem In opening Source Image');
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),Imag eSY($srcimg)) or die('Problem In resizing');
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),ImageS Y($srcimg)) or die('Problem In resizing');
}
ImageJPEG($destimg,$image_name,100) or die('Problem In saving');
}
}
mysql_query("INSERT INTO `images` (`ref`,`order`,`suffix`,`cat`)VALUES('$imageid','$neworder','$newsuffix','$cat')");
imagedestroy($destimg);

echo '<img src="http://www.caravansandtrailertents.co.uk/'.$image_name.'"><br/>'.$image_name.' '.$imageid.'<br/>';

}

//Large Images
if($large_size > 0) {
uploadImage('560','400','large','1',$imageid);
uploadImage('70','55','large','2',$imageid);
}
//Thumbnail Images
if($thumb_size > 0) {
uploadImage('150','120','thumb','3',$imageid);
uploadImage('70','55','thumb','4',$imageid);
}

}

else {

echo '


<form action="/imageupload.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Thumbnail:</td>
<td style="text-align:left"><input name="thumb" type="file" style="width:260px" value="" /></td>
</tr>
<tr>
<td>Large Image:</td>
<td style="text-align:left"><input name="large" type="file" style="width:260px" value="" /></td>
</tr>
</table>
<input type="hidden" value="100" name="ref"/>
<input type="submit" value="resize" name="upload"/>


';

}

?>

danielwarner
12-26-2006, 12:23 AM
* Picks up computer and throws it out the window *

Im really sorry for wasting your time. The problem was that the file i was trying to make work was called trialupload.php. This is a cut and paste job of a larger form which made it easier to refresh the image upload system and avoid the sequrity of the larger form. The larger form was called imageupload.php and if you look in the code i posted called trialupload.php the form still pointed to the old file called imageupload.php. This means all the work i was trying to do to fix it wasnt athchually being tested but was sitting in a different file.

I think i can fix it on my own now its just that before i was very unframiliar with the irregular result php was showing me!

Im very sorry for wasting your time and im very greatful that you spent the time to help me fix my script!

marek_mar
12-26-2006, 01:35 AM
I think i can fix it on my own now its just that before i was very unframiliar with the irregular result php was showing me!
Won't it be difficult without the computer?

danielwarner
12-26-2006, 02:22 AM
* Picks up computer and plugs it back in *


...sorry me again.

Got abit carried away before when i got it to work and now i have got another bug although this one i cant understand at all.

What happened is i managed to get the images to resize, upload to my server, upload to my database, rename them all, fixed a few more bugs along the way untill i finally finished and it worked perfectly. Then it struck me. Because i have 2 of the same image sometimes that are just sized differently i needed a database column to show a link between the two image files. I made this and it worked, but then the images decided not to resize themselves to their correct defined size and all come out small. Not only this i have found that although the images load to the database correctly the suffix seems to be fixed at _10. I dont know whats causing this and i suspect this is the reason all 5 images havnt been uploaded to my server but only 1 of them has (being 72_10.jpg)

Test the code for yourself >>http://www.caravansandtrailertents.co.uk/trialupload.php<<

Thanks for the help



This is the new code
<?php
error_reporting(E_ALL);

$dbhost = "72.41.255.202";
$dbname = "danielw_catt";
$dbuser = "danielw_admin";
$dbpass = "jomooss9115";


//Connection handle
$db = @mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname") or die( "Unable to select database. Check your connection handle.");

session_start();

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

$imageid = $_POST["ref"];

$thumb_size = $_FILES['thumb']['size'];
$large_size = $_FILES['large']['size'];
$upload_size = $_FILES['upload']['size'];

function uploadImage($getfrom,$cat,$enabled,$imageid) {

$queryorder = mysql_query("SELECT `order` FROM `images` WHERE ref ='$imageid' ORDER BY `order` DESC LIMIT 0,1");
$resultorder = mysql_num_rows($queryorder);if($resultorder == 0){ $neworder = 1; }
else {
$orderimgs = mysql_fetch_array($queryorder);
$oldorder = $orderimgs["order"];
$neworder = ($oldorder + 1);
}
$querysuffix = mysql_query("SELECT `suffix` FROM `images` WHERE ref ='$imageid' ORDER BY `suffix` DESC LIMIT 0,1");
$resultsuffix = mysql_num_rows($querysuffix);
if($resultsuffix <= 1){
$suffiximgs = mysql_fetch_array($querysuffix);
$oldsuffix = $suffiximgs["suffix"];
$newoldsuffix = str_replace("_","",$oldsuffix);
$newsuffix = ($newoldsuffix + 1);
$newsuffix = '_'.$newsuffix;
}else{$newsuffix = '_1';}
$querylink = mysql_query("SELECT `link` FROM `images` WHERE ref ='$imageid' ORDER BY `link` DESC LIMIT 0,1");
$resultlink = mysql_num_rows($querylink);
if($resultlink <= 1){
$linkimgs = mysql_fetch_array($querylink);
$oldlink = $linkimgs["link"];
$newlink = ($oldlink + 1);
}else{$newlink = '1';}
if($cat > 1){ $newlink = ($newlink - 1); }

if($cat == 1){ //Large Image
$max_large_width = "560";
$max_large_height = "400";
}
if($cat == 2){ //Thumbnail Image
$max_large_width = "150";
$max_large_height = "120";
}
if($cat == 3){ //Small Image
$max_large_width = "70";
$max_large_height = "55";
}

if($cat > 1){
$image_name = "uploads/".$imageid.''.$oldsuffix.".jpg";
$image_name2 = "uploads/".$imageid.''.$newsuffix.".jpg";
}
if($cat == 1){
$image_name = "uploads/".$imageid.''.$newsuffix.".jpg";
$image_name2 = $image_name;
$large_userfile_name = $_FILES[$getfrom]['name'];
$large_userfile_tmp = $_FILES[$getfrom]['tmp_name'];
move_uploaded_file($large_userfile_tmp,$image_name);
chmod ($image_name,octdec(644));
}
list($w1,$h1) = getimagesize($image_name);
if($w1 > $h1){
if($w1 > $max_large_width){
$aspect_ratio = $w1/$h1;
$new_large_width = $max_large_width;
$new_large_height = abs($new_large_width/$aspect_ratio);
$destimg=ImageCreateTrueColor($new_large_width,$new_large_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($image_name) or die('Problem In opening Source Image');
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),Imag eSY($srcimg)) or die('Problem In resizing');
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),ImageS Y($srcimg)) or die('Problem In resizing');
}
ImageJPEG($destimg,$image_name2,100) or die('Problem In saving');
}
}
if($h1 > $w1){
if($h1 > $max_large_height){
$aspect_ratio = $h1/$w1;
$new_large_height = $max_large_height;
$new_large_width = abs($new_large_height/$aspect_ratio);
$destimg=ImageCreateTrueColor($new_large_width,$new_large_height) or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($image_name) or die('Problem In opening Source Image');
if(function_exists('imagecopyresampled'))
{
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),Imag eSY($srcimg)) or die('Problem In resizing');
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_large_width,$new_large_height,ImageSX($srcimg),ImageS Y($srcimg)) or die('Problem In resizing');
}
ImageJPEG($destimg,$image_name2,100) or die('Problem In saving');
}
}
mysql_query("INSERT INTO `images` (`ref`,`order`,`suffix`,`cat`,`enabled`,`link`)VALUES('$imageid','$neworder','$newsuffix','$cat','$e nabled','$newlink')");
imagedestroy($destimg);

echo '<img src="http://www.caravansandtrailertents.co.uk/'.$image_name2.'"><br/>'.$image_name2.'<br/>';

}

if($large_size > 0){
uploadImage('large','1','1',$imageid);
}
if($thumb_size > 0){
uploadImage('thumb','1','0',$imageid);
uploadImage('thumb','2','1',$imageid);
}
if($upload_size > 0){
uploadImage('upload','1','1',$imageid);
uploadImage('upload','3','1',$imageid);
}

}

else {

echo '


<form action="/trialupload.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Thumbnail:</td>
<td style="text-align:left"><input name="thumb" type="file" style="width:260px" value="" /></td>
</tr>
<tr>
<td>Large Image:</td>
<td style="text-align:left"><input name="large" type="file" style="width:260px" value="" /></td>
</tr>
<tr>
<td>Upload Image:</td>
<td style="text-align:left"><input name="upload" type="file" style="width:260px" value="" /></td>
</tr>
</table>
<input type="hidden" name="ref" value="72"/>
<input type="submit" value="resize" name="init"/>


';

}

?>

Infact, after running a few tests, the images all resize to the smallest (or maybe just last) image size that has been defined. Because the Suffix isnt changing this may be the reason why all the images are being shown as smaller. as the page is attempting to load the last image made 5 times when being echo'd.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum