Ver Mensaje Individual
  #13 (permalink)  
Antiguo 08/06/2011, 13:30
Avatar de destor77
destor77
 
Fecha de Ingreso: noviembre-2004
Ubicación: Gálvez, Santa Fe, Argentina
Mensajes: 2.654
Antigüedad: 19 años, 6 meses
Puntos: 43
Respuesta: [APORTE] Subir y redimensionar imagenes

@abimaelrc,
no me funciona con tu script, me pone el fondo negro, te dejo mi como tengo mi clase para que vas:

Código PHP:
Ver original
  1. <?php
  2. class SimpleImage {
  3.  
  4.     private  $image;
  5.     private  $image_type;
  6.     private  $instance;
  7.     private  $transparent;
  8.  
  9.     /**
  10.      * patron singleton
  11.      *
  12.      * @version 0.2
  13.      * @author Lucas M. Sastre
  14.      * @access Public
  15.      * @name singleton
  16.      *
  17.      * @return $instance
  18.      *
  19.      * Modificado:
  20.      *
  21.      */
  22.     public static function singleton() {
  23.         if( self::$instance == null ) {
  24.             self::$instance = new self();
  25.         }
  26.  
  27.         return self::$instance;
  28.     }
  29.  
  30.     public function setTransparent($bool)
  31.     {
  32.         $this->transparent = (boolean)$bool;
  33.     }
  34.  
  35.     /**
  36.      * carga una imagen para obtener su propiedades
  37.      *
  38.      * @version 0.2
  39.      * @author Lucas M. Sastre
  40.      * @access Public
  41.      * @name load
  42.      *
  43.      * @param string $filename
  44.      *
  45.      * Modificado:
  46.      *
  47.      */
  48.     public function load($filename) {
  49.         $image_info = getimagesize($filename);
  50.         $this->image_type = $image_info[2];
  51.         if( $this->image_type == IMAGETYPE_JPEG ) {
  52.             $this->image = imagecreatefromjpeg($filename);
  53.         } elseif( $this->image_type == IMAGETYPE_GIF ) {
  54.             $this->image = imagecreatefromgif($filename);
  55.         } elseif( $this->image_type == IMAGETYPE_PNG ) {
  56.             $this->image = imagecreatefrompng($filename);
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * guarda una imagen
  62.      *
  63.      * @version 0.2
  64.      * @author Lucas M. Sastre
  65.      * @access Public
  66.      * @name save
  67.      *
  68.      * @param string $filename
  69.      * @param string $image_type
  70.      * @param integer $compression
  71.      * @param integer $permissions
  72.      *
  73.      * Modificado:
  74.      *
  75.      */
  76.     public function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  77.         if( $image_type == IMAGETYPE_JPEG ) {
  78.             imagejpeg($this->image,$filename,$compression);
  79.         } elseif( $image_type == IMAGETYPE_GIF ) {
  80.             imagegif($this->image,$filename);
  81.         } elseif( $image_type == IMAGETYPE_PNG ) {
  82.             imagepng($this->image,$filename);
  83.         }
  84.         if( $permissions != null) {
  85.             chmod($filename,$permissions);
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * genera la salida de la image segun su extension
  91.      *
  92.      * @version 0.2
  93.      * @author Lucas M. Sastre
  94.      * @access Public
  95.      * @name output
  96.      *
  97.      * @param string $image_type
  98.      *
  99.      * Modificado:
  100.      *
  101.      */
  102.     public function output($image_type=IMAGETYPE_JPEG) {
  103.         if( $image_type == IMAGETYPE_JPEG ) {
  104.             imagejpeg($this->image);
  105.         } elseif( $image_type == IMAGETYPE_GIF ) {
  106.             imagegif($this->image);
  107.         } elseif( $image_type == IMAGETYPE_PNG ) {
  108.             imagepng($this->image);
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * recupera el ancho de la imagen
  114.      *
  115.      * @version 0.2
  116.      * @author Lucas M. Sastre
  117.      * @access Public
  118.      * @name getWidth
  119.      *
  120.      * Modificado:
  121.      *
  122.      */
  123.     public function getWidth() {
  124.         return imagesx($this->image);
  125.     }
  126.  
  127.     /**
  128.      * recupera el alto de la imagen
  129.      *
  130.      * @version 0.2
  131.      * @author Lucas M. Sastre
  132.      * @access Public
  133.      * @name getHeight
  134.      *
  135.      * Modificado:
  136.      *
  137.      */
  138.     public function getHeight() {
  139.         return imagesy($this->image);
  140.     }
  141.  
  142.     /**
  143.      * redimenciona segun el alto
  144.      *
  145.      * @version 0.2
  146.      * @author Lucas M. Sastre
  147.      * @access Public
  148.      * @name resizeToHeight
  149.      *
  150.      * @param integer $height
  151.      *
  152.      * Modificado:
  153.      *
  154.      */
  155.     public function resizeToHeight($height) {
  156.         $ratio = $height / $this->getHeight();
  157.         $width = $this->getWidth() * $ratio;
  158.         $this->resize($width,$height);
  159.     }
  160.  
  161.     /**
  162.      * redimenciona segun el ancho
  163.      *
  164.      * @version 0.2
  165.      * @author Lucas M. Sastre
  166.      * @access Public
  167.      * @name resizeToWidth
  168.      *
  169.      * @param integer $width
  170.      *
  171.      * Modificado:
  172.      *
  173.      */
  174.     public function resizeToWidth($width) {
  175.         $ratio = $width / $this->getWidth();
  176.         $height = $this->getheight() * $ratio;
  177.         $this->resize($width,$height);
  178.     }
  179.  
  180.     /**
  181.      * escala una imagen segun el valor pasado
  182.      *
  183.      * @version 0.2
  184.      * @author Lucas M. Sastre
  185.      * @access Public
  186.      * @name scale
  187.      *
  188.      * @param integer $scale
  189.      *
  190.      * Modificado:
  191.      *
  192.      */
  193.     public function scale($scale) {
  194.         $width = $this->getWidth() * $scale/100;
  195.         $height = $this->getheight() * $scale/100;
  196.         $this->resize($width,$height);
  197.     }
  198.  
  199.     /**
  200.      * no reduce una imagen
  201.      *
  202.      * @version 0.2
  203.      * @author Lucas M. Sastre
  204.      * @access Public
  205.      * @name noResize
  206.      *
  207.      *
  208.      * Modificado:
  209.      *
  210.      */
  211.     public function noResize() {
  212.         $new_image = imagecreatetruecolor($this->getWidth(), $this->getHeight());
  213.  
  214.         //creo la transprencia para los png y gif
  215.         if(($this->image_type == IMAGETYPE_GIF) OR ($this->image_type == IMAGETYPE_PNG)) {
  216.             imagealphablending($this->image, false);
  217.             imagesavealpha($this->image,true);
  218.             $transparent = imagecolorallocate($new_image, 255, 255, 255);//imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  219.             //imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
  220.             imagefill($new_image, 0, 0, $transparent);
  221.         }
  222.  
  223.         imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $this->getWidth(), $this->getHeight(), $this->getWidth(), $this->getHeight());
  224.         $this->image = $new_image;
  225.     }
  226.  
  227.     /**
  228.      * reduce una imagen
  229.      *
  230.      * @version 0.2
  231.      * @author Lucas M. Sastre
  232.      * @access Public
  233.      * @name resize
  234.      *
  235.      * @param integer $width
  236.      * @param integer $height
  237.      *
  238.      * Modificado:
  239.      *
  240.      */
  241.     public function resize($width,$height) {
  242.         $new_image = imagecreatetruecolor($width, $height);
  243.  
  244.         //creo la transprencia para los png y gif
  245.        /*
  246. asi tenia mi codigo original
  247. if(($this->image_type == IMAGETYPE_GIF) OR ($this->image_type == IMAGETYPE_PNG)) {
  248.             imagealphablending($this->image, false);
  249.             imagesavealpha($this->image,true);
  250.             $transparent = imagecolorallocate($new_image, 255, 255, 255);//imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  251.             //imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
  252.             imagefill($new_image, 0, 0, $transparent);
  253.         }
  254.  
  255.         imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  256.         $this->image = $new_image;*/
  257.  
  258.         $newImage = imagecreatetruecolor($width, $height);
  259.         if($this->image_type == IMAGETYPE_PNG && $this->transparent === true){
  260.             imagealphablending($newImage, false);
  261.             imagesavealpha($newImage, true);
  262.             $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127);
  263.             imagefilledrectangle($newImage, 0, 0, $width, $height, $transparent);
  264.         }
  265.         imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  266.         $this->image = $newImage;
  267.     }
  268. }
  269. ?>

salu2