Ver Mensaje Individual
  #14 (permalink)  
Antiguo 12/02/2009, 14:02
Avatar de ElJavista
ElJavista
Colaborador
 
Fecha de Ingreso: marzo-2007
Ubicación: Lima Perú
Mensajes: 2.231
Antigüedad: 19 años, 7 meses
Puntos: 67
Respuesta: Imagenes en base de datos

Bueno, no sé si ya te servirá porque te han ayudado otros, pero de todos modos si no es a ti a alguien le podría servir:

Código PHP:
<?php
  
class Imagen {
        private 
$exist = false;
        private 
$width = 0;
        private 
$height = 0;
        private 
$source = null;
        private 
$thumb = null;

        function 
Imagen($img) {
                 if (
file_exists($img)) {
                    list(
$wi, $he, $tip) = getimagesize($img);
                    
$this->width = $wi;
                    
$this->height = $he;

                    if (
$tip == 1) $this->source = imagecreatefromgif($img);
                    elseif (
$tip == 2) $this->source = imagecreatefromjpeg($img);
                    elseif (
$tip == 3) $this->source = imagecreatefrompng($img);
                    
$this->exist = true;
                 }
        }

        private function 
createThumb($w, $h) {
                
$this->thumb = imagecreatetruecolor($w, $h);
        }

        function 
makeThumb($ruta, $w, $h, $opc = false) {
                 if (
$this->exist) {
                    
$wi = $this->width;
                    
$he = $this->height;

                    if (
$w) {
                       
$wp = $w;
                       
$hp = round(($he / $wi) * $wp);
                    }  elseif (
$h) {
                       
$hp = $h;
                       
$wp = round(($wi / $he) * $hp);
                    }

                    
$this->createThumb($wp, $hp);
                    
$this->reDim(0, 0, 0, 0, $wp, $hp, $wi, $he);

                    if (
$opc) return $this->thumb;
                    else return 
$this->setImg($ruta);
                 }  else return 
false;
        }

        function 
makeCuadro($ruta, $w, $h, $opc = false) {
                 if (
$this->exist) {
                    if (
$w && $h) {
                       
$wi = $this->width;
                       
$he = $this->height;

                       if (
$wi > $w || $he > $h) {
                          if (
$wi / $w > $he / $h) {
                             
$wp = $w;
                             
$hp = round(($he / $wi) * $wp);
                          }  else {
                             
$hp = $h;
                             
$wp = round(($wi / $he) * $hp);                       
                          }
                       }  else {
                          
$wp = $wi;
                          
$hp = $he;
                       }

                       
$this->createThumb($wp, $hp);
                       
$this->reDim(0, 0, 0, 0, $wp, $hp, $wi, $he);

                       if (
$opc) return $this->thumb;
                       else return 
$this->setImg($ruta);
                    }  else return 
false;
                 }  else return 
false;
        }

        function 
makeCorte($ruta, $w, $h, $opc = false) {
                 if (
$this->exist) {
                    
$wi = $this->width;
                    
$he = $this->height;

                    if (
$w && $h) {
                       if (
$wi / $w < $he / $h) {
                          
$wp = $wi;
                          
$hp = round(($h / $w) * $wp);
                       }  else {
                          
$hp = $he;
                          
$wp = round(($w / $h) * $hp);
                       }
                       
$lf = round(($wi - $wp) / 2);
                       
$tp = round(($he - $hp) / 2);

                       
$this->createThumb($w, $h);
                       
$this->reDim(0, 0, $lf, $tp, $w, $h, $wp, $hp);

                       if (
$opc) return $this->thumb;
                       else return 
$this->setImg($ruta);
                    }  else return 
false;
                 }  else return 
false;
        }

        private function 
reDim($dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH) {
                
imagecopyresampled($this->thumb, $this->source, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
        }

        private function 
setImg($ruta) {
                return 
imagejpeg($this->thumb, $ruta);
        }
}
if (isset(
$_GET["img"])) {
   
$img = $_GET["img"];
   if (
file_exists($img)) {
      
$ima = new Imagen($img);
      
$tip = $_GET["tip"];
      
$w = $_GET["w"];
      
$h = $_GET["h"];

      if (
$tip == 0) $thumb = $ima->makeThumb("", $w, $h, true);
      if (
$tip == 1) $thumb = $ima->makeCuadro("", $w, $h, true);
      if (
$tip == 2) $thumb = $ima->makeCorte("", $w, $h, true);

      if (
$thumb) {
         
header('Content-type: image/jpeg');
         
imagejpeg($thumb);
      }  else echo 
"Error";
   }
}
?>