Php/GdImage
From Php-Classes
Contents |
[edit] GdImage
- with this class, you can create some image with Gd library or read image from file.
- there are some methods to make some copy-resize of images
- you can tile an image into a new image with some parameters.
- you can make thumbnails and load them from a cache.
[edit] usage
[edit] Source Code
<?php
class GdImage{
var $img;
var $size;
var $quality = 75;
var $colors;
# function __constructor($file=null){
function __construct($w, $h){
$this->createtruecolor($w, $h);
$this->colors = array();
}
function __destroy(){
$this->destroy();
}
# resample($percent)
# resample($w, $h)
function resample($w, $h=null){
if($h == null){
$percent = $w;
$w = intval($this->width() * $percent);
$h = intval($this->height() * $percent);
}
$new = new GdImage($w, $h);
# imagecopyresampled($new->img, $this->img, 0, 0, 0, 0, $w, $h, $this->width(), $this->height());
imagecopyresized($new->img, $this->img, 0, 0, 0, 0, $w, $h, $this->width(), $this->height());
$this->destroy();
$this->img = $new->img;
$this->size[0] = $w;
$this->size[1] = $h;
}
function resample_new($percent){
$w = intval($this->width() * $percent);
$h = intval($this->height() * $percent);
$obj = new GdImage();
$obj->img = $obj->createtruecolor($w, $h);
imagecopyresampled($obj->img, $this->img, 0, 0, 0, 0, $w, $h, $this->width(), $this->height());
return $obj;
}
# border is in percent of image width()
function tile($img, $count, $border=4){
$border = (int) ($this->width() * $border) / 100;
$w = (int) (($this->width() - $border*($count+1)) / $count);
$h = (int) (($this->height() - $border*($count+1)) / $count);
$img->resample($w, $h);
for($j=0 ; $j< $count ; $j++){
$y = $border + ($border+$h)*($j);
for($i=0 ; $i< $count ; $i++){
$x = $border + ($border+$w)*($i);
$this->image_copy_resample($img, $x, $y, $w, $h);
}
}
}
function image_copy_resample($img, $x, $y, $w, $h){
return imagecopyresampled($this->img, $img->image(), $x, $y, 0, 0, $w, $h, $img->width(), $img->height());
}
function image_copy($img, $x, $y, $w, $h){
# bool imagecopy ( dst_im, src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h )
return imagecopy($this->img, $img->image(), $x, $y, 0, 0, $img->width(), $img->height());
}
# IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM
function show($type = IMG_JPG, $percent=75){
$types = array(
IMG_GIF => array('image/gif', 'imagegif', null),
IMG_PNG => array('image/png', 'imagepng', null),
IMG_JPG => array('image/jpeg', 'imagejpeg', $percent)
);
if(!isset($types[$type]))
$type = IMG_JPG;
header('Content-type: ' . $types[$type][0]);
if($types[$type][2] == null){
$types[$type][1]($this->img);
} else{
$types[$type][1]($this->img, NULL, $percent);
}
}
function image(){
return $this->img;
}
function width(){
$list = $this->size();
return $list[0];
}
function height(){
$list = $this->size();
return $list[1];
}
function type(){
$list = $this->size();
return $list[2];
}
function size(){
return $this->size;
}
function createtruecolor($w, $h){
$this->img = imagecreatetruecolor($w, $h);
$this->size = array($w, $h, );
$this->fill($this->color_white());
$this->rectangle(0, 0, $w-1, $h-1); # for debug
return $this->img;
}
function destroy($img = null){
if($img == null){
$img = $this->img;
}
return imagedestroy($img);
}
# IMG_GIF | IMG_JPG | IMG_PNG | IMG_WBMP | IMG_XPM
function supported_types(){
return imagetypes();
}
function supported_types_ascii() {
$aSupportedTypes = array();
$aPossibleImageTypeBits = array(
IMG_GIF=>'GIF',
IMG_JPG=>'JPG',
IMG_PNG=>'PNG',
IMG_WBMP=>'WBMP'
);
foreach ($aPossibleImageTypeBits as $iImageTypeBits => $sImageTypeString) {
if (imagetypes() & $iImageTypeBits) {
$aSupportedTypes[] = $sImageTypeString;
}
}
return $aSupportedTypes;
}
function fill($color = null, $x=0, $y=0){
if($color == null)
$color = $this->color_white();
return imagefill($this->img, $x, $y, $color);
}
function rectangle($x1, $y1, $x2, $y2, $color=null){
if($color == null)
$color = $this->color_black();
return imagerectangle($this->img, $x1, $y1, $x2, $y2, $color);
}
function filled_rectangle($x1, $y1, $x2, $y2, $color=null){
if($color == null)
$color = $this->color_black();
return imagefilledrectangle($this->img, $x1, $y1, $x2, $y2, $color);
}
function alloc_color($r, $v, $b){
if(! isset($this->colors["$r:$v:$b"]))
$this->colors["$r:$v:$b"] = imagecolorallocate($this->img, $r, $v, $b);
return $this->colors["$r:$v:$b"];
}
function color_white(){
return $this->alloc_color(255,255,255);
}
function color_black(){
return $this->alloc_color(0,0,0);
}
}
class GdImageFile extends GdImage{
# function __constructor($file=null){
protected $filename;
function __construct($file){
if(file_exists($file)){
$this->filename = $file;
$this->size = null;
$this->load_from_file($file);
}
$this->colors = array();
}
function size(){
if($this->size == null)
$this->size = getimagesize($this->filename);
return $this->size;
}
function load_from_file($file){
if(preg_match('/\.jpg/i', $file) || preg_match('/\.jpeg/i', $file)){
$this->img = imagecreatefromjpeg($file);
return;
}
if(preg_match('/\.gif/i', $file)){
$this->img = imagecreatefromgif($file);
return;
}
if(preg_match('/\.png/i', $file)){
$this->img = imagecreatefrompng($file);
return;
}
}
}
class GdImageFileCached extends GdImageFile{
protected $cache_dir = "./cache/";
function resample($w, $h){
$md5 = md5($this->filename . "/$w:$h");
$md5file = sprintf('%s/%s.jpg', $this->cache_dir, $md5);
# echo "md5 = $md5\n"; exit(0);
if(file_exists($md5file)){
# echo "cached\n" ; exit(0);
$this->load_from_file($md5file);
return;
}
parent::resample($w, $h);
$this->save($md5file, IMG_JPG, 85);
}
function save($file, $type, $quality){
if($type == IMG_JPG){
imagejpeg($this->img, $file, $quality);
}
}
/*
function cache_purge(){
static int count=0;
$count++;
if($count %10 != 9)
return;
$files = $this->get_files($this->cache);
}
*/
function get_files($dir){
if(!is_dir($dir)){
echo "directory not found $dir\n";
return array();
}
$files = glob("$dir/*");
# $files = $this->read_dir($dir);
$files = glob("$dir/*");
$list = array();
foreach($files as $index=>$file){
if(!is_dir($file) && preg_match('/\.jpg/i', $file))
$list[] = $file;
}
return $list;
}
}
?>

