Files
BE_IBL/application/controllers/mockup/masterdata/mddoctor/Photo.php
2026-04-15 15:24:12 +07:00

195 lines
6.5 KiB
PHP

<?php
class Photo extends MY_Controller
{
var $db_smartone;
public function index()
{
echo "Photo API";
}
public function __construct()
{
parent::__construct();
$this->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/doctor/" . date("Y") . "/";
$y = $this->regenerateOldPhoto($home_dir, $inp['id']);
// get patient mr
// $p = $this->db_smartone->select("M_DoctorCode")
// ->where("M_DoctorID", $inp['id'])
// ->get('m_doctor')
// ->row();
$sql = "SELECT M_DoctorCode FROM m_doctor WHERE M_DoctorID = {$inp['id']}";
$qry = $this->db_smartone->query($sql);
if(!$qry){
$this->sys_error_db("doctor select error", $this->db_smartone);
exit;
}
$p = $qry->row();
if (!file_exists($target_dir)) {
mkdir($target_dir, 0755, true);
}
$target_path = $target_dir . $p->M_DoctorCode . ".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_DoctorPhoto", "/" . str_replace($home_dir, "", $target_path))
// ->set("M_DoctorPhotoThumb", "/" . str_replace($home_dir, "", $x))
// ->set('M_DoctorPhotoCounter', '`M_DoctorPhotoCounter` + 1', false)
// ->where('M_DoctorID', $inp['id'])
// ->update('m_doctor');
$M_DoctorPhoto = "/" . str_replace($home_dir, "", $target_path);
$M_DoctorPhotoThumb = "/" . str_replace($home_dir, "", $x);
$M_DoctorID = $inp['id'];
$sql = "UPDATE m_doctor
SET
M_DoctorPhoto = '$M_DoctorPhoto',
M_DoctorPhotoThumb = '$M_DoctorPhotoThumb',
M_DoctorPhotoCounter = '`M_DoctorPhotoCounter` + 1',
M_DoctorPhotoLastUpdated = NOW()
WHERE M_DoctorID = $M_DoctorID";
$qry = $this->db_smartone->query($sql);
if(!$qry){
$this->sys_error_db("doctor update photo error", $this->db_smartone);
exit;
}
// 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_DoctorCode,
"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 ] == <actual base64 string>
$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_doctorphoto, m_doctorphotocounter', false)
// ->where('m_doctorid', $id)
// ->get('m_doctor')
// ->row();
// $r = $this->db_smartone->select('M_DoctorPhoto, M_DoctorPhotoCounter', false)
// ->where('M_DoctorID', $id)
// ->get('m_doctor')
// ->row();
$sql = "SELECT * FROM m_doctor WHERE M_DoctorID = $id";
$qry = $this->db_smartone->query($sql);
if(!$qry){
$this->sys_error_db("doctor select error", $this->db_smartone);
exit;
}
$r = $qry->row();
if ($r->M_DoctorPhoto != null && $r->M_DoctorPhotoCounter > 0) {
$full_path = substr_replace($home_dir ,"", -1) . $r->M_DoctorPhoto;
$path_parts = pathinfo($full_path);
$rename = $path_parts['dirname'] . '/' . $path_parts['filename'] . '-' . $r->M_DoctorPhotoCounter . '.' . $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;
}
}