class ImgSpin extends CI
{
public $degrees;
public $fileimg;
public $pathFile;
public function __construct()
{
$this->fileimg = '';
$this->degrees = 90;
$this->pathFile = false;
parent::__construct();
}
public function Imgbase64()
{
if ($this->fileimg === '') return false;
$path = MBAPPPATH . 'public/document/Temporarypath/';
if (!is_dir($path)) @mkdir($path);
$FileName = date("Y_mdHis", time()) . "_" . substr(time(), -6) . '.png';
$outputFile = $path . $FileName;
$ifp = fopen($outputFile, "wb");
fwrite($ifp, base64_decode($this->fileimg));
fclose($ifp);
$newImg = $this->SetImg($outputFile);
unlink($outputFile);
ob_start();
if ($this->pathFile === false) {
imagepng($newImg);
$imageString = base64_encode(ob_get_contents());
unlink($newImg);
} else {
$imageString = imagepng($newImg, $this->pathFile);
}
ob_end_clean();
return $imageString;
}
public function ImgPath()
{
if ($this->fileimg === '') return false;
$newImg = $this->SetImg($this->fileimg);
ob_start();
if ($this->pathFile === false) {
$url = $this->fileimg;
} else {
$url = $this->pathFile;
}
$imageString = imagepng($newImg, $url);
ob_end_clean();
return $imageString;
}
private function SetImg($outputFile)
{
$ename = getimagesize($outputFile);
$ename = explode('/', $ename['mime']);
$ext = $ename[1];
switch ($ext) {
case "png":
$srcImg = imagecreatefrompng($outputFile);
break;
case "jpeg":
$srcImg = imagecreatefromjpeg($outputFile);
break;
case "jpg":
$srcImg = imagecreatefromjpeg($outputFile);
break;
case "gif":
$srcImg = imagecreatefromgif($outputFile);
break;
}
$rotate = imagerotate($srcImg, $this->degrees, 0);
$srcWidth = imagesx($rotate);
$srcHeight = imagesy($rotate);
$newImg = imagecreatetruecolor($srcWidth, $srcHeight);
$alpha = imagecolorallocatealpha($newImg, 0, 0, 0, 127);
imagefill($newImg, 0, 0, $alpha);
imagecopyresampled($newImg, $rotate, 0, 0, 0, 0, $srcWidth, $srcHeight, $srcWidth, $srcHeight);
imagesavealpha($newImg, true);
return $newImg;
}
public function pngMerge($o_pic)
{
$picture = imagecreatefrompng($o_pic);
$img_w = imagesx($picture);
$img_h = imagesy($picture);
$color = imagecolorat( $picture, $img_w-1, 1);
for( $x = 0; $x < $img_w; $x++ ) {
for( $y = 0; $y < $img_h; $y++ ) {
$c = imagecolorat( $picture, $x, $y );
if($color!=$c){
$new_x[$x] = $x;
$new_y[$y] = $y;
}
}
}
list($Max_x,$Min_x) = [max($new_x),min($new_x)];
list($Max_y,$Min_y) = [max($new_y),min($new_y)];
$Min_x -= 5; $Max_x+=5; $Min_y-=5;$Max_y+=5;
$des_w = $Max_x-$Min_x;
$des_h = $Max_y-$Min_y;
$dst_ims = imagecreatetruecolor($des_w, $des_h);
$white = imagecolorallocate($dst_ims, 255, 255, 255);
imagefill($dst_ims, 0, 0, $white);
imagecopyresampled($dst_ims, $picture, 0, 0, $Min_x, 0, $des_w, $des_h, $des_w, $des_h);
ob_start();
imagepng($dst_ims);
$imageString = 'data:image/png;base64,'.base64_encode(ob_get_contents());
ob_end_clean();
imagedestroy($dst_ims);
imagedestroy($picture);
return $imageString;
}