<?php
class Imaset
{
var $error; // array que contiene los errores ocurridos durante la ejecucion
var $content; //la direccion hasta wp-content
var $dir; // directorio del plugin
var $ims; // imagenes de un directorio
var $num; //Cantidad de imagenes
var $filtros; //Filtros disponibles
function Imaset($dir='')
{
$this->dir = str_replace('imasetClass.php','',__FILE__);
$this->content = substr(__FILE__,0,strpos(__FILE__,'plugins'));
$this->lee_dir($this->content.$dir);
if(!empty($this->ims))
{
krsort($this->ims); //ordeno inversamente por claves, para mostrarlas en orden de modificacion, las mas nuevas primero
foreach($this->ims as $i) //coloco indices ordenados
$ims[] = $i;
$this->ims = $ims;
}
$this->num = count($this->ims);
//Filtros disponibles
$this->filtros = array();
$this->filtros[] = array('nom'=>'sombra','alt'=>'Crea una sombra alrededor');
$this->filtros[] = array('nom'=>'marcus','alt'=>'Crea un marco agradable');
$this->filtros[] = array('nom'=>'reflekt','alt'=>'Crea un efecto de reflejo');
$this->filtros[] = array('nom'=>'brillo','alt'=>'Dibuja un brillo curvo');
$this->filtros[] = array('nom'=>'perspectiva','alt'=>'Aplica un efecto de perspectiva');
$this->filtros[] = array('nom'=>'wave','alt'=>'Ondula la imagen');
$this->filtros[] = array('nom'=>'marca','alt'=>'Aplica una marca de agua');
}
function ddir($dir) //por cuestion de estetica y de espacio, se considera raiz al directorio desde donde testea
{
return str_replace($this->content,'',$dir);
}
function lee_dir($dir) //lee todo un directorio: todos sus archivos y los directorios hijos
{
if(substr($dir,-1)!='/') $dir .= '/'; //importante que acabe en "/"
if (is_dir($dir))
{
if ($gd = @opendir($dir))
{
while (($ar = @readdir($gd)) !== false)
{
if($ar != '.' AND $ar != '..') //Evito el propio directorio y el directorio padre
{
if(is_dir($dir . $ar))
{
if(!eregi('(plugins|languages|themes)',$ar))
$this->lee_dir($dir.$ar);
}
else
{
if(eregi('\.(jpg|gif|png)',$ar) AND !eregi('\.(thumbnail|miniatura|old)',$ar))
{
$tm = filemtime($dir.$ar);
if(empty($tm)) $tm = 0;
$this->ims[$tm] = $this->ddir($dir).$ar;
}
}
}
}
closedir($gd);
}
else
$this->error[] = 'Imposible abrir el directorio '.$dir ;
}
else
$this->error[] = $dir.' no es un directorio.' ;
}
function dir_marcas()
{
$gd = @opendir($this->dir.'img/marcas/');
while (($ar = @readdir($gd)) !== false)
{
if(eregi('\.(jpg|gif|png)',$ar))
$mar[] = $ar;
}
return $mar;
}
//Funciones para manipular en String Query
function getQuery() //obtiene el querystring en un array asociativo
{
$qs = $_SERVER['QUERY_STRING'];
$aqs = explode('&',$qs);
foreach($aqs as $q)
{ $q = explode('=',$q); $cla=$q[0]; $vqs[$cla] = $q[1]; }
return $vqs;
}
function implodeQuery($q)
{
foreach($q as $c=>$v)
$nqs[] = "$c=$v";
$nqs = implode('&',$nqs);
return $nqs;
}
function addToQuery($ar) //añade la variable $c[lave] =>$v[alor] al query_string.
{
$vqs = $this->getQuery();
foreach($ar as $c=>$v)
{
if(!$v) unset($vqs[$c]);
else $vqs[$c] = $v;
}
return $this->implodeQuery($vqs);
}
function unsetQuery($unset) //quita variables del querystring
{
$vqs = $this->getQuery();
foreach($unset as $u)
{ if($u) unset($vqs[$u]);}
return $this->implodeQuery($vqs);
}
//Funciones para poner y quitar extension .old
function ponPre($f)
{
return ereg_replace( "\.(jpg|gif|png)", ".old.\\1", $f );
}
function quitaPre($f)
{
return str_replace('.old','',$f);
}
// Funciones de retoque
function nuevaImagen($img)
{
$im = false;
switch(true)
{
case ereg('\.jpg',$img):
$im = imagecreatefromjpeg($img);
break;
case ereg('\.gif',$img):
$im = imagecreatefromgif($img);
break;
case ereg('\.png',$img):
$im = imagecreatefrompng($img);
break;
}
return $im;
}
function creaImagen($im,$img)
{
$t = false;
switch(true)
{
case ereg('\.jpg',$img):
$t = imagejpeg($im,$img);
break;
case ereg('\.gif',$img):
$t = imagegif($im,$img);
break;
case ereg('\.png',$img):
$t = imagepng($im,$img);
break;
}
return $t;
}
function exportaImagen($im,$img)
{
$t = false;
switch(true)
{
case ereg('\.jpg',$img):
$t = imagejpeg($im);
break;
case ereg('\.gif',$img):
$t = imagegif($im);
break;
case ereg('\.png',$img):
$t = imagepng($im);
break;
}
return $t;
}
function marcus($im)
{
$sx = imagesx($im);
$sy = imagesy($im);
$x = $sx+20;
$y = $sy+20;
$marcus = imagecreatetruecolor($x,$y);
$color = imagecolorallocate($marcus, 255,255,255); //Blanco
imagefilledrectangle($marcus, 0, 0, ($sx+20), ($sy+20), $color); //Relleno el fondo
imagecopy($marcus, $im, 10, 10, 0, 0, $sx, $sy);
$esquinas = array(array('tl',0,0),array('tr',$x-15,0),array('br',$x-15,$y-15),array('bl',0,$y-15));
$bordes = array(array('t',0,0,$x,10),array('r',$x-10,0,10,$y),array('b',0,$y-10,$x,10),array('l',0,0,10,$y));
foreach($bordes as $bo)
imagecopyresampled ($marcus, imagecreatefrompng($this->dir."img/b_".$bo[0].'.png'), $bo[1], $bo[2], 0, 0, $bo[3], $bo[4], 10, 10);
foreach($esquinas as $es)
imagecopy($marcus, imagecreatefrompng($this->dir."img/b_".$es[0].'.png'), $es[1], $es[2], 0, 0, 15, 15);
return $marcus;
}
function reflekt($im)
{
$sx = imagesx($im);
$sy = imagesy($im);
$x = $sx;
$y = $sy+30;
$reflekt = imagecreatetruecolor($x,$y);
$color = imagecolorallocate($reflekt, 255,255,255); //Blanco
imagefilledrectangle($reflekt, 0, 0, $x, $y, $color); //Relleno el fondo
imagecopy($reflekt, $im, 0, 0, 0, 0, $sx, $sy);
for($i=0;$i<=30;$i++)
imagecopymerge($reflekt, $im, 0, $sy+$i, 0, $sy-($i+1), $x, 1, 60-$i*2);
return $reflekt;
}
function marca($im,$mar)
{
$ma = $this->nuevaImagen($this->dir.'img/marcas/'.$mar);
$mx = imagesx($ma);
$my = imagesy($ma);
imagecopy($im, $ma, 0, 0, 0, 0, $mx, $my);
return $im;
}
function wave($im, $amplitude = 5,$period = 30)
{
$width = imagesx($im);
$height = imagesy($im);
$img2 = imagecreatetruecolor($width * 2, $height * 2);
imagecopyresampled ($img2,$im,0,0,$x,$y,$width * 2,$height * 2,$width, $height);
// Wave it
for ($i = 0;$i < ($width * 2);$i += 2)
{
imagecopy($img2,$img2,
$x + $i - 2,$y + sin($i / $period) * $amplitude, // dest
$x + $i,$y, // src
2,($height * 2));
}
// Resample it down again
imagecopyresampled ($im,$img2,$x,$y,0,0,$width, $height,$width * 2,$height * 2);
imagedestroy($img2);
return $im;
}
function brillo($im)
{
$w = imagesx($im);
$h = imagesy($im);
$imblanc = imagecreatetruecolor(1,$h);
$color = imagecolorallocate($imblanc, 255,255,255); //Blanco
imagefilledrectangle($imblanc, 0, 0, $w, $h, $color); //Relleno el fondo
for($i=0; $i<$w; $i++)
{
imagecopymerge($im, $imblanc, $i, 0, 0, 0, 1, ($h/2+($h/25)*sin(6.3/$w*$i)), 25);
}
return $im;
}
function perspectiva($im) // int $porc = porcentaje de perspectiva (100=nada, 0=completa)
{
$porc = 30; // el porcentaje de reducción
$x = imagesx($im);
$y = imagesy($im);
$pers = imagecreatetruecolor($x,$y);
$color = imagecolorallocate($pers, 255,255,255); //Blanco
imagefilledrectangle($pers, 0, 0, $x, $y, $color); //Relleno el fondo
$factor = ($porc/100)/$x ;
for($i=0;$i<$x;$i++)
{
$hy = round($y-($y*($i+1)*$factor));
$py = round(($y - $hy)/2);
imagecopyresampled ( $pers, $im, $i, $py, $i, 0, 1, $hy, 1, $y );
}
$pers2 = imagecreatetruecolor(0.8*$x,$y);
imagecopyresampled($pers2, $pers, 0, 0, 0, 0, 0.8*$x, $y, $x, $y);
return $pers2;
}
function sombra($im)
{
$sx = imagesx($im);
$sy = imagesy($im);
$x = $sx+16;
$y = $sy+16;
$sombra = imagecreatetruecolor($x,$y);
$color = imagecolorallocate($sombra, 255,255,255); //Blanco
imagefilledrectangle($sombra, 0, 0, ($sx+20), ($sy+20), $color); //Relleno el fondo
imagecopy($sombra, $im, 8, 8, 0, 0, $sx, $sy);
$esquinas = array(array('tl',0,0),array('tr',$x-12,0),array('br',$x-12,$y-12),array('bl',0,$y-12));
$bordes = array(array('t',0,0,$x,8),array('r',$x-8,0,8,$y),array('b',0,$y-8,$x,8),array('l',0,0,8,$y));
foreach($bordes as $bo)
imagecopyresampled ($sombra, imagecreatefrompng($this->dir."img/s_".$bo[0].'.png'), $bo[1], $bo[2], 0, 0, $bo[3], $bo[4], 8, 8);
foreach($esquinas as $es)
imagecopy($sombra, imagecreatefrompng($this->dir."img/s_".$es[0].'.png'), $es[1], $es[2], 0, 0, 12, 12);
return $sombra;
}
function aplicaFiltro($im,$n,$e=false) //resource im, nombre n, extra e
{
switch($n)
{
case 'marcus': return $this->marcus($im);
break;
case 'sombra': return $this->sombra($im);
break;
case 'reflekt': return $this->reflekt($im);
break;
case 'marca': return $this->marca($im,$e);
break;
case 'wave': return $this->wave($im);
break;
case 'brillo': return $this->brillo($im);
break;
case 'perspectiva': return $this->perspectiva($im);
break;
default: return $im;
break;
}
}
}
?>