Skip to content

Instantly share code, notes, and snippets.

@bobmagicii
Created August 7, 2012 17:59
Show Gist options
  • Save bobmagicii/3287846 to your computer and use it in GitHub Desktop.
Save bobmagicii/3287846 to your computer and use it in GitHub Desktop.
gd image class
<?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