Jump to content

whats wrong with this code(image uploading)...


jronyagz

Recommended Posts

  private function save_image() {
extract($_POST['item']);
  if (!$_FILES) return;
 
  $MemberPicture = new MemberPicture();
 
try {
/****************** Saving the attachment. ****************************/
$this->Member = new Member();
$this->MemberPicture = new MemberPicture();

$_POST['file_name'] = str_replace(' ','_',$_FILES['attachfile']['name']);
$image_type = $_POST['type'] = $_FILES['attachfile']['type'];
$_POST['member_id'] = $_SESSION['_user'];

// STEP 2: CREATE the picture information and retrive the ID
      $this->MemberPicture->create($_POST);
$default_picture_id = $this->MemberPicture->last_id();

  $member = $this->Member->find($_SESSION['_user']);
$query = "
UPDATE
members
SET
default_picture_id = '".addslashes($default_picture_id)."'
WHERE
id =".$_SESSION['_user'];

$member->find_by_sql($query);
     
// Create user folder
$val = (ceil($_SESSION['_user']/1000));
if(!$val <= 0){
$upload_path = Config::UPLOAD_DIRECTORY_1.$_SESSION['_user'].'/';
} else {
$upload_path = Config::UPLOAD_DIRECTORY_2.$_SESSION['_user'].'/';
}

if (!is_dir($upload_path)) {
  mkdir($upload_path);
}
if (move_uploaded_file($_FILES['attachfile']['tmp_name'],$upload_path.$_POST['file_name'])) {
echo 'File uploaded succesfully';
chmod("$path",0777);
} else {
echo 'Erorr while uploading the file.';
}
/********* Creating a thumbnail base from the file uploaded  **********/

if(is_dir($upload_path)) {
$subdir_a = $upload_path.ApplicationController::PicSize_A.'/';
mkdir($subdir_a, 0700);

$subdir_b = $upload_path.ApplicationController::PicSize_B.'/';
mkdir($subdir_b, 0700);

$subdir_c = $upload_path.ApplicationController::PicSize_C.'/';
mkdir($subdir_c, 0700);


$filename = $upload_path.$_SESSION['_user'];

switch ($image_type) {
case ImageType::JPEG: case ImageType::JPG:
$img = imagecreatefromjpeg($filename); break;
case ImageType::BMP:
$img = imagecreatefromwbmp($filename); break;
case ImageType::GIF:
$img = imagecreatefromgif($filename); break;
case ImageType::PNG:
$img = imagecreatefrompng($filename); break;
default:
break;
}

$fname = $_POST['file_name'];
$imgsize_a = split('x',ApplicationController::PicSize_A );
$imgsize_b = split('x',ApplicationController::PicSize_B );
$imgsize_c = split('x',ApplicationController::PicSize_C );

$this->Member->thumbnail($img,$imgsize_a[0],$imgsize_a[1],$subdir_a.$fname);
$this->Member->thumbnail($img,$imgsize_b[0],$imgsize_b[1],$subdir_b.$fname);
$this->Member->thumbnail($img,$imgsize_c[0],$imgsize_c[1],$subdir_c.$fname);
    }
return true;

#Flash::$success_msg = 'You have succesfully attached the file.';
} catch (Exception $e) {
Flash::$error_msg = $e->getMessage();
ActiveRecordBase::transaction_rollback();
return false;
}
}


Here is the thumbnail function...

public function thumbnail($img,$nw,$nh,$filename) {
    $ow=imagesx($img);
    $oh=imagesy($img);
    $scale=$nw/$ow;
    $nh=ceil($oh*$scale);
    $newimg=imagecreatetruecolor($nw,$nh);
    imagecopyresized($newimg,$img,0,0,0,0,$nw,$nh,$ow,$oh);
    imagejpeg($newimg, $filename);
    return true;
}

It creates the image but its on a black color. im new to this so i dont have any idea on how to solve this one...any help pls..sorry for my english.tnx!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.