Posted By

jretamal on 11/13/11


Tagged

resize


Versions (?)

Resize Image


 / Published in: PHP
 

  1. <?php
  2. /*
  3. Filename: example.php
  4. */
  5.  
  6. <img src='photo.php?img=folder/image.jpg&w=320&h=240' />
  7. ?>
  8.  
  9. <?php
  10. /*
  11. Filename: photo.php
  12. */
  13.  
  14. header('Content-Type: image/jpeg');
  15. include('SimpleImage.php');
  16.  
  17. $image = new SimpleImage();
  18. $image->load($_GET['img']);
  19. $image->resize($_GET['w'],$_GET['h']);
  20. $img= $image->output();
  21. ?>
  22.  
  23.  
  24. <?php
  25. /*
  26. Filename: SimpleImage.php
  27. */
  28.  
  29. class SimpleImage {
  30.  
  31. var $image;
  32. var $image_type;
  33.  
  34. function load($filename) {
  35.  
  36. $image_info = getimagesize($filename);
  37. $this->image_type = $image_info[2];
  38. if( $this->image_type == IMAGETYPE_JPEG ) {
  39.  
  40. $this->image = imagecreatefromjpeg($filename);
  41. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  42.  
  43. $this->image = imagecreatefromgif($filename);
  44. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  45.  
  46. $this->image = imagecreatefrompng($filename);
  47. }
  48. }
  49. function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
  50.  
  51. if( $image_type == IMAGETYPE_JPEG ) {
  52. imagejpeg($this->image,$filename,$compression);
  53. } elseif( $image_type == IMAGETYPE_GIF ) {
  54.  
  55. imagegif($this->image,$filename);
  56. } elseif( $image_type == IMAGETYPE_PNG ) {
  57.  
  58. imagepng($this->image,$filename);
  59. }
  60. if( $permissions != null) {
  61.  
  62. chmod($filename,$permissions);
  63. }
  64. }
  65. function output($image_type=IMAGETYPE_JPEG) {
  66.  
  67. if( $image_type == IMAGETYPE_JPEG ) {
  68. imagejpeg($this->image);
  69. } elseif( $image_type == IMAGETYPE_GIF ) {
  70.  
  71. imagegif($this->image);
  72. } elseif( $image_type == IMAGETYPE_PNG ) {
  73.  
  74. imagepng($this->image);
  75. }
  76. }
  77. function getWidth() {
  78.  
  79. return imagesx($this->image);
  80. }
  81. function getHeight() {
  82.  
  83. return imagesy($this->image);
  84. }
  85. function resizeToHeight($height) {
  86.  
  87. $ratio = $height / $this->getHeight();
  88. $width = $this->getWidth() * $ratio;
  89. $this->resize($width,$height);
  90. }
  91.  
  92. function resizeToWidth($width) {
  93. $ratio = $width / $this->getWidth();
  94. $height = $this->getheight() * $ratio;
  95. $this->resize($width,$height);
  96. }
  97.  
  98. function scale($scale) {
  99. $width = $this->getWidth() * $scale/100;
  100. $height = $this->getheight() * $scale/100;
  101. $this->resize($width,$height);
  102. }
  103.  
  104. function resize($width,$height) {
  105. $new_image = imagecreatetruecolor($width, $height);
  106. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  107. $this->image = $new_image;
  108. }
  109.  
  110. }
  111. ?>

Report this snippet  

You need to login to post a comment.