Codice PHP:
<?php
class IMG{
//dichiaro le VARIABILI GLOBALI
var $_img; //variabile globale dell'immmagine
var $_x; //variabile globale dimensione width img
var $_y; //variabile globale dimensione height img
var $_type; //variabile globale tipo img
var $_width; //dimensione w immagine finale
var $_height; //dimensione h immagine finale
var $_versione; // versione dell'immagine orizzontale o verticale
//dichiaro il COTRUTTORE
function IMG($file=""){
if($file!=""){
$this->_x=0;
$this->_y=0;
$this->info($file);
$this->load($file);
}
}
// definisco la funzione "load"
function load($file){
if($file=="") return FALSE;
if($this->_type=="") return FALSE;
else {
switch($this->_type){
case "gif":
return FALSE; //questo perché le img GID con la GD2 non funzionano
break;
case "jpeg":
$this->_img=imagecreatefromjpeg($file);
break;
case "png":
$this->_img=imagecreatefrompng($file);
break;
case "wbmp":
$this->_img=imagecreatefromwbmp($file);
break;
default:
return FALSE;
break;
}
return TRUE;
}
}
//ritorno valore width
function getWidth(){
return $this->_x;
}
//ritorno valore height
function getHeight(){
return $this->_y;
}
//ritorno valore type
function getType(){
return $this->_type;
}
//ritorno versione type
function getVersione(){
if($this->_x > $this->_y) $this->_versione="o"; //orizzontale
else $this->_versione = "v"; //verticale
return $this->_versione;
}
//dichiarazione delle caratteristiche dell'img
function info($file){
if($file=="") return FALSE;
$info=getimagesize($file);
if($info==FALSE) return FALSE;
else {
switch($info[2]){
case 1:
$this->_type="gif";
break;
case 2:
$this->_type="jpeg";
break;
case 3:
$this->_type="png";
break;
case 6:
$this->_type="wbmp";
break;
default:
return FALSE;
break;
}
$this->_x=$info[0]; //assegno il valore a x e y
$this->_y=$info[1];
return TRUE;
}
}
/*-----> dichiarazione funzione "RESIZE"
ridimensiona l'immagine dentro una quadro con dimensioni
passate e inserisce il colore passato nello spazio rimanente
*/
function resize($width="",$height="",$r=255,$g=255,$b=255){
if($this->_x==0||$this->_y==0) return FALSE;
else {
if($this->_x>$width||$this->_y>$height){
$scale = min($width/$this->_x, $height/$this->_y);
$img_width = (int)($this->_x*$scale);
$img_height = (int)($this->_y*$scale);
$deltaw = (int)(($width - $img_width)/2);
$deltah = (int)(($height - $img_height)/2);
$newimg=imagecreatetruecolor($width,$height); //imagecreatetruecolor GD2
$color = imagecolorallocate ($this->_img,$r,$g,$b);
imagefill($newimg,0,0,$color);
$ok=imagecopyresampled($newimg,$this->_img,$deltaw,$deltah,0,0,$img_width,$img_height,$this->_x,$this->_y); //imagecopyresampled GD2
$this->_x=$width;
$this->_y=$height;
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
} else {
$deltaw = (int)(($width - $this->_x)/2);
$deltah = (int)(($height - $this->_y)/2);
$newimg=imagecreatetruecolor($width,$height); //imagecreatetruecolor GD2
$color = imagecolorallocate ($this->_img,$r,$g,$b);
imagefill($newimg,0,0,$color);
$ok=imagecopyresampled($newimg,$this->_img,$deltaw,$deltah,0,0,$this->_x,$this->_y,$this->_x,$this->_y); //imagecopyresampled GD2
$this->_x=$width;
$this->_y=$height;
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
}
}
}
/*------>dichiarazione funzione "RESIZE MAX"
ridimensiona le immagini in modo che stiano
all'interno di un'area definita
*/
function resize_max($max_w="",$max_h=""){
if($this->_x==0||$this->_y==0) return FALSE;
else {
if($this->_x>$max_w||$this->_y>$max_h){
$scale = min($max_w/$this->_x, $max_h/$this->_y);
$img_width = (int)($this->_x*$scale);
$img_height = (int)($this->_y*$scale);
$newimg=imagecreatetruecolor($img_width,$img_height); //imagecreatetruecolor GD2
$ok=imagecopyresampled($newimg,$this->_img,0,0,0,0,$img_width,$img_height,$this->_x,$this->_y); //imagecopyresampled GD2
$this->_x=$img_width;
$this->_y=$img_height;
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
} else {
$newimg=imagecreatetruecolor($this->_x,$this->_y); //imagecreatetruecolor GD2
$ok=imagecopyresampled($newimg,$this->_img,0,0,0,0,$this->_x,$this->_y,$this->_x,$this->_y); //imagecopyresampled GD2
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
}
}
}
/*-----> dichiarazione funzione "IMG CENTRALE"
prende l'area centrale dell'immagine di dimensioni definite
e la ridimensiona alle nuove dimensioni che vengono passate
GD2 nuove .... :o)
*/
function centro($width="",$height="",$por_w="",$por_h=""){
if($this->_x==0||$this->_y==0) return FALSE;
else {
$x=($this->_x-$por_w)/2;
$y=($this->_y-$por_h)/2;
$newimg=imagecreatetruecolor($width,$height);
$ok=imagecopyresampled($newimg,$this->_img,0,0,$x,$y,$width,$height,$por_w,$por_h); //imagecopyresampled GD2
$this->_x=$width;
$this->_y=$height;
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
}
}
/*-----> dichiarazione funzione "IMG CENTRALE"
prende l'area centrale dell'immagine di dimensioni definite
e la ridimensiona alle nuove dimensioni che vengono passate
GD2 nuove .... :o)
*/
function centro_proporzionato($width="",$height=""){
if($this->_x==0||$this->_y==0) return FALSE;
else {
$scale=min($this->_x/$width,$this->_y/$height);
$por_w=$width*$scale;
$por_h=$height*$scale;
$x=($this->_x-$por_w)/2;
$y=($this->_y-$por_h)/2;
$newimg=imagecreatetruecolor($width,$height);
$ok=imagecopyresampled($newimg,$this->_img,0,0,$x,$y,$width,$height,$por_w,$por_h); //imagecopyresampled GD2
$this->_x=$width;
$this->_y=$height;
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
}
}
/*-----> dichiarazione funzione "IMG CENTRALE 2"
prende l'area centrale dell'immagine di dimensioni definite
e la ridimensiona alle nuove dimensioni che vengono passate
GD2 nuove .... :o)
*/
function centro_proporzionato_2($width="",$height=""){
if($this->_x==0||$this->_y==0) return FALSE;
else {
$scale=min($this->_x/$width,$this->_y/$height);
$por_w=$width*$scale;
$por_h=$height*$scale;
$x=($this->_x-$por_w)/2;
$y=($this->_y-$por_h)/2;
$newimg=imagecreatetruecolor($width,$height);
$ok=imagecopyresampled($newimg,$this->_img,0,0,$x,$y,$width,$height,$por_w,$por_h); //imagecopyresampled GD2
$this->_x=$width;
$this->_y=$height;
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
}
}
/*-----> dichiarazione funzione "IMG CENTRALE"
prende l'area centrale dell'immagine di dimensioni definite
e la ridimensiona alle nuove dimensioni che vengono passate
GD2 vecchia .... :o(
*/
function centro2($width="",$height="",$por_w="",$por_h=""){
if($this->_x==0||$this->_y==0) return FALSE;
else {
$x=($this->_x-$por_w)/2;
$y=($this->_y-$por_h)/2;
$newimg=imagecreatetruecolor($width,$height);
$tmp_img=imagecreatetruecolor($por_w,$por_h);
imagecopy ($tmp_img, $this->_img,0,0,$x,$y,$por_w,$por_h);
$ok=imagecopyresampled ($newimg,$tmp_img,0,0,0,0,$width,$height,$por_w,$por_h);
$this->_x=$width;
$this->_y=$height;
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
}
}
/*-----> dichiarazione funzione "RESIZE STRECH"
ridimensiona alle dimensioni stabilite strecciando l'immagine (senza mantenere il rapporto originale)
*/
function resize_strech($width="",$height=""){
if($this->_x==0||$this->_y==0) return FALSE;
else {
$this->_width=$width;
$this->_height=$height;
$newimg=imagecreatetruecolor($this->_width,$this->_height);
$ok=imagecopyresampled($newimg,$this->_img,0,0,0,0,$this->_width,$this->_height,$this->_x,$this->_y);
$this->_x=$this->_width;
$this->_y=$this->_height;
if($ok==FALSE) return FALSE;
else {
$this->_img=$newimg;
return TRUE;
}
}
}
/* -----> dichiarazione funzione "ADD_IMG"
aggiunge una immagine sopra l'originale
*/
function add_img($x,$y,$d,$img,$width,$height){
if($d=="sopra"){
$pawn = imagecreatefrompng ($img);
$ok=imagecopy ($this->_img, $pawn, $x, $y, 0, 0, $width, $height);
} else if($d=="sotto"){
$pawn = imagecreatefrompng ($img);
$ok=imagecopy ($pawn, $this->_img, $x, $y, 0, 0, $width, $height);
$this->_img=$pawn;
}
//$this->_x=($this->_x>$width)?$this->_x:$width;
//$this->_y=($this->_y>$height)?$this->_y:$height;
if($ok==FALSE) return FALSE;
else {
return TRUE;
}
}
/* -----> dichiarazione funzione "SAVE"
salva l'immagine con il nome passato
*/
function save($nome,$qualita="60"){
if($this->_x==0||$this->_y==0) return FALSE;
else {
$func="image".$this->_type;
$ok=call_user_func($func,$this->_img,$nome);
if($ok!=FALSE) return FALSE;
else {
return TRUE;
}
}
}
/*---> dichiarazione funzione "DISPLAY"
stampa l'immagine sullo schermo
*/
function display($qualita="60"){
if($this->_x==0||$this->_y==0) return FALSE;
else {
header("Content-Type: image/".$this->_type."");
$func="image".$this->_type;
$ok=call_user_func($func,$this->_img,"",$qualita);
if($ok==FALSE) return FALSE;
else {
return TRUE;
}
}
}
}
?>
Dopo di che salvi il tutto nel DB.