All pastes #387380 Raw Edit

Anonymous

public php v1 · immutable
·published 2007-03-09 09:40 UTC
rendered paste body
<?php//require( '../config.php' );class ImageResizer{  private $src;  private $dest;  private $gdImg;  private $srcData;  private $srcHeight;  private $srcWidth;  private $width;  private $height;    public function __construct ( $src, $dest, $width, $height )  {    $this->src = $src;    $this->dest = $dest;    $this->height = $height;    $this->width = $width;    $this->prepImg();  }    private function prepImg ()  {    $this->srcData = getimagesize($this->src);    switch ( $this->srcData['mime'] )    {      case 'image/png' :        $this->gdImg = imagecreatefrompng($this->src);        break;      case 'image/jpeg' :      case 'image/pjpeg' :        $this->gdImg = imagecreatefromjpeg($this->src);        break;      case 'image/gif' :        $this->gdImg = imagecreatefromgif($this->src);        break;    }    $this->srcWidth = $this->srcData[0];    $this->srcHeight = $this->srcData[1];  }    public function doMattResize ()  {        $ratio_orig = $this->srcWidth/$this->srcHeight;    $width = $this->width;    $height = $this->height;        if ($width/$height > $ratio_orig) {       $width = $height*$ratio_orig;    } else {       $height = $width/$ratio_orig;    }    $offsetY = 0;    $offsetX = 0;        if ( $width !== $this->srcWidth )    {      $offsetX = round(($this->width / 2) - ($width / 2), 0);    }    if ( $height !== $this->srcHeight )    {      $offsetY = round(($this->height / 2) - ($height / 2), 0);    }        // Resample    $image_p = imagecreatetruecolor($this->width, $this->height);    $image = $this->gdImg;    imagecopyresampled($image_p, $image, $offsetX, $offsetY, 0, 0, $width, $height, $this->srcWidth, $this->srcHeight);    // Output    imagejpeg($image_p, $this->dest, 90);    imagedestroy($image_p);    imagedestroy($this->gdImg);  }    public function doResize ()  {        $ratio_orig = $this->srcWidth/$this->srcHeight;    $width = $this->width;    $height = $this->height;        if ($width/$height > $ratio_orig) {       $width = $height * $ratio_orig;    } else {       $height = $width / $ratio_orig;    }    // Resample    $image_p = imagecreatetruecolor($width, $height);    $image = $this->gdImg;    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $this->srcWidth, $this->srcHeight);    // Output    imagejpeg($image_p, $this->dest, 90);    imagedestroy($image_p);    imagedestroy($this->gdImg);  }  }//$i = new ImageResizer( IMG_DIR . 'test.jpg', IMG_DIR . 'test.jpg', 500, 375 );//$i->doMattResize();?>