Hello. Tell me please. I encountered such a problem: I need to make a small copy of the picture, but I cannot find out the file extension. Here is the compression code:

list($width, $height) = getimagesize($fileName); $new_width = 200; $new_height = 160; if(strstr($fileName, '.png') || strstr($fileName, '.PNG')) { $image = imagecreatefrompng($fileName); } else if(strstr($fileName, '.jpg') || strstr($fileName, '.JPG') || strstr($fileName, '.jpeg') || strstr($fileName, '.JPEG')) { $image = imagecreatefromjpeg($fileName); } else if(strstr($fileName, '.gif') || strstr($fileName, '.GIF')) { $image = imagecreatefromgif($fileName); } $image_p = imagecreatetruecolor($new_width, $new_height) or die('Cannot initialize new GD image stream.'); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); function addThumb($fileName) { $fileName = array_reverse(explode('.', $fileName)); $fileName[0] = 'smpgthumb.'.$fileName[0]; $fileName = implode('.', array_reverse($fileName)); return $fileName; } 

It seems that everything should work, but the $fileName variable has values ​​of the form my_photo.jpeg or my_photo.gif , etc. and etc. .... how can I compress in this case?
Thank you in advance!

  • @eicto, +1 transfer back - Gedweb

3 answers 3

 // Недавно нашёл в интернете, очень хороший класс <?php class SimpleImage { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } ?> // Вызываем класс $image = new SimpleImage(); $image->load("Сдесь сама фотка фотка"); $image->resizeToWidth(70); // В аргумент ширину картинки, которая нужна(Она пропорц. уменьш.) $image->save("Путь сохранение"); // Сохраняем 

    http://www.php.net/manual/en/function.imagecreate.php#81831 is one of the correct methods for obtaining the type of image. and the extension string can be obtained:

     $extension=pathinfo($filename)['extension']; 
    • And this script does not tell me how to remake what he squeezed? - OverLoader
     $a=end(explode(".", "$filename"));