Jump to content

Help with Upload with the GD library prob silly mistakes


steve490

Recommended Posts

Hey guys,

 

Heres the problem... Im trying to upload an image with my uploader (this works fine) but im having problems with the GD library trying to create thumbnails but I've never used the library before so having problems getting an output. I've tried echoing the GD code to find the problem but i get nothing returned. Can someone explain what i've done wrong and how to resolve the issue. All the folders and directories are in the right place and the images are being uploaded into the 'uploads' folder but not the 'thumbs' folder.

 

Heres all the code:-

 

 

 


$pic=($_FILES['name']);
$imagefolder='/';
$thumbsfolder='/';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
$files = directory("/","jpg,gif");
$name='/uploads';		//Original filename
$filename='/thumbs';	//Filename of the resized image
$new_w = '100'; 
$new_h = '100';


$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = '****';
mysql_select_db($dbname);

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$pic = ($_FILES['uploadedfile']['name']);

mysql_query ("INSERT INTO img (name, email) VALUES ('uploads/$pic','$_SESSION[email]')");  

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded to" . $target_path;
} else{
    echo "There was an error uploading the file, please try again!";
}

if ($pics[0]!="")
{
foreach ($pics as $p)
{
	createthumb($p,"tn_".$p,150,150);
}
}

/*
Function ditchtn($arr,$thumbname)
filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
	if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}


function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) 
{
	$thumb_w=$new_w;
	$thumb_h=$old_y*($new_h/$old_x);

}
if ($old_x < $old_y) 
{
	$thumb_w=$old_x*($new_w/$old_y);
	$thumb_h=$new_h;
}
if ($old_x == $old_y) 
{
	$thumb_w=$new_w;
	$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
if (preg_match("/png/",$system[1]))
{
	imagepng($dst_img,$filename); 
} else {
	imagejpeg($dst_img,$filename); 
}
imagedestroy($dst_img); 
imagedestroy($src_img); 
}


function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
	$filters=explode(",",$filters);
	while (($file = readdir($handle))!==false)
	{
		for ($f=0;$f<sizeof($filters);$f++):
			$system=explode(".",$file);
			if ($system[1] == $filters[$f]){$files[] = $file;}
		endfor;
	}
}
closedir($handle);
return $files;
}


 

Im a novice with php so im still trying to understand how most things work and get rid of the silly mistakes I keep making.

Thanks for the help :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.