Вход Регистрация
Файл: DARK WARS/dark_war/class/files_image.class.php
Строк: 50
<?php
class files_image {
    private 
$path;
    private 
$ext;
    private 
$width;
    private 
$height;
    private 
$is_img; // Конечный результат, картинка для сохранения
    
    
function __construct($path) {
        
$this->path = $path;
        
$pinfo = pathinfo($this->path);
        
$this->ext = strtolower($pinfo['extension']);
        
$this->setSize();
    }

    function 
getScreen() {
        switch (
$this->ext) {
            case 
'jpg':
                return @
imagecreatefromjpeg($this->path);
            case 
'jpeg':
                return @
imagecreatefromjpeg($this->path);
            case 
'gif':
                return @
imagecreatefromgif($this->path);
            case 
'png':
                return @
imagecreatefrompng($this->path);
            default:
                return 
false;
        }
    }
    
    
// Зармер изображения, ширина и высота
    
private function setSize() {
        
$this->width = imagesx($this->getScreen());
        
$this->height = imagesy($this->getScreen());
    }
    
    
// Просто сохраняем картинку
    
function saveImage($path, $quality) {
        
imagejpeg($this->is_img,$path,$quality);
    }
    
    
// Уменьшаем изображение
    
function resize($width = false, $height = false) {
        if(
is_numeric($width) && is_numeric($height) && $width > 0 && $height > 0) {
            
$newSize = $this->getSizeByFramework($width, $height);
        }
        else {
            
$newSize = array($this->width, $this->height);
        }
        
$newImage = imagecreatetruecolor($newSize[0], $newSize[1]);
        
imagecopyresampled($newImage, $this->getScreen(), 0, 0, 0, 0, $newSize[0], $newSize[1], $this->width, $this->height);
        
$this->is_img = $newImage;
    }
    
    
// Вычисляем необходимую ширену и высоту 
    
private function getSizeByFramework($width, $height) {
        if(
$this->width <= $width && $this->height <= height) { 
            return array(
$this->width, $this->height);
        }
        if(
$this->width / $width > $this->height / $height) {
            
$newSize[0] = $width;
            
$newSize[1] = round($this->height * $width / $this->width);
        }
        else {
            
$newSize[1] = $height;
            
$newSize[0] = round($this->width * $height / $this->height);
        }
        return 
$newSize;
    }

}
?>
Онлайн: 0
Реклама