class Kimage {
private $image, $width, $height = false;
public function __construct($file) {
$this->tmp($file);
}
public function save($filename, $quality = 9) {
imagepng($this->image, $filename, $quality, PNG_ALL_FILTERS);
}
public function tmp($image) {
if (file_exists($image)) {
$image = file_get_contents($image);
} elseif (preg_match('#[^-A-Za-z0-9+/=]|=[^=]|={3,}$#', $image)) {
$image = base64_decode($image);
} elseif (substr($image, 0, 4) == 'http') {
if (list(, , $type) = getimagesize($image)) {
if ($type) {
$upload = new SplFileObject($image, 'rb');
$image = '';
while (!$upload->eof()) {
$image .= $upload->fgets();
}
} else {
throw new Exception('Unsupported image type');
return false;
}
} else {
throw new Exception('Unsupported image type');
return false;
}
} else {
throw new Exception('Bad path to image');
return false;
}
$image = imagecreatefromstring($image);
if (!is_resource($image)) {
throw new Exception('Image create failed');
return false;
}
$this->width = imagesx($image);
$this->height = imagesy($image);
$this->image = $image;
imagesavealpha($this->image, true);
imagealphablending($this->image, false);
return $this;
}
public function mirroring() {
$newimage = $this->newImage($this->width, $this->height);
foreach(range($this->width, 0) as $range) {
imagecopy($newimage, $this->image, $this->width - $range - 1, 0, $range, 0, 1, $this->height);
}
$this->image = $newimage;
return $this;
}
public function newImage($width, $height) {
$newimg = imagecreatetruecolor($width, $height);
$transparent = imagecolorallocatealpha($newimg, 255, 255, 255, 127);
imagefill($newimg, 0, 0, $transparent);
imagealphablending($newimg, true);
imagesavealpha($newimg, true);
return $newimg;
}
public function rotate($angle = 90) {
$transparent = imagecolorallocatealpha($this->image, 0, 0, 0, 127);
$rotate = imagerotate($this->image, $angle, $transparent);
imagealphablending($rotate, true);
imagesavealpha($rotate, true);
$this->image = $rotate;
$this->setSizes();
return $this;
}
public function reHeight($height) {
$width = $this->width * ($height / $this->height);
$this->resize($width, $height);
return $this;
}
public function reWidth($width) {
$height = $this->height * ($width / $this->width);
$this->resize($width, $height);
return $this;
}
public function scale($scale) {
$width = $this->width * ($scale / 100);
$height = $this->height * ($scale / 100);
$this->resize($width, $height);
return $this;
}
public function resize($width, $height) {
$newimage = $this->newImage($width, $height);
imagecopyresampled($newimage, $this->image, 0, 0, 0, 0, $width, $height, $this->width , $this->height);
$this->image = $newimage;
$this->setSizes();
return $this;
}
public function crop($width, $height, $x, $y) {
$width = $width - $x;
$height = $height - $y;
$newimage = $this->newImage($width, $height);
imagecopyresampled($newimage, $this->image, 0, 0, $x, $y, $width, $height, $width, $height);
$this->image = $newimage;
$this->setSizes();
return $this;
}
public function setSizes() {
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
return $this;
}
public function grayscale() {
imagefilter($this->image, IMG_FILTER_GRAYSCALE);
return $this;
}
}