0

Hi, My script works but not the way I would like it to.

$file = $_FILES['content']['tmp_name'];

if
(!isset($file))
echo "please select an image.";
else {
$content = addslashes (file_get_contents ($_FILES['content']['tmp_name']));
$content_name = addslashes ($_FILES['content']['name']);
$content_size = getimagesize ($_FILES['content'] ['tmp_name']);
$user = $row_Recordset1['user_id'];
if (!$content_size)
echo "Thats not an image";
else {
$colname_Recordset1 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Recordset1 = $_SESSION['MM_Username'];
}
//
// define constants
  define('THUMBS_DIR','C:/folder/' );
  define('MAX_WIDTH', 200);
  define('MAX_HEIGHT', 200);
  
  // process the uploaded image
  if (is_uploaded_file($_FILES['content']['tmp_name'])) {
    $original = $_FILES['content']['tmp_name'];
    // begin by getting the details of the original
    list($width, $height, $type) = getimagesize($original);
	// calculate the scaling ratio
    if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
      $ratio = 1;
      }
    elseif ($width > $height) {
      $ratio = MAX_WIDTH/$width;
      }
    else {
      $ratio = MAX_HEIGHT/$height;
      }
	// strip the extension off the image filename
	$imagetypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
    $name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));
	
	// create an image resource for the original
	switch($type) {
      case 1:
        $source = @ imagecreatefromgif($original);
	    if (!$source) {
	      $result = 'Cannot process GIF files. Please use JPEG or PNG.';
	      }
	    break;
      case 2:
        $source = imagecreatefromjpeg($original);
	    break;
      case 3:
        $source = imagecreatefrompng($original);
	    break;
      default:
        $source = NULL;
	    $result = 'Cannot identify file type.';
      }
	// make sure the image resource is OK
	if (!$source) {
	  $result = 'Problem copying original';
	  }
	else {
	  // calculate the dimensions of the thumbnail
      $thumb_width = round($width * $ratio);
      $thumb_height = round($height * $ratio);
	  // create an image resource for the thumbnail
      $thumb = imagecreatetruecolor($thumb_width, $thumb_height);
	  // create the resized copy
	  imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
	  // save the resized copy
	  switch($type) {
        case 1:
	      if (function_exists('imagegif')) {
	        $success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
	        $thumb_name = $name.'_thb.gif';
		    }
	      else {
	        $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50);
		    $thumb_name = $name.'_thb.jpg';
		    }
	      break;
	    case 2:
	      $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
	      $thumb_name = $name.'_thb.jpg';
	      break;
	    case 3:
	      $success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
	      $thumb_name = $name.'_thb.png';
	    }
		if ($success) {
		  $result = "$thumb_name created";
		  }
		else {
		  $result = 'Problem creating thumbnail';
		  }
	  // remove the image resources from memory
	  imagedestroy($source);
      imagedestroy($thumb);
	  }
	}
// 
//include('includes/create_thumb3.inc.php');
if (!$insert = mysql_query("INSERT INTO mystuff.image (image_id, user_id, name,content)VALUES('', '$user','$content_name','$content')"))
echo (mysql_error());
}
}
?>

The image that goes into the folder is resized but the copy that is inserted into database is not. I see why it's not working. $content is the image beforre resize. The problem is that I don't know how to get it to work. Any help. I would like to eliminate the folder and put resized image into database. Thanks

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by kkjay
0

It is wise to store actual images in a database and only store the paths to the images in the database. ie have ../User_Pictures/1234.jpg. where the user pictures is a folder on the server containing the image 1234.jpg.

1

Yes you can. You can choose to have a different folder created for every user to store their images in or store them in one folder and give the images new random names.

Votes + Comments
Good response
0

Thanks kkjay, I have been going about images the wrong way. I started working with folders and I almost have it.
Thanks again.

0

Below is some code I use to upload images. You can use it as a guide. Hope it helps.

<?php
//define a maxim size for the uploaded images
define ("MAX_SIZE","500");
// note that these dimmensions are considered the maximum dimmension and are not fixed
// because we have to keep the image ratio intact or it will be deformed
//define a maximum size for the uploaded images
define ("LARGE_WIDTH","500");
define ("LARGE_HEIGHT","390");
define ("WIDTH","100"); //set here the width you want your thumbnail to be
define ("HEIGHT","100"); //set here the height you want your thumbnail to be.
// this is the function that will create the appropriately sized images from the upload 
// the resize will be done considering the width and height defined, but without deforming the image

