Created
August 7, 2012 17:59
-
-
Save bobmagicii/3287846 to your computer and use it in GitHub Desktop.
gd image class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// sort of psuedo, but a basic image class. | |
/* | |
$img = new image($file); | |
$img->scale(640,480)->crop(64,64,128,128); | |
*/ | |
class image { | |
public $img; | |
public function __construct($filename) { | |
$this->img = imagecreatefromjpeg($img); | |
$this->w = imagesx($this->img); | |
$this->h = imagesy($this->img); | |
} | |
public function scale($w,$h,$ratio=true) { | |
if($ratio) list($w,$h) = magic_ratio_function($this->w,$this->h,$w,$h); | |
$new = imagecreatetruecolor($w,$h); | |
imagecopyresampled($new,$this->img, 0,0, 0,0, $w,$h, $this->w,$this->h); | |
$this->rethrone($new); | |
return $this; | |
} | |
public function crop($x,$y,$w,$h) { | |
$new = imagecreatetruecolor($w,$h); | |
imagecopyresampled($new,$this->img, 0,0, $x,$y, $w,$h, $w,$h); | |
$this->rethrone($new); | |
return $this; | |
} | |
protected function rethrone($new) { | |
imagedestroy($this->img); | |
$this->img = $new; | |
$this->w = imagesx($this->img); | |
$this->h = imagesy($this->img); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment