/    Sign up×
Community /Pin to ProfileBookmark

Image resize not working in multiple upload script

Hey everyone, below is a script that I got online and have modified to be used for up to 5 images, instead of just 1. The script is supposed to take the uploaded file, resize it to 350px wide x (whatever heights end up), and also create a thumbnail that is 50x50px, then store into a database.
The script works great with everything except resizing the initial image to 350px. when using this by itself, the script worked great but now i cant figure out the resize for multiple images. if anyone could help me out it would be greatly appreciated.
Thanks in advance,
NickG

P.S. I’ve commented everything that needs to be looked at so you dont need to sieve through everything to find it.

[code=php]<?php
include(‘header.inc.php’);
include(‘dbinfo.inc.php’);

error_reporting(0);

$change=””;
$abc=””;
$max_img_no=5; //Maximum images to be set here

define (“MAX_SIZE”,”40000″);
function getExtension($str) {
$i = strrpos($str,”.”);
if (!$i) { return “”; }
$l = strlen($str) – $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$errors=0;

if($_SERVER[“REQUEST_METHOD”] == “POST”){

foreach($_POST as $key => $value){
$$key = $value;
//echo “Key is: ” . $key . ‘<br/>’;
//echo “Value is: ” . $value . ‘<br/>’;
}
//Error check for open fields
if((empty($PropTitle)) || (empty($PropDesc)) || (empty($PropPrice))){
$error++;
echo “There were unfilled areas, only the picture is optional, all other areas must be filled out.”;

}else{
$PropTitle = mysql_real_escape_string($PropTitle);
$PropDesc = mysql_real_escape_string($PropDesc);
$x = 1;
$image1 = $_FILES[“file”][“name”][$x];
//echo “image 1 is” . $image1;
$InsPropSQL = “INSERT into tblProperty(PropID, PropTitle, PropDesc, PropPrice, PropImage)values(”,’$PropTitle’,’$PropDesc’,’$PropPrice’,’$image1′)”;
//echo $InsPropSQL;
$rsInsPropSQL = mysql_query($InsPropSQL);

//Select last row in tblProperty after inserted, to get newest Property ID
$query = mysql_query(“SELECT * FROM tblProperty WHERE PropID = (SELECT MAX(PropID) FROM tblProperty)”);
while($row=mysql_fetch_array($query)){
$_SESSION[‘PropID’] = $row[‘PropID’];
//echo $_SESSION[‘PropID’];
}
//Loop through the files that were chosen to be uploaded, resize them, upload and insert into other DB
for($i=1; $i<=$max_img_no; $i++){
$image = $_FILES[“file”][“name”][$i];
//echo $image . ‘<br/>’;
$filename = stripslashes($_FILES[‘file’][‘name’][$i]);
$extension = getExtension($filename);
$extension = strtolower($extension);

if(($extension != “jpg”) && ($extension != “jpeg”) && ($extension != “png”) && ($extension != “gif”)){
$change='<div class=”msgdiv”>Unknown Image extension </div> ‘;
$errors=1;

}else{

$size=filesize($_FILES[‘file’][‘tmp_name’][$i]);

if ($size > MAX_SIZE*1024){

$change='<div class=”msgdiv”>You have exceeded the size limit!</div> ‘;
$errors=1;
}

if($extension==”jpg” || $extension==”jpeg” ){

$uploadedfile = $_FILES[‘file’][‘tmp_name’][$i];
$src = imagecreatefromjpeg($uploadedfile);

}else if($extension==”png”){

$uploadedfile = $_FILES[‘file’][‘tmp_name’][$i];
$src = imagecreatefrompng($uploadedfile);

}else{

$src = imagecreatefromgif($uploadedfile);
}
//echo $src;
list($width,$height)=getimagesize($uploadedfile);

//set the newwidth for the initial image as well as newwidth1 for the thumbnail image
$newwidth=350;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newwidth1=50;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

//set the directories to be uploaded to as well as the filename to be stored in DB
$filename = “../images/”. $_FILES[‘file’][‘name’][$i];
$ImageName = $_FILES[‘file’][‘name’][$i];

$filename1 = “../images/small/small”. $_FILES[‘file’][‘name’][$i];
$ImageName1 = $_FILES[‘file’][‘name’][$i];

imagejpeg($tmp,$filename,100);

imagejpeg($tmp1,$filename1,100);
//destroy the temporary image and src then loop through
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}
$uploadedfile = $_FILES[‘file’][‘tmp_name’][$i];
//if the file successfully uploaded, then insert new row into Property Images table
if(move_uploaded_file($uploadedfile, $filename)){

$InsImgSQL = “INSERT into tblImage(ImgID,ImgPropID,ImgName,ImgThumb) value(”,’$_SESSION[PropID]’,’$ImageName’,’small$ImageName’)”;
//echo $InsImgSQL;
$rsInsImgSQL = mysql_query($InsImgSQL);
}
}
}
}

//If no errors registred, print the success message
if(isset($_POST[‘Submit’]) && !$errors)
{
// mysql_query(“update {$prefix}users set img=’$big’,img_small=’$small’ where user_id=’$user'”);
echo ‘ <div class=”msgdiv”>Image Uploaded Successfully!</div>’;
}
?>[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@tirnaMay 13.2010 — Some basic Debugging 101:

If you don't have a debugger, maybe try inserting

[CODE]
echo some_variable_or_flag;
die();
[/CODE]


at various points in your code to check variable values or whatever to find the line(s) causing the problems.
Copy linkTweet thisAlerts:
@NickG21authorMay 14.2010 — there aren't errors being thrown, it's just that the image resize for the original file isn't working, there's not variables to check against it i dont think but ill make sure everything is set.
Copy linkTweet thisAlerts:
@tirnaMay 15.2010 — there aren't errors being thrown, it's just that the image resize for the original file isn't working, there's not variables to check against it i dont think but ill make sure everything is set.[/quote]

If no errors are thrown then that means you most likely have logic errors in your code if it isn't doing what you want.

Maybe have a look at some basic Debugging 101 principles at

[URL]http://www.webdeveloper.com/forum/showthread.php?p=1074647#post1074647[/URL]
Copy linkTweet thisAlerts:
@NickG21authorMay 17.2010 — alright, well thank you for the most generic, general response i've ever received on here. if i needed to know how to test i would have posted a question about testing, since i need help in the code of this then that's what id like a response to
Copy linkTweet thisAlerts:
@tirnaMay 17.2010 — If you know how to test and debug then there is no point in me going through your code as well to find the logic errors in your code since you say you can do that yourself.

Good luck ?
Copy linkTweet thisAlerts:
@criterion9May 17.2010 — Are you sure error reporting is turned on? Usually if you aren't getting any output (even with echo statements) php is hitting an error but error reporting is off.
Copy linkTweet thisAlerts:
@SrWebDeveloperMay 17.2010 — alright, well thank you for the most generic, general response i've ever received on here. if i needed to know how to test i would have posted a question about testing, since i need help in the code of this then that's what id like a response to[/quote]

Hold on there. I would have given you the same advice, anyone with experience would have. Why? Because of what you said, which I quote verbatim:

when using this by itself, the script worked great but now i cant figure out the resize for multiple images.[/QUOTE]

This tells us that when you converted your code to handle multiple images, something got fudged in the conversion process. In such situations adding in debugging code to check variable settings within the loop is a sensible approach to take. In addition, as criterion9 noted, the GD library code might be bombing and we are NOT psychics and know your php.ini settings as to error reporting. So that was excellent advice also.

I am confident if you try both, you will find the problem.

You didn't receive help on the specific code you posted because it is rather complex and we can't run it in our minds. So doing a little basic debugging considering your comment, the code as posted and common sense is within reason at this stage. This is not to say someone might not go further than this, but one step at a time is wise at this juncture.

-jim
Copy linkTweet thisAlerts:
@NickG21authorMay 18.2010 — hey everyone, i didn't mean to snap at anyone on here, i definitely appreciate everyone's comments and efforts in trying to help me in this process, it's just been a ridiculous process trying to get help, i'll continue trying to do different debugging methods, i was just too frustrated, and I lost my cool. I just got so frustrated because all i did was enclose everything in a for loop depending on the images set.

thanks again for pointing me in the right direction and tirna, again, sorry for the rude and unprofessional comment.
×

Success!

Help @NickG21 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...