{% else-1 %}
Суперский класс загрузки, конвертирования и изменения разрешения изображения.

Взят из старого скрипта masteram.us и доработан ЛИЧНО МНОЙ.

Поправил тот момент когда создается изображение с другим разрешением.

Использовать просто:

$img = img(путь к изображению);
$img -> img_down(); // загружаем всю инфу
$img -> resize(ширина, высота); // изменяем разрешение
$img -> save(загруженная картинка, расширение, качество);
$img -> print_img();

                        
<?php

class img
{
public $width;
public $height;
public $newwidth = 0;
public $newheight = 0;
public $mime;
public $file;
public $img;
public $newimg;
public $error;
function __construct($file)
{
$this->file = $file;
}

/* * * * * * * * * * * * * * * *
* Функция загрузки изображеня
* $img - путь к файлу
* * * * * * * * * * * * * * * */
public function img_down()
{
$info = getimagesize($this->file);
switch ($info['mime'])
{
case'image/jpeg': $this->img = imagecreatefromJPEG($this->file); break;
case'image/gif': $this->img = imagecreatefromGIF($this->file); break;
case'image/png': $this->img = imagecreatefromPNG($this->file); break;
default: $this->error = 'Формат не поддерживается'; break;
}
$this->width = $info[0];
$this->height = $info[1];
$this->mime = $info['mime'];
return $this;
}
/* * * * * * * * * * * * * * * *
* Функция коректной смены размера изображения
* $width - ширина изображения
* $height - высота если не указывать изображение будет менять размер с пропорциями
* * * * * * * * * * * * * * * */
public function resize($width, $height = 0)
{
if($this->error) return $this;

$twidth1 = 0;
$theight1 = 0;
$twidth2 = $this->width;
$theight2 = $this->height;

if($height == $width)
{
$this->newWidth = $width;
$this->newHeight = $height;
if($this->width > $this->height)
{
$tet = round((($this->width - $this->height)/2), 0);
$twidth1 = $tet;
$theight1 = 0;
$twidth2 = $this->height;
$theight2 = $this->height;
}
else
{
$tet = round((($this->height - $this->width)/2), 0);
$twidth1 = 0;
$theight1 = $tet;
$twidth2 = $this->width;
$theight2 = $this->width;
}
}
else
if($height && $height != $width)
{
$this->newWidth = $width;
$this->newHeight = $height;
$ratio = round(($this->width / $this->height), 3);
$new_ratio = round(($height / $width), 3);
$wtet1 = round(($width * round(($this->height / $height), 3)), 0);
$wtet2 = round((($this->width - $wtet1) / 2), 0);
$htet1 = round(($height * round(($this->width / $width), 3)), 0);
$htet2 = round((($this->height - $htet1) / 2), 0);
$twidth1 = ($new_ratio <= $ratio) ? 0 : $wtet2;
$theight1 = ($new_ratio <= $ratio) ? $htet2 : 0;
$twidth2 = ($new_ratio <= $ratio) ? $this->width : $wtet1;
$theight2 = ($new_ratio <= $ratio) ? $htet1 : $this->height;
}
else
{
$x_ratio = $width / $this->width;
$y_ratio = $width / $this->height;
if (($this->width <= $width) && ($this->height <= $width))
{
$this->newWidth = $this->width;
$this->newHeight = $this->height;
}
else if (($x_ratio * $this->height) < $width)
{
$this->newHeight = ceil($x_ratio * $this->height);
$this->newWidth = $width;
}
else
{
$this->newWidth = ceil($y_ratio * $this->width);
$this->newHeight = $width;
}
}

if($this->width == $this->newWidth && $this->height == $this->newHeight)
{
$this->newimg = $this->img;
return $this;
}

$this->newimg = imagecreatetruecolor($this->newWidth, $this->newHeight);
imagecopyresampled($this->newimg, $this->img, 0, 0, $twidth1, $theight1, $this->newWidth, $this->newHeight, $twidth2, $theight2);
return $this;
}
/* * * * * * * * * * * * * * * *
* Сохранение изображения
* $img - Зугруженая картинка
* $patch - Имя я путь куда сохранить изображение
* $type - В каком формате сохранить
* $compres - Сжатие, от 0 до 100 (для jpeg)
* * * * * * * * * * * * * * * */
public function save($patch, $type = 'jpg', $compres = 75)
{
if($this->error) return $this;
if (!$this->newimg) $this->newimg = $this->img;
if ($type == 'gif') imagegif($this->newimg, $patch);
if ($type == 'jpg' || $type == 'jpeg') imagejpeg($this->newimg, $patch, $compres);
if ($type == 'png') imagepng($this->newimg, $patch);
@chmod($patch, 0666);
return $this;
}
/* * * * * * * * * * * * * * * *
* Вывод изображения
* $type - В каком формате выводить gif, jpg или png (по умолчанию jpg)
* $compres - Сжатие, от 0 до 100 (для jpeg)
* * * * * * * * * * * * * * * */
public function print_img($type = 'jpg', $compres = 75)
{
if($this->error) return $this;
if ($type == 'gif') {
header("content-type: image/gif");
imagegif($this->newimg);
} elseif ($type == 'jpg' || $type == 'jpeg') {
header("content-type: image/jpeg");
imagejpeg($this->newimg, '', $compres);
} elseif ($type == 'png') {
header("content-type: image/png");
imagepng($this->newimg);
}else{
header("content-type: image/jpeg");
imagejpeg($this->newimg, '', $compres);
}
return $this;
}
function __destruct()
{
if(!$this->error){
ImageDestroy($this->img);
ImageDestroy($this->newimg);
}
}
}

?>
1 15 0
Без комментариев...