MeXXi: Noch eine Lösung - Bildgröße proportional ändern

Beitrag lesen

Hallo,

ich habe das Problem selbst gelöst.

Gruß
anno2007

versuch doch mal folgende Klasse:

  
<?php  
class ImageResizer {  
  
var $imageFile;  
var $filetype;  
var $quality;  
var $size;  
var $newSize;  
  
 function ImageResizer() {  
  
  // constructor  
  // @param  void  
  // @return void  
  
  $this->imageFile = "";  
  $this->filetype = 0;  
  $this->quality = 70;  
  $this->size = "";  
  $this->newSize = "";  
  
  return;  
 }  
  
  
 function setImageFile($ifile = "") {  
  
  // sets the image filename  
  // @param  string $ifile  
  // @return bool   success  
  
  if(!file_exists($ifile)) {  
   die("Image doesn't exist.");  
   return false;  
  }  
  
  $size = @getimagesize($ifile);  
  if(!is_array($size)) {  
   die("Image isn't a valid image.");  
   return false;  
  }  
  
  $this->imageFile = $ifile;  
  $this->size = $size;  
  $this->filetype = $size[2];  
  return true;  
 }  
  
  
 function setJpgQuality($quality = 70) {  
  
  // sets the jpg quality percentage  
  // @param  int unsigned [0-100] $quality  
  // @return bool   success  
  
  if($quality < 0 or $quality > 100) {  
   die("Wrong JPG quality given (must be 0 &lt; x &lt; 100).");  
   return false;  
  }  
  $this->quality = $quality;  
  return true;  
 }  
  
  
 function setNewSize($params) {  
  
  // sets the new size of the image  
  // @param  $params = array(V);  
  //   int unsigned $width  
  //   int unsigned $height  
  //   int unsigned $larger  
  //   int unsigned $smaller  
  //   bool $proportional, default value true  
  //   bool $percent, default value false  
  // @return bool   success  
  
  // unpack params  
  $proportional = ( isset($params['proportional'])  
       and is_bool($params['proportional']))  
      ? $params['proportional']  
      : true;  
  
  $percent = (isset($params['percent'])  
     and is_bool($params['percent']))  
       ? $params['percent']  
       : false;  
  
  $stretchSmall = (isset($params['stretchSmall'])  
       and is_bool($params['stretchSmall']))  
      ? $params['stretchSmall']  
      : false;  
  
  $landscape = $this->size[0] > $this->size[1]  
      ? true  
      : false;  
  
  $width = isset($params['width'])  
     ? $params['width']  
     : 0;  
  
  $height = isset($params['height'])  
     ? $params['height']  
     : 0;  
  
  $larger = isset($params['larger'])  
     ? $params['larger']  
     : 0;  
  
  $smaller = isset($params['smaller'])  
     ? $params['smaller']  
     : 0;  
  
  if($landscape) {  
  
   $width = $larger;  
   $height = $smaller;  
  }  
  else {  
  
   $width = $smaller;  
   $height = $larger;  
  }  
  
  if($proportional) {  
  
    if($width > 0 and $height == 0) {  
    $height = $width * ($this->size[1] / $this->size[0]);  
   }  
   elseif($width == 0 and $height > 0) {  
    $width = $height * ($this->size[0] / $this->size[1]);  
   }  
  }  
  
  if($percent) {  
  
   $width = $this->size[0] * ($width / 100);  
   $height = $this->size[1] * ($height / 100);  
  }  
  
  if(!$stretchSmall) {  
  
   if($width > $this->size[0] or $height > $this->size[1]) {  
  
    $width = $this->size[0];  
    $height = $this->size[1];  
   }  
  }  
  
  $this->newSize = array($width, $height);  
  
  return true;  
 }  
  
  
 function createResizedImage($filename = "") {  
  
  $img1 = "";  
  
  switch($this->filetype) {  
  
   case 1:  
    $img1 = imagecreatefromgif($this->imageFile);  
    break;  
   case 2:  
    $img1 = imagecreatefromjpeg($this->imageFile);  
    break;  
   case 3:  
    $img1 = imagecreatefrompng($this->imageFile);  
    break;  
   default:  
    return false;  
  }  
  
  $img2 = imagecreate($this->newSize[0], $this->newSize[1]);  
  $randomString = substr(md5(uniqid(rand())), 0, 8);  
  $tmp = DIR_TEMP . "_" . $randomString . ".jpg";  
  
  imagejpeg($img2, $tmp, 100);  
  imagedestroy($img2);  
  $img2 = imagecreatefromjpeg($tmp);  
  unlink($tmp);  
  
  imagecopyresized($img2, $img1, 0, 0, 0, 0,  
       $this->newSize[0], $this->newSize[1],  
       $this->size[0], $this->size[1]);  
  
  
  if($filename != "") {  
  
   imagejpeg($img2, $filename, $this->quality);  
  }  
  else {  
  
   header("Content-Type:image/jpeg");  
   imagejpeg($img2, "", $this->quality);  
  }  
  imagedestroy($img1);  
  imagedestroy($img2);  
  
  return true;  
 }  
  
}  
  
  
// zu benutzen wie folgt:  
  
define("MAX_IMAGE_SIZE", 800);  
  
$ir = new ImageResizer();  
$ir->setImageFile($filename); $ir->setNewSize(array("larger"=>MAX_IMAGE_SIZE));  $ir->createResizedImage($newFilename);  
  
// durch "larger"=>MAX_IMAGE_SIZE wird die größere Seite des Bildes auf den Wert geschrumpft, die kleinere entsprechend = proportional.  
  
// auch möglich: "smaller"=>MAX_IMAGE_SIZE  
// schau dir die Funktion setNewSize() in der Klasse an, da findest du eine komplette Param-Liste.  
  
// wenn du statt createResizedImage($newFilename); schreibst:  
// createResizedImage(); also ohne Parameter wird das Bild an den Browser gesendet.  
  
?>