Как исправить код resize?

Вот код, он работает, НО как-то он неправильно ресайзит, ресайзит по высоте скоре чем по ширине , если я гружу картинку 1000х600 то в одном случае должно бы загрузиться без изменений а вдругом случае должно быть 1000х400 но оно сделает 700х400 и тд както не правильно както в зависимости от рисунка не пому. Мог бы мне кто помочь исправить?


class ImageResize
{
private $image;
private $width;
private $height;
private $type;

function __construct( $file )
{
if ( ! file_exists( $file ) ) {
exit( 'File does not exist' );
}
if ( ! $this->setType( $file ) ) {
exit( 'File is not an image' );
}
$this->openImage( $file );
$this->setSize( );
}

private function setType( $file )
{
$pic = @getimagesize( $file );
switch( $pic['mime'] )
{
case 'image/jpeg':
$this->type = 'jpg';
return true;
case 'image/png':
$this->type = 'png';
return true;
case 'image/gif':
$this->type = 'gif';
return true;
default:
return false;
}
}

private function openImage( $file )
{
switch( $this->type )
{
case 'jpg':
$this->image = @imagecreatefromJpeg( $file );
break;
case 'png':
$this->image = @imagecreatefromPng( $file );
break;
case 'gif':
$this->image = @imagecreatefromGif( $file );
break;
default:
exit( 'File is not an image' );
}
}

private function setSize( )
{
$this->width = imageSX( $this->image );
$this->height = imageSY( $this->image );
}

function resize( $width = false, $height = false )
{
if ( is_numeric( $width ) && is_numeric( $height ) && $width > 0 && $height > 0 ) {
$newSize = $this->getSizeByFramework( $width, $height );
}
elseif ( is_numeric( $width ) && $width > 0 ) {
$newSize = $this->getSizeByWidth( $width );
}
elseif ( is_numeric( $height ) && $height > 0 ) {
$newSize = $this->getSizeByHeight( $height );
}
else {
$newSize = array( $this->width, $this->height );
}
$newImage = imagecreatetruecolor( $newSize[0], $newSize[1] );
imagecopyresampled( $newImage, $this->image, 0, 0, 0, 0, $newSize[0], $newSize[1], $this->width, $this->height );
$this->image = $newImage;
$this->setSize( );
return $this;
}

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[0] = round( $this->width * $height / $this->height );
$newSize[1] = $height;
}
return $newSize;
}

private function getSizeByWidth( $width )
{
if( $width >= $this->width ) {
return array( $this->width, $this->height );
}
$newSize[0] = $width;
$newSize[1] = round( $this->height * $width / $this->width );
return $newSize;
}

private function getSizeByHeight( $height )
{
if ( $height >= $this->height ) {
return array( $this->width, $this->height );
}
$newSize[1] = $height;
$newSize[0] = round( $this->width * $height / $this->height );
return $newSize;
}

function save( $path = '', $fileName, $quality = 100 )
{
if ( trim( $fileName ) == '' || $this->image === false ) {
return false;
}
if ( ! is_dir( $path ) ) {
return false;
}
if ( ! is_numeric( $quality ) || $quality < 0 || $quality > 100) {
$quality = 100;
}
$savePath = $path.trim( $fileName ).'.jpg';
imagejpeg( $this->image, $savePath, $quality );
return true;
}

}

// Ширина и высота эскизов изображения:
$webinar_width = '1000';
$webinar_height = '415';

// Ширина и высота больших изображения:
$meeting_width = '1000';
$meeting_height = '600';

// Качество изображений:
$webinar_quality = '70';
$meeting_quality = '70';

if ( ! isset( $_FILES['Filedata'] ) || ! is_uploaded_file( $_FILES['Filedata']['tmp_name'] ) || $_FILES['Filedata']['error'] != 0 ) {
header( "HTTP/1.1 500 Internal Server Error" );
echo "ошибка загрузки";
exit( 0 );
}
$file_name = basename($_FILES['Filedata']['name']);
//$file_name = strval( $_FILES['Filedata']['tmp_name'] + rand( 1111111111, 9999999999 ) );

ob_start( );
$img = new ImageResize( $_FILES['Filedata']['tmp_name'] );
$img->resize( $webinar_width, $webinar_height )->save( '../webinar/', $file_name, $webinar_quality );
$imagevariable_webinar = ob_get_contents( );
ob_end_clean( );

ob_start( );
$img = new ImageResize( $_FILES['Filedata']['tmp_name'] );
$img->resize( $meeting_width, $meeting_height )->save('../meeting/', $file_name, $meeting_quality );
$imagevariable_meeting = ob_get_contents( );
ob_end_clean( );

  • Вопрос задан
  • 2326 просмотров
Пригласить эксперта
Ответы на вопрос 2
saintbyte
@saintbyte
Django developer
Функция getSizeByFramework( $width, $height )

А подробнее только за $ =)
Ответ написан
Комментировать
"Вот код, он работает, НО как-то он неправильно"
а следовательно, он никуда не годится. а причина ровно одна - вы сами не понимаете свой код.

начните с того, что переименуйте класс, его методы, свойства и все прочие переменные, вдумчиво выбирая имена.
затем перечитайте код.

не поможет - учитесь дебажить.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
YCLIENTS Москва
от 200 000 до 350 000 ₽
Ведисофт Екатеринбург
от 25 000 ₽
ИТЦ Аусферр Магнитогорск
от 100 000 до 160 000 ₽