db_smartone = $this->load->database("onedev", true); $this->load->library('ImageManipulator'); } public function upload() { $inp = $this->sys_input; $home_dir = "/home/one/project/one/"; $target_dir = $home_dir . "one-media/one-photo/patient/" . date("Y") . "/"; $y = $this->regenerateOldPhoto($home_dir, $inp['id']); // get patient mr $p = $this->db_smartone->select("M_PatientNoReg") ->where("M_PatientID", $inp['id']) ->get('m_patient') ->row(); if (!file_exists($target_dir)) { mkdir($target_dir, 0755, true); } $target_path = $target_dir . $p->M_PatientNoReg . ".jpg"; $this->base64_to_jpeg($inp['data'], $target_path); // CROP Image $im = new ImageManipulator($target_path); $w = $im->getWidth(); $h = $im->getHeight(); $mw = ceil(3 * $h / 4); if ($w <= $mw) { $x1 = 0; $y1 = 0; $x2 = $w; $y2 = $h; } else { $x1 = floor(($w - $mw) / 2); $y1 = 0; $x2 = ceil($w - (($w - $mw) / 2)); $y2 = $h; } $im->crop($x1, $y1, $x2, $y2); // takes care of out of boundary conditions automatically $im->save($target_path); $x = $this->generateThumbnail($target_path, 75, 100); // Save to DB $this->db_smartone->set("M_PatientPhoto", "/" . str_replace($home_dir, "", $target_path)) ->set("M_PatientPhotoThumb", "/" . str_replace($home_dir, "", $x)) ->set('M_PatientPhotoCounter', '`M_PatientPhotoCounter` + 1', false) ->where('M_PatientID', $inp['id']) ->update('m_patient'); // LOGGING $code = $y ? "PHOTO.PATIENT.EDIT" : "PHOTO.PATIENT.ADD"; $one_log = $this->load->database('onelog', true); $one_log->set('Log_PhotoCode', $code) ->set('Log_PhotoM_PatientID', $inp['id']) ->set('Log_PhotoUrl', $y ? $y : "/" . str_replace($home_dir, "", $target_path)) ->insert('log_photo'); $this->sys_ok(["rename"=>$y, "patient_id"=>$inp['id'], "patient_mr"=>$p->M_PatientNoReg, "photo_url"=>"http://" . $_SERVER['SERVER_NAME'] . "/" . str_replace($home_dir, "", $target_path) . "?d=" . date("YmdHis")]); } function base64_to_jpeg($base64_string, $output_file) { // open the output file for writing $ifp = fopen( $output_file, 'wb' ); // split the string on commas // $data[ 0 ] == "data:image/png;base64" // $data[ 1 ] == $data = explode( ',', $base64_string ); // we could add validation here with ensuring count( $data ) > 1 fwrite( $ifp, base64_decode( $data[ 1 ] ) ); // clean up the file resource fclose( $ifp ); return $output_file; } function generateThumbnail($img, $width, $height, $quality = 90) { if (is_file($img)) { $imagick = new Imagick(realpath($img)); $imagick->setImageFormat('jpeg'); $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); $imagick->setImageCompressionQuality($quality); $imagick->thumbnailImage($width, $height, false, false); $filename_no_ext = reset(explode('.', $img)); if (file_put_contents($filename_no_ext . '_thumb' . '.jpg', $imagick) === false) { throw new Exception("Could not put contents."); } return $filename_no_ext . '_thumb' . '.jpg'; } else { throw new Exception("No valid image provided with {$img}."); } } function regenerateOldPhoto($home_dir, $id) { $r = $this->db_smartone->select('m_patientphoto, m_patientphotocounter', false) ->where('m_patientid', $id) ->get('m_patient') ->row(); if ($r->m_patientphoto != null && $r->m_patientphotocounter > 0) { $full_path = substr_replace($home_dir ,"", -1) . $r->m_patientphoto; $path_parts = pathinfo($full_path); $rename = $path_parts['dirname'] . '/' . $path_parts['filename'] . '-' . $r->m_patientphotocounter . '.' . $path_parts['extension']; rename($full_path, $rename); // echo $path_parts['dirname'], "\n"; // echo $path_parts['extension'], "\n"; // echo $path_parts['filename'], "\n"; return "/" . str_replace($home_dir, "", $rename); } return false; } }