Here is the code, it works, BUT somehow it is incorrectly resized, resize by height rather than width, if I load a 1000x600 image, in one case it should load without changes, and suddenly it should be 1000x400 but it will do 700x400 and so on Correctly, depending on the picture, I do not remember. Could someone help me fix it?

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( ); 
  • How wrong? - OlegUP
  • @Bridun; Try to write more detailed questions. Explain what you see the problem, how to reproduce it, etc. - Nicolas Chabanovsky ♦
  • The question is not clear. What exactly is wrong? How much I remember in PHP GDLib has its image resizer - 2ray
  • I understand it is wrong that the pictures are not compressed to the desired size. Those. not due to imbalances anyway the height or width is not as expected? - Barton
  • one
    @Bridun Because the image you upload cannot be reduced to a given proportion. So you need to compress the image to the minimum size (height or width) by the maximum size of the image itself, and the other to count by proportion. For example, you have a placeholder 200x200. You upload a picture 300x500. Proportion 0.6. Squeeze the height to 200. Consider the width of 300 * 0.6 = 180. It turns out 180x200. Then place it in the center or at your convenience. Well and still, at loading it is possible to fasten the validator that proportions were observed. - Barton

0