Public feed #601445

Untitled

public php v1 · immutable
Raw Edit
·published 2007-07-03 14:44 UTC
rendered paste body
class Image {	private $img = null;	private $thumb = null;	var $height = 0;	var $width = 0;		function __construct($file) {		if (! is_readable($file) ) throw new Exception('Nie mogę odczytać pliku '.$file);				if ( eregi('\.jpg', $file) ) $this->img = imagecreatefromjpeg($file);		elseif ( eregi('\.png', $file) ) $this->img = imagecreatefrompng($file);		elseif ( eregi('\.gif', $file) ) $this->img = imagecreatefromgif($file);		else throw new Exception("Nieznany format pliku $file");				$size = getimagesize($file);		$this->width = $size[0];		$this->height = $size[1];	}		function resize ($nwidth, $nheight = 0) {		if (! $nwidth && !$nheight) exit;		if ($nwidth == 0)			$nwidth = round($this->width * ($nheight / $this->height));		elseif ($nheight == 0)			$nheight = round($this->height * ($nwidth / $this->width));					$nimg = imagecreatetruecolor($nwidth, $nheight);				if (! imagecopyresampled($nimg, $this->img, 0, 0, 0, 0, $nwidth, $nheight, $this->width, $this->height) )			return false;				unset($this->img);		$this->img = $nimg;				return true;	}		function makethumb($width, $height) {		if (! $width && !$height) exit;				$cwidth = round($this->width * ($height / $this->height));		$cheight = round($this->height * ($width / $this->width));				$hmargin = ($width == 0) || ($cwidth < $width) ? 0 : ($width - $cwidth) / 2;		$vmargin = ($height == 0) || ($cheight < $height) ? 0 : ($height - $cheight) / 2;				if (! $height ) $height = $cheight;		if (! $width ) $width = $cwidth;				$nimg = imagecreatetruecolor($width, $height);				echo "h: $hmargin, v: $vmargin\n[{$this->width}, {$this->height}]";		echo "$width, $height";				if (! imagecopyresampled($nimg, $this->img, $hmargin, $vmargin, 0, 0, $vmargin ? $width : $cwidth, $hmargin ? $height : $cheight, $this->width, $this->height) )			return false;				unset($this->thumb);		$this->thumb = $nimg;		$this->width = $width;		$this->height = $height;				return true;	}		function saveas($file, $thumbfile = '') {// 		if (! is_writable($file) ) throw new Exception('Nie można było utworzyć pliku '.$file);				if ( eregi('\.jpg', $file) ) {			imagejpeg($this->img, $file, 97);			if ($this->thumb && $thumbfile) imagejpeg($this->thumb, $thumbfile, 97);		}		elseif ( eregi('\.png', $file) ) {			imagepng($this->img, $file);			if ($this->thumb && $thumbfile) imagepng($this->thumb, $thumbfile);		}		elseif ( eregi('\.gif', $file) ) {			imagegif($this->img, $file);			if ($this->thumb && $thumbfile) imagegif($this->thumb, $thumbfile);		}		else throw new Exception("Nieznany format pliku $file");			return true;	}}