Hi frnds...

here i am confusing a lot..lot...from last 4 days i am struggling here...plz solve this problem aeap with patience....


This is my code for uploading images to my folder and db table...now, all images are blur at my user side...so, i need to create THUMBNAIL images for every image...plz change my code and write code for thumbmail creation..explain it breifly....

<?php
ob_start();
session_start();
include("config.inc.php");
$gcategory=mysql_real_escape_string($_POST['gcategory']);

$gname=mysql_real_escape_string($_POST['gname']);

$date=date('y-m-d');

foreach ($_FILES["attachments4"]["tmp_name"] as $key => $error) 
{
    if ($error == UPLOAD_ERR_OK) 
{

		@$tmp_name0 = $_FILES["attachments4"]["tmp_name"][0];
        @$name0 = $_FILES["attachments4"]["name"][0];
		
		@$tmp_name1 = $_FILES["attachments4"]["tmp_name"][1];
        @$name1 = $_FILES["attachments4"]["name"][1];
		@$tmp_name2 = $_FILES["attachments4"]["tmp_name"][2];
        @$name2 = $_FILES["attachments4"]["name"][2];
		@$tmp_name3 = $_FILES["attachments4"]["tmp_name"][3];
        @$name3 = $_FILES["attachments4"]["name"][3];
		@$tmp_name4 = $_FILES["attachments4"]["tmp_name"][4];
        @$name4 = $_FILES["attachments4"]["name"][4];
		
		@$tmp_name5 = $_FILES["attachments4"]["tmp_name"][5];
        @$name5 = $_FILES["attachments4"]["name"][5];
		@$tmp_name6 = $_FILES["attachments4"]["tmp_name"][6];
        @$name6 = $_FILES["attachments4"]["name"][6];
		@$tmp_name7 = $_FILES["attachments4"]["tmp_name"][7];
        @$name7 = $_FILES["attachments4"]["name"][7];
		
		@$tmp_name8 = $_FILES["attachments4"]["tmp_name"][8];
        @$name8 = $_FILES["attachments4"]["name"][8];
		@$tmp_name9 = $_FILES["attachments4"]["tmp_name"][9];
        @$name9 = $_FILES["attachments4"]["name"][9];

		$title1="../../gallery/allgallerys/$gcategory/$gname";
		
		@move_uploaded_file($tmp_name0, "$title1"."/"."$name0");
	    @move_uploaded_file($tmp_name1, "$title1"."/"."$name1");
		@move_uploaded_file($tmp_name2, "$title1"."/"."$name2");
		@move_uploaded_file($tmp_name3, "$title1"."/"."$name3");
		@move_uploaded_file($tmp_name4, "$title1"."/"."$name4");
	    @move_uploaded_file($tmp_name5, "$title1"."/"."$name5");
		@move_uploaded_file($tmp_name6, "$title1"."/"."$name6");
		@move_uploaded_file($tmp_name7, "$title1"."/"."$name7");
		@move_uploaded_file($tmp_name8, "$title1"."/"."$name8");
		@move_uploaded_file($tmp_name9, "$title1"."/"."$name9");
		}
		}
		

	
$sql="select id from gallerymain where gcategory='$gcategory' and gname='$gname'";
$res=mysql_query($sql)or die(mysql_error());
while($row=mysql_fetch_array($res)){
$gid=$row['id'];
}
$title="allgallerys/$gcategory/$gname";

if($name0!='')
{	
$path="$title"."/"."$name0";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}

if($name1!='')
{	
$path="$title"."/"."$name1";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}

if($name2!='')
{	
$path="$title"."/"."$name2";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}

if($name3!='')
{	
$path="$title"."/"."$name3";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}

if($name4!='')
{	
$path="$title"."/"."$name4";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}

if($name5!='')
{	
$path="$title"."/"."$name5";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}

if($name6!='')
{	
$path="$title"."/"."$name6";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}

if($name7!='')
{	
$path="$title"."/"."$name7";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}

if($name8!='')
{	
$path="$title"."/"."$name8";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}
if($name9!='')
{	
$path="$title"."/"."$name9";
$sql="insert into galleryphotos values('','$gid','$path','$date')";
mysql_query($sql)or die(mysql_error());
}
//header('Location : uploadgallery.php'); 
?>

Thanks in advance....

