PHP Classes
elePHPant
Icontem

File: image.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mikhail D  >  Easy Image DB Management  >  image.php  >  Download  
File: image.php
Role: Class source
Content type: text/plain
Description: The main class.
Class: Easy Image DB Management
Manage images stored in files or MySQL database
Author: By
Last change:
Date: 5 years ago
Size: 6,016 bytes
 

Contents

Class file image Download
<?php
//******************************************************************************************************
// Name: image.php
// Revision: 1.3
// Date: May 19, 2009
// Link: http://doc776.org
// Developer: Mikhail Davtian
// Description: Class - Image manipulation and presentation from database.
// Dependancy: sql.php
//******************************************************************************************************


class Image {
   
    var
$db; //mysql class, please add this after loading Image class if using fetch() or register()
   
var $table = 'pix';
    var
$fields = array('id' => 'image_id', 'blob' => 'image_blob', 'type' => 'image_type', 'size' => 'image_size', 'hash' => 'image_hash'); //database fields
   
   
var $tempdir = 'tmp/'; //temporary directory for converting image binary data
   
   
var $data = null; //file data
   
var $image = null; //GD resource
   
   
var $gd = false; //is GD currently in use and resource available
   
var $op = false; //is image data currenty opened and available
   
   
var $error = null; //error id if any created
   
   
   
function show($image_type = null) {
        if(
$this->op) {
            if(
$this->gd)
               
$this->output($image_type);
            else {
               
header('Content-Type: '.$this->data->image_type);
                echo
$this->data->image_blob;
            }
        }
    }
   
    function
fetch($id = 0) {
       
$this->data = $this->db->data($this->db->query("select * from `{$this->table}` where `image_id`='{$id}' limit 1"),2);
       
$this->op = true;
    }
   
    function
load() {
        if(
$this->op){
           
$this->image = imagecreatefromstring($this->data->image_blob);
            if(!empty(
$this->image)){
               
$this->gd = true;
                return
true;
            }
            else{
               
$this->gd = false;
               
$this->error = 5;
                return
false;
            }
        }
    }
   
    function
unload(){
        if(
$this->gd){
           
$this->image = null;
           
$this->gd = false;
        }
    }
   
    function
open($path = ''){
        if(
is_file($path)){
           
$image_type = getimagesize($path);
           
$this->data->image_type = $image_type['mime'];
           
$this->data->image_size = filesize($path);
           
$this->data->image_blob = file_get_contents($path);
           
$this->data->image_id = null;
           
$this->data->image_hash = md5_file($path);
           
$this->op = true;
        }
    }
   
    function
destroy(){
       
$this->image = null;
       
$this->data = null;
       
$this->gd = null;
       
$this->op = null;
       
$this->error = null;
    }
   
    function
register(){
        if(
$this->op){
            if(
$this->db->num($this->db->query("select null from `{$this->table}` where `image_hash`='{$this->data->image_hash}' limit 1")) == 0) {
               
$this->data->image_blob = $this->db->prot($this->data->image_blob);
               
$fields = "`{$this->fields['id']}`, `{$this->fields['blob']}`, `{$this->fields['type']}`, `{$this->fields['size']}`, `{$this->fields['hash']}`";
               
$values = "null, '{$this->data->image_blob}', '{$this->data->image_type}', '{$this->data->image_size}', '{$this->data->image_hash}'";
                if(
$this->db->query("insert into `{$this->table}` ($fields) values($values)"))
                    return
true;
                else {
                   
$this->error = 3;
                    return
false;
                }
            } else {
               
$this->error = 2;
                return
false;
            }
        } else {
           
$this->error = 1;
            return
false;
        }
    }
   
    function
convert($image_type){
        if(
$image_type != $this->data->image_type){
            if(
$this->gd){
               
$f = time().rand(0,9999);
                if(
$image_type == 'image/jpeg')
                   
imagejpeg($this->image, $this->tempdir.$f);
                elseif(
$image_type == 'image/gif')
                   
imagegif($this->image, $this->tempdir.$f);
                elseif(
$image_type == 'image/png')
                   
imagepng($this->image, $this->tempdir.$f);
                if(
is_file($this->tempdir.$f)){
                   
$this->open($this->tempdir.$f);
                   
$this->load();
                   
unlink($this->tempdir.$f);
                    return
true;
                }
                else{
                   
$this->error = 4;
                    return
false;
                }
            }
        }
    }
   
    function
save($filename, $image_type=null, $permissions=null) {
        if(
$image_type == null) $image_type = $this->data->image_type;
        if(
$image_type == $this->data->image_type && !$this->gd){
            if(
$image_type == 'image/jpeg')
               
$ext = '.jpg';
            elseif(
$image_type == 'image/gif')
               
$ext = '.gif';
            elseif(
$image_type == 'image/png')
               
$ext = '.png';
           
file_put_contents($filename.$ext, $this->data->image_blob);
        }
        else{
            if(!
$this->gd) $this->load();
            if(
$image_type == 'image/jpeg')
               
imagejpeg($this->image,$filename,100);
            elseif(
$image_type == 'image/gif')
               
imagegif($this->image,$filename);
            elseif(
$image_type == 'image/png')
               
imagepng($this->image,$filename);
        }
        if(
$permissions != null)
           
chmod($filename,$permissions);
    }
   
    function
output($image_type=null) {
        if(
$this->gd)
        {
            if(empty(
$image_type)) $image_type = $this->data->image_type;
           
header('Content-Type: '.$image_type);
            if(
$image_type == 'image/jpeg' )
               
imagejpeg($this->image);
            elseif(
$image_type == 'image/gif' )
               
imagegif($this->image);
            elseif(
$image_type == 'image/png' )
               
imagepng($this->image);
        }
    }
   
    function
getwidth() {
        if(
$this->gd)
            return
imagesx($this->image);
    }
    function
getheight() {
        if(
$this->gd)
            return
imagesy($this->image);
    }
    function
resizetoheight($height) {
        if(
$this->gd){
           
$ratio = $height / $this->getheight();
           
$width = $this->getwidth() * $ratio;
           
$this->resize($width,$height);
        }
    }
    function
resizetowidth($width) {
        if(
$this->gd){
           
$ratio = $width / $this->getwidth();
           
$height = $this->getheight() * $ratio;
           
$this->resize($width,$height);
        }
    }
    function
scale($scale) {
        if(
$this->gd){
           
$width = $this->getwidth() * $scale/100;
           
$height = $this->getheight() * $scale/100;
           
$this->resize($width,$height);
        }
    }
    function
resize($width,$height) {
        if(
$this->gd){
           
$new_image = imagecreatetruecolor($width, $height);
           
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getwidth(), $this->getheight());
           
$this->image = $new_image;
        }
    }
   
   
}

?>