function make_largeimage($img_name,$filename,$new_large_w,$new_large_h)
{
	//get image extension.
	$ext=getExtension($img_name);
	//creates the new image using the appropriate function from gd library
	if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
	$src_img=imagecreatefromjpeg($img_name);
	if(!strcmp("png",$ext))
	$src_img=imagecreatefrompng($img_name);
	if(!strcmp("gif",$ext))
	$src_img=imagecreatefromgif($img_name);
	//gets the dimmensions of the image
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	// next we will calculate the new dimmensions for the large image
	// the next steps will be taken:
	// 1. calculate the ratio by dividing the old dimmensions with the new ones
	// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
	// and the height will be calculated so the image ratio will not change
	// 3. otherwise we will use the height ratio for the image
	// as a result, only one of the dimmensions will be from the fixed ones
	$ratio1_large=$old_x/$new_large_w;
	$ratio2_large=$old_y/$new_large_h;
	if($ratio1_large>$ratio2_large) 
	{
		$large_w=$new_large_w;
		$large_h=$old_y/$ratio1_large;
	}else
	{
		$large_h=$new_large_h;
		$large_w=$old_x/$ratio2_large;
	}
	// we create a new image with the new dimmensions
	$dst_large_img=ImageCreateTrueColor($large_w,$large_h);
	// resize the big image to the newly created one
	imagecopyresampled($dst_large_img,$src_img,0,0,0,0,$large_w,$large_h,$old_x,$old_y);
	// output the created image to the file. Now we will have the image into the file named by $filename
	if(!strcmp("png",$ext))
		imagepng($dst_large_img,$filename);
	else
		imagejpeg($dst_large_img,$filename);
	if (!strcmp("gif",$ext))
	imagegif($$dst_large_img,$filename);
	//destroys source and destination images.
	imagedestroy($dst_large_img);
	imagedestroy($src_img);
}

function make_thumb($img_name,$filename,$new_w,$new_h)
{
	//get image extension.
	$ext=getExtension($img_name);
	//creates the new image using the appropriate function from gd library
	if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
	$src_img=imagecreatefromjpeg($img_name);
	if(!strcmp("png",$ext))
	$src_img=imagecreatefrompng($img_name);
	if(!strcmp("gif",$ext))
	$src_img=imagecreatefromgif($img_name);
	//gets the dimmensions of the image
	$old_x=imageSX($src_img);
	$old_y=imageSY($src_img);
	// next we will calculate the new dimmensions for the thumbnail image
	// the next steps will be taken:
	// 1. calculate the ratio by dividing the old dimmensions with the new ones
	// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
	// and the height will be calculated so the image ratio will not change
	// 3. otherwise we will use the height ratio for the image
	// as a result, only one of the dimmensions will be from the fixed ones
	$ratio1=$old_x/$new_w;
	$ratio2=$old_y/$new_h;
	if($ratio1>$ratio2) 
	{
		$thumb_w=$new_w;
		$thumb_h=$old_y/$ratio1;
	}else
	{
		$thumb_h=$new_h;
		$thumb_w=$old_x/$ratio2;
	}
	// we create a new image with the new dimmensions
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
	// resize the big image to the newly created one
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
	// output the created image to the file. Now we will have the thumbnail into the file named by $filename
	if(!strcmp("png",$ext))
		imagepng($dst_img,$filename);
	else
		imagejpeg($dst_img,$filename);
	if (!strcmp("gif",$ext))
	imagegif($dst_img,$filename);
	//destroys source and destination images.
	imagedestroy($dst_img);
	imagedestroy($src_img);
}
// This function reads the extension of the file.
// It is used to determine if the file is an image by checking the extension.
function getExtension($str) 
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// This variable is used as a flag. The value is initialized with 0 (meaning no error found)
//and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
$errors=0;
// if it is not empty
if ($event_image)
{
// get the original name of the file from the clients machine
$filename = stripslashes($_FILES['event_image']['name']);

// get the extension of the file in a lower case format
$extension = getExtension($filename);
$extension = strtolower($extension);
// if it is not a known extension, we will suppose it is an error, print an error message
//and will not upload the file, otherwise we continue
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$warning = ("File extension of image not allowed");
header("location:event_add.php?warning=$warning");	
$errors=1;
exit();
}
else
{
// get the size of the image in bytes
// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which the uploaded file was stored on the server
$size=getimagesize($_FILES['event_image']['tmp_name']);
$sizekb=filesize($_FILES['event_image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($sizekb > MAX_SIZE*1024)
{
$warning = ("Image has exceeded the size limit of 1MB");
header("location:event_add.php?warning=$warning");	
$errors=1;
exit();
}
$rand= rand(0, 100000);
//we will give an unique name, for example a random number
$image_name=$rand.'.'.$extension;
//the new name will be containing the full path where the image will be stored (images folder)
$consname="C:/wamp/www/NNL/Images/".$image_name; //change the image/ section to where you would like the original image to be stored
$consname2="C:/wamp/www/NNL/Images/Thumbs/".$image_name;
//change the image/thumb to where you would like to store the new created thumbnail of the image
$copied = copy($_FILES['event_image']['tmp_name'], $consname);
$copied = copy($_FILES['event_image']['tmp_name'], $consname2);
//localhost calling of images
$img_large="../Images/".$image_name; //change the image/ section to where you would like the original image to be stored
$img_thumb="../Images/Thumbs/".$image_name;
//we verify if the image has been uploaded, and print error instead
if (!$copied) {
$warning = ("Unable to upload image file");
header("location:event_add.php?warning=$warning");	
$errors=1;
exit();
}else{
// the new large image will be placed in Images/ folder
$imagelarge_name=$consname ;
// call the function that will create the thumbnail. The function will get as parameters
// the image name, the thumbnail name and the width and height desired for the thumbnail
$imagelarge=make_largeimage($consname,$imagelarge_name,LARGE_WIDTH,LARGE_HEIGHT);
// the new thumbnail image will be placed in Images/Thumbs/ folder
$thumb_name=$consname2 ;
// call the function that will create the thumbnail. The function will get as parameters
// the image name, the thumbnail name and the width and height desired for the thumbnail
$thumb=make_thumb($consname,$thumb_name,WIDTH,HEIGHT);
}
}
}
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.