Dani AI

Generated

A short, practical supplement to the thread: the blur you see usually comes from serving full‑size photos scaled down in the browser or from low‑quality server resizing. was right to point at GD — use it, but also tighten the upload flow and preserve quality and transparency. The original upload code in ’s post unrolls every file index, suppresses errors with @, and uses repeated insert blocks; replace that with a safe loop, proper validation, and a single thumbnail routine.

Checklist (apply to each uploaded file)

  • Validate the upload error, MIME/type (finfo or getimagesize()), and size limits.
  • Generate a safe unique filename; never trust the original client name.
  • Move the original with move_uploaded_file() into a per‑gallery folder you create (check and mkdir() if needed).
  • Create a thumbnail immediately after moving the file and save it to a thumbs/ subfolder. Use a quality resampling routine (not simple nearest‑neighbor) and preserve alpha for PNG/GIF.
  • Store only the safe paths in the DB using prepared statements (PDO) instead of old mysql_* calls.

Example thumbnail routine (concise, modern GD approach):

function create_thumbnail($srcPath, $dstPath, $max = 200, $quality = 90) {
    $info = getimagesize($srcPath);
    if (!$info) return false;
    $src = imagecreatefromstring(file_get_contents($srcPath));
    $w = imagesx($src); $h = imagesy($src);
    $ratio = min($max/$w, $max/$h);
    $tw = max(1, (int)($w * $ratio));
    $th = max(1, (int)($h * $ratio));
    $thumb = imagecreatetruecolor($tw, $th);
    if ($info['mime'] === 'image/png' || $info[2] === IMAGETYPE_GIF) {
        imagesavealpha($thumb, true);
        imagealphablending($thumb, false);
        $transparent = imagecolorallocatealpha($thumb, 0,0,0,127);
        imagefill($thumb, 0,0, $transparent);
    }
    imagecopyresampled($thumb, $src, 0,0, 0,0, $tw, $th, $w, $h);
    if ($info['mime'] === 'image/png') imagepng($thumb, $dstPath);
    elseif ($info[2] === IMAGETYPE_GIF) imagegif($thumb, $dstPath);
    else imagejpeg($thumb, $dstPath, $quality);
    imagedestroy($src); imagedestroy($thumb);
    return true;
}

Notes and cautions

  • Very large originals can exhaust memory; consider ini_set('memory_limit', ...), or use ImageMagick/Imagick for heavy batches.
  • Create 1x and optional 2x thumbnails for retina displays.
  • Replace repeated DB inserts with one prepared statement inside the file loop (see PDO docs).
  • Remove all @ error suppressions; check return values and log errors.

Useful manual pages: getimagesize() and GD resampling (imagecreatetruecolor(), imagecopyresampled()) in the PHP manual for details and edge cases (getimagesize, imagecreatetruecolor, imagecopyresampled).

This function should do the trick as long as you have the PHP GD libraries.
Pass two file paths and it will copy $srcim into $destim and resize it to the size of $size.
For more details see this as that is where I got most of the code.

<?php
function makeThumbnail($srcim, $destim) {
$size = 200;
$thumb = ImageCreate(200, 200); 

$draw_from = $srcim;
$dim = GetImageSize($draw_from);
if($dim[0]>$dim[1])
{
$to_w = $size;
$to_h = round($dim[1]*($size/$dim[0]));
$to_x = 0;
$to_y = round($size-$to_h)/2;
}
else
{
$to_h = $size;
$to_w = round($dim[0]*($size/$dim[1]));
$to_y = 0;
$to_x = round($size-$to_w)/2;
}
if($dim[2]==1) {$from = ImageCreateFromGIF($draw_from);}
elseif($dim[2]==2) {$from = ImageCreateFromJPEG($draw_from);}
elseif($dim[2]==3) {$from = ImageCreateFromPNG($draw_from);} 
ImageCopyResized($thumb, $from, $to_x, $to_y, 0,  
0, $to_w, $to_h, $dim[0], $dim[1]); 
switch($dim[2]){
case 1:
ImageGIF($thumb, $destim);
break;		
case 2:
ImageJPEG($thumb, $destim);
break;
case 3:
ImagePNG($thumb, $destim);
break;
}
ImageDestroy($from);
ImageDestroy($thumb); 
return;
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.