Jump to content

Multiple Image upload and resize


123guy

Recommended Posts

hello. I am working on a multiple image upload and resizing system. I have found a code online that does exactly as I want!!!! I am now trying to get it to insert into my database properly, which is where I need help at. I have two tables, an items and an images table. I want the item table to only insert once, while I want the images table to insert all the files and their new name that the code creates. The first few lines are the insert code, and the after is the upload/resize code. any help on this is greatly appreciated.

$title = $_POST['title'];

$des = mysql_real_escape_string($_POST['description']);
$price = mysql_real_escape_string($_POST['price']);
$qty = mysql_real_escape_string($_POST['quantity']);
$cat2 = mysql_real_escape_string($_POST['category2']);
$cat = mysql_real_escape_string($_POST['category']);


if ($cat2 !='')
{$cat = $cat2; }

mysql_query("INSERT INTO items (id, name, price, description, quantity, category_id)
VALUES ('$id123', '$title', '$price','$des', '$qty', '$cat')");
if($cat2 !=''){
mysql_query("INSERT INTO categories (category)
VALUES ('$cat2')");

mysql_query("INSERT INTO images (item_id, image)
VALUES ('$id123', 'images/".$NewImageName."')");
 }
foreach($_FILES as $file)
{

// some information about image we need later.
$ImageName      = $file['name'];
$ImageSize      = $file['size'];
$TempSrc        = $file['tmp_name'];
$ImageType      = $file['type'];


if (is_array($ImageName))
{
   $c = count($ImageName);

   echo  '<ul>';

   for ($i=0; $i < $c; $i++)
   {
       $processImage           = true;
       $RandomNumber           = rand(0, 9999999999);  // We need same random name for both files.

       if(!isset($ImageName[$i]) || !is_uploaded_file($TempSrc[$i]))
       {
           echo '<div class="error">Error occurred while trying to process <strong>'.$ImageName[$i].'</strong>, may be file too big!</div>'; //output error
       }
       else
       {
           //Validate file + create image from uploaded file.
           switch(strtolower($ImageType[$i]))
           {
               case 'image/png':
                   $CreatedImage = imagecreatefrompng($TempSrc[$i]);
                   break;
               case 'image/gif':
                   $CreatedImage = imagecreatefromgif($TempSrc[$i]);
                   break;
               case 'image/jpeg':
               case 'image/pjpeg':
                   $CreatedImage = imagecreatefromjpeg($TempSrc[$i]);
                   break;
               default:
                   $processImage = false; //image format is not supported!
           }
           //get Image Size
           list($CurWidth,$CurHeight)=getimagesize($TempSrc[$i]);

           //Get file extension from Image name, this will be re-added after random name
           $ImageExt = substr($ImageName[$i], strrpos($ImageName[$i], '.'));
           $ImageExt = str_replace('.','',$ImageExt);

           //Construct a new image name (with random number added) for our new image.
           $NewImageName = $RandomNumber.'.'.$ImageExt;

           //Set the Destination Image path with Random Name
           $thumb_DestRandImageName    = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumb name
           $DestRandImageName          = $DestinationDirectory.$NewImageName; //Name for Big Image

           //Resize image to our Specified Size by calling resizeImage function.
           if($processImage && resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType[$i]))
           {
               //Create a square Thumbnail right after, this time we are using cropImage() function
               if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType[$i]))
                   {
                       echo 'Error Creating thumbnail';
                   }
                   /*
                   At this point we have succesfully resized and created thumbnail image
                   We can render image to user's browser or store information in the database
                   For demo, we are going to output results on browser.
                   */

                   //Get New Image Size
                   list($ResizedWidth,$ResizedHeight)=getimagesize($DestRandImageName);


                   echo '<li><table width="100%" border="0" cellpadding="4" cellspacing="0">';
                   echo '<tr>';
                   echo '<td align="center"><img src="../images/'.$ThumbPrefix.$NewImageName.'" alt="Thumbnail" height="'.$ThumbSquareSize.'" width="'.$ThumbSquareSize.'"></td>';
                   echo '</tr><tr>';
                   echo '<td align="center"><img src="../images/'.$NewImageName.'" alt="Resized Image" height="'.$ResizedHeight.'" width="'.$ResizedWidth.'"></td>';
                   echo '</tr>';
                   echo '</table></li>';
                   /*
                   // Insert info into database table!
                   mysql_query("INSERT INTO myImageTable (ImageName, ThumbName, ImgPath)
                   VALUES ($DestRandImageName, $thumb_DestRandImageName, 'uploads/')");
                   */

           }else{
               echo '<div class="error">Error occurred while trying to process <strong>'.$ImageName[$i].'</strong>! Please check if file is supported</div>'; //output error
           }

       }

   }
   echo '</ul>';
   }
}

// This function will proportionally resize image
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
   //Check Image size is not 0
   if($CurWidth <= 0 || $CurHeight <= 0)
   {
       return false;
   }

   //Construct a proportional size of new image
   $ImageScale         = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
   $NewWidth           = ceil($ImageScale*$CurWidth);
   $NewHeight          = ceil($ImageScale*$CurHeight);

   if($CurWidth < $NewWidth || $CurHeight < $NewHeight)
   {
       $NewWidth = $CurWidth;
       $NewHeight = $CurHeight;
   }
   $NewCanves  = imagecreatetruecolor($NewWidth, $NewHeight);
   // Resize Image
   if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
   {
       switch(strtolower($ImageType))
       {
           case 'image/png':
               imagepng($NewCanves,$DestFolder);
               break;
           case 'image/gif':
               imagegif($NewCanves,$DestFolder);
               break;
           case 'image/jpeg':
           case 'image/pjpeg':
               imagejpeg($NewCanves,$DestFolder,$Quality);
               break;
           default:
               return false;
       }
   if(is_resource($NewCanves)) {
     imagedestroy($NewCanves);
   }
   return true;
   }

}

//This function corps image to create exact square images, no matter what its original size!
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
   //Check Image size is not 0
   if($CurWidth <= 0 || $CurHeight <= 0)
   {
       return false;
   }

   //abeautifulsite.net has excellent article about "Cropping an Image to Make Square"
   //http://www.abeautifulsite.net/blog/2009/08/cropping-an-image-to-make-square-thumbnails-in-php/
   if($CurWidth>$CurHeight)
   {
       $y_offset = 0;
       $x_offset = ($CurWidth - $CurHeight) / 2;
       $square_size    = $CurWidth - ($x_offset * 2);
   }else{
       $x_offset = 0;
       $y_offset = ($CurHeight - $CurWidth) / 2;
       $square_size = $CurHeight - ($y_offset * 2);
   }

   $NewCanves  = imagecreatetruecolor($iSize, $iSize);
   if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
   {
       switch(strtolower($ImageType))
       {
           case 'image/png':
               imagepng($NewCanves,$DestFolder);
               break;
           case 'image/gif':
               imagegif($NewCanves,$DestFolder);
               break;
           case 'image/jpeg':
           case 'image/pjpeg':
               imagejpeg($NewCanves,$DestFolder,$Quality);
               break;
           default:
               return false;
       }
   if(is_resource($NewCanves)) {
     imagedestroy($NewCanves);
   }
   return true;

   }

}
?>

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.