Page 1 of 1

Uploading One Image - Resizing, and NOT resizing

Posted: Fri Oct 07, 2011 4:34 am
by simonmlewis
Hi

I need to upload an image to a server, and to the MySQL DB. I know how to do this.
It needs however to resize it for for a thumbnail, which works, but also to NOT resize it for the full sized version.

For some reason, my resize tool often loses the clarity of the picture, and in this case the user is uploading a pre-sized image which is fine.

This script should resize it and upload, then use the original and upload that too - both using the same filenames.

What it's doing is to upload the resized thumbnail one ONLY, but it also echos the
[text] echo "<div class='admincompletedbox'>Main Lightbox image uploaded.</div>";[/text]
Result at the end, and not the previous result text.

What am I doing wrong here?

Code: Select all

if ($update == "addgallery") 
{
  error_reporting(0);
  $change="";
  $abc="";
  define ("MAX_SIZE","400");
  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")
  {
    $image =$_FILES["photo"]["name"];
    $uploadedfile = $_FILES['photo']['tmp_name'];    
    if ($image) 
      {
      $filename = stripslashes($_FILES['photo']['name']);
      $extension = getExtension($_FILES['photo']['name']);
      $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['photo']['tmp_name']);
        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['photo']['tmp_name'];
          $src = imagecreatefromjpeg($uploadedfile);
          }
        else if($extension=="png")
          {
          $uploadedfile = $_FILES['photo']['tmp_name'];
          $src = imagecreatefrompng($uploadedfile);
          }
      else 
        {
        $src = imagecreatefromgif($uploadedfile);
        }
      echo $scr;
      list($width,$height)=getimagesize($uploadedfile);
      $newwidth1=170;
      $newheight1=($height/$width)*$newwidth1;
      $tmp1=imagecreatetruecolor($newwidth1,$newheight1);
      imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
      $pic=($_FILES['photo']['name']);
      srand(time());
      $random = (rand()%99999999);
      $newname="$random"."$pic";
      $filename1 = "images/productphotos/small/". $newname;
      imagejpeg($tmp1,$filename1,100);
      imagedestroy($src);
      imagedestroy($tmp1);
      }
    }
  }

$result = mysql_query ("SELECT photo FROM products WHERE id = '$id'");
while ($row = mysql_fetch_object($result)){
  mysql_query("UPDATE products SET photo = '$newname|$row->photo' where id = '$id'");
}
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
  echo "<div class='admincompletedbox' style='margin-top: 5px'>
		<b>Thumbnail image uploaded.</b></div>";
}

$target = $_SERVER['DOCUMENT_ROOT']."/images/productphotos/";
  $picmain=($_FILES['photo']['name']);
  $newname="$random"."$picmain";
  $target = $target . $newname;
  if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
    {
  echo "<div class='admincompletedbox'>Main Lightbox image uploaded.</div>";
    }  
}