Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Nieruchome gify
Forum PHP.pl > Forum > PHP
lukasz3sko
Witam, oto moja klasa do zmiany parametrow obrazkow.




  1.  
  2. <?php
  3.  
  4.  
  5. class imageResize
  6. {
  7.  
  8. var $image;
  9. var $image_type;
  10.  
  11. function load($filename) {
  12. $image_info = getimagesize($filename);
  13. $this->image_type = $image_info[2];
  14.  
  15.  
  16. if( $this->image_type == IMAGETYPE_JPEG ) {
  17. $this->image = imagecreatefromjpeg($filename);
  18. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  19. $this->image = imagecreatefromgif($filename);
  20. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  21. $this->image = imagecreatefrompng($filename);
  22. }
  23. }
  24. function save($filename, $image_type, $compression=100, $permissions=null) {
  25. if( $image_type == 'image/jpeg' ) {
  26. imagejpeg($this->image,$filename,$compression);
  27. } elseif( $image_type == 'image/gif' ) {
  28. imagegif($this->image,$filename);
  29. } elseif( $image_type == 'image/png' ) {
  30. imagepng($this->image,$filename);
  31. }
  32. if( $permissions != null) {
  33. chmod($filename,$permissions);
  34. }
  35. }
  36. function output($image_type=IMAGETYPE_JPEG) {
  37. if( $image_type == IMAGETYPE_JPEG ) {
  38. imagejpeg($this->image);
  39. } elseif( $image_type == IMAGETYPE_GIF ) {
  40. imagegif($this->image);
  41. } elseif( $image_type == IMAGETYPE_PNG ) {
  42. imagepng($this->image);
  43. }
  44. }
  45. function getWidth() {
  46. return imagesx($this->image);
  47. }
  48. function getHeight() {
  49. return imagesy($this->image);
  50. }
  51. function resizeToHeight($height) {
  52. $ratio = $height / $this->getHeight();
  53. $width = $this->getWidth() * $ratio;
  54. $this->resize($width,$height);
  55. }
  56. function resizeToWidth($width) {
  57. $ratio = $width / $this->getWidth();
  58. $height = $this->getheight() * $ratio;
  59. $this->resize($width,$height);
  60. }
  61. function scale($scale) {
  62. $width = $this->getWidth() * $scale/100;
  63. $height = $this->getheight() * $scale/100;
  64. $this->resize($width,$height);
  65. }
  66. function resize($width,$height) {
  67. $new_image = imagecreatetruecolor($width, $height);
  68. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  69. $this->image = $new_image;
  70. }
  71. function loadReturn($filename) {
  72. $image_info = getimagesize($filename);
  73. $this->image_type = $image_info[2];
  74.  
  75. if( $this->image_type == IMAGETYPE_JPEG ) {
  76. $this->image = imagecreatefromjpeg($filename);
  77. return $this->image;
  78. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  79.  
  80. $this->image = imagecreatefromgif($filename);
  81. return $this->image;
  82.  
  83. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  84. $this->image = imagecreatefrompng($filename);
  85.  
  86. return $this->image;
  87. }
  88. }
  89.  
  90. }
  91. ?>

Najprostszy sposób wykorzystania wygląda tak:
  1. $img_res = new imageResize();
  2. $img_res->load($img_path);
  3. $img_res->resize($newWidth,$newHeight);
  4. $img_res->save($new_img_path,$image_type);
  5.  

Problem występuje w przypadku próby zmiany parametrów gifa który po uploadzie staje się nieruchomy.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.