Tag: imagen - Webeame Snippets


Añadir snippet

Tag: imagen



Clase para la manipulación de imágenes con GD

Clase realizada por Daniel Mota, http://icebeat.bitacoras.com/post/279/class-image
  1. <?php
  2.  
  3. /*
  4.  
  5. //Creamos un thumb con 200px de ancho, la altura es automatica.
  6. $thumb = new Image('directorio/imagen.jpg');
  7. $thumb->width(200);
  8. $thumb->save();
  9.  
  10. //Crear un thumb al 50%
  11. $thumb = new Image('directorio/imagen.jpg');
  12. $thumb->resize(50);
  13. $thumb->save();
  14.  
  15. //Cortar una porcion de la imegen
  16. $thumb = new Image('directorio/imagen.jpg');
  17. //indicar el punto de corte
  18. $thumb->crop(0,200);
  19. //luego puedes poner el ancho y el alto que quieras
  20. $thumb->save();
  21.  
  22. //Añadir o cambiar el nombre, no hace falta indicar la extensión
  23. $thumb = new Image('directorio/imagen.jpg');
  24. $thumb->name('imagen2'); // o tambien $thumb->name($thumb->name().'_thumb');
  25. $thumb->width(200);
  26. $thumb->save();
  27.  
  28. */
  29.  
  30. class Image {
  31.  
  32. var $file;
  33. var $image_width;
  34. var $image_height;
  35. var $width;
  36. var $height;
  37. var $ext;
  38. var $types = array('','gif','jpeg','png','swf');
  39. var $quality = 80;
  40. var $top = 0;
  41. var $left = 0;
  42. var $crop = false;
  43. var $type;
  44.  
  45. function Image($name='') {
  46. $this->file = $name;
  47. $info = getimagesize($name);
  48. $this->image_width = $info[0];
  49. $this->image_height = $info[1];
  50. $this->type = $this->types[$info[2]];
  51. $info = pathinfo($name);
  52. $this->dir = $info['dirname'];
  53. $this->name = str_replace('.'.$info['extension'], '', $info['basename']);
  54. $this->ext = $info['extension'];
  55. }
  56.  
  57. function dir($dir='') {
  58. if(!$dir) return $this->dir;
  59. $this->dir = $dir;
  60. }
  61.  
  62. function name($name='') {
  63. if(!$name) return $this->name;
  64. $this->name = $name;
  65. }
  66.  
  67. function width($width='') {
  68. $this->width = $width;
  69. }
  70.  
  71. function height($height='') {
  72. $this->height = $height;
  73. }
  74.  
  75. function resize($percentage=50) {
  76. if($this->crop) {
  77. $this->crop = false;
  78. $this->width = round($this->width*($percentage/100));
  79. $this->height = round($this->height*($percentage/100));
  80. $this->image_width = round($this->width/($percentage/100));
  81. $this->image_height = round($this->height/($percentage/100));
  82. } else {
  83. $this->width = round($this->image_width*($percentage/100));
  84. $this->height = round($this->image_height*($percentage/100));
  85. }
  86.  
  87. }
  88.  
  89. function crop($top=0, $left=0) {
  90. $this->crop = true;
  91. $this->top = $top;
  92. $this->left = $left;
  93. }
  94.  
  95. function quality($quality=80) {
  96. $this->quality = $quality;
  97. }
  98.  
  99. function show() {
  100. $this->save(true);
  101. }
  102.  
  103. function save($show=false) {
  104.  
  105. if($show) @header('Content-Type: image/'.$this->type);
  106.  
  107. if(!$this->width && !$this->height) {
  108. $this->width = $this->image_width;
  109. $this->height = $this->image_height;
  110. } elseif (is_numeric($this->width) && empty($this->height)) {
  111. $this->height = round($this->width/($this->image_width/$this->image_height));
  112. } elseif (is_numeric($this->height) && empty($this->width)) {
  113. $this->width = round($this->height/($this->image_height/$this->image_width));
  114. } else {
  115. if($this->width<=$this->height) {
  116. $height = round($this->width/($this->image_width/$this->image_height));
  117. if($height!=$this->height) {
  118. $percentage = ($this->image_height*100)/$height;
  119. $this->image_height = round($this->height*($percentage/100));
  120. }
  121. } else {
  122. $width = round($this->height/($this->image_height/$this->image_width));
  123. if($width!=$this->width) {
  124. $percentage = ($this->image_width*100)/$width;
  125. $this->image_width = round($this->width*($percentage/100));
  126. }
  127. }
  128. }
  129.  
  130. if($this->crop) {
  131. $this->image_width = $this->width;
  132. $this->image_height = $this->height;
  133. }
  134.  
  135. if($this->type=='jpeg') $image = imagecreatefromjpeg($this->file);
  136. if($this->type=='png') $image = imagecreatefrompng($this->file);
  137. if($this->type=='gif') $image = imagecreatefromgif($this->file);
  138.  
  139. $new_image = imagecreatetruecolor($this->width, $this->height);
  140. imagecopyresampled($new_image, $image, 0, 0, $this->top, $this->left, $this->width, $this->height, $this->image_width, $this->image_height);
  141.  
  142. $name = $show ? null: $this->dir.DIRECTORY_SEPARATOR.$this->name.'.'.$this->ext;
  143. if($this->type=='jpeg') imagejpeg($new_image, $name, $this->quality);
  144. if($this->type=='png') imagepng($new_image, $name);
  145. if($this->type=='gif') imagegif($new_image, $name);
  146.  
  147. imagedestroy($image);
  148. imagedestroy($new_image);
  149.  
  150. }
  151.  
  152. }
  153.  
  154. ?>
http://icebeat.bitacoras.com/descarga/class.image.phps

En PHP imagen gd por kike hace on 18/2/08 | Comentarios



Ejemplo básico para generar una imagen con GD

  1. <?php
  2.  
  3. // Definimos los headers
  4. header("Content-type: image/gif");
  5.  
  6. // Creamos la imagen
  7. $imagen = imagecreate(400,300);
  8.  
  9. // Agregamos contenido
  10. $blanco = imagecolorallocate($imagen,255,255,255);
  11. $negro = imagecolorallocate($imagen,0,0,0);
  12. $rojo = imagecolorallocate($imagen,200,0,0);
  13. $verde = imagecolorallocate($imagen,0,130,0);
  14. $gris = imagecolorallocate($imagen,140,140,140);
  15.  
  16. imagefilledrectangle($imagen,50,50,145,250,$verde);
  17. imagefilledrectangle($imagen,255,50,350,250,$rojo);
  18. imagefilledellipse($imagen,200,150,80,80,$gris);
  19. imagerectangle($imagen,50,50,350,250,$negro);
  20.  
  21. // Damos salida a la imagen
  22. imagegif($imagen);
  23.  
  24. // Destruimos la imagen
  25. imagedestroy($imagen);
  26.  
  27. ?>
http://www.washeebo.com/sargento/03_php/0301/0301.php

En PHP GD imagen por admin hace on 30/1/08 | Comentarios