Просмотр поста

.
Koenig
(\/)____o_O____(\/)
Folour,
<?php
  class Kimage 
{
    private $image, $width, $height;
    
    public function __construct($image) {
        if (file_exists($image)) {
            $image = file_get_contents($image);
        } elseif (substr($image, -2) == '==' || substr($image, -1) == '=') {    // TODO: skl;djf
            $image = base64_decode($image);
        } elseif (substr($image, 0, 4) == 'http') {
            if (list(, , $type) = getimagesize($image)) {
                $upload = new SplFileObject($image, 'rb');
                $image = '';
                while (!$upload->eof()) {
                    $image .= $upload->fgets();
                }
            } else {
                return false;
            }
        } else {
            throw new Exception('Unsupported image type');
        }
        $this->image = imagecreatefromstring($image);
        $this->setSizes();
        $newimg = imagecreatetruecolor($this->width, $this->height);
        $black = imagecolorallocate($newimg, 0, 0, 0);
        imagefill($newimg, 0, 0, $black);
        imagecolortransparent($newimg, $black);
        imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
        $this->image = $newimg;                                                                      
    }
    
    public function base64($resource = null) {
        ob_start();
        imagepng($resource ? $resource : $this->image, null, 9, PNG_ALL_FILTERS);
        $temp = chunk_split(base64_encode(ob_get_contents()));
        ob_clean();

        return trim($temp);
    }
    
    public function setSizes() {
         $this->setWidth();
         $this->setHeight();
         
         return $this;
    }
    
    public function setWidth() {
        $this->width = imagesx($this->image);
        
        return $this;
    }
    
    public function setHeight() {
        $this->height = imagesy($this->image);
        
        return $this;
    }
    
}

require_once('kim.php');

$a = new Kimage('test.png');

echo '<img src="data:image/png;base64,' . $a->base64() . '" alt="base64_image" />';