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( );