Advertisement
Guest User

Simple image resize/crop php

a guest
Dec 19th, 2011
1,969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. <?php
  2.  
  3. class SimpleImage {
  4.  
  5. var $image;
  6. var $image_type;
  7.  
  8. function load($filename) {
  9. $image_info = getimagesize($filename);
  10. $this->image_type = $image_info[2];
  11. if( $this->image_type == IMAGETYPE_JPEG ) {
  12. $this->image = imagecreatefromjpeg($filename);
  13. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  14. $this->image = imagecreatefromgif($filename);
  15. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  16. $this->image = imagecreatefrompng($filename);
  17. }
  18. }
  19. function save($filename, $image_type=IMAGETYPE_JPEG, $compression=100, $permissions=null) {
  20. if( $image_type == IMAGETYPE_JPEG ) {
  21. imagejpeg($this->image,$filename,$compression);
  22. } elseif( $image_type == IMAGETYPE_GIF ) {
  23. imagegif($this->image,$filename);
  24. } elseif( $image_type == IMAGETYPE_PNG ) {
  25. imagepng($this->image,$filename);
  26. }
  27. if( $permissions != null) {
  28. chmod($filename,$permissions);
  29. }
  30. }
  31. function output($image_type=IMAGETYPE_JPEG) {
  32. if( $image_type == IMAGETYPE_JPEG ) {
  33. imagejpeg($this->image);
  34. } elseif( $image_type == IMAGETYPE_GIF ) {
  35. imagegif($this->image);
  36. } elseif( $image_type == IMAGETYPE_PNG ) {
  37. imagepng($this->image);
  38. }
  39. }
  40. function getWidth() {
  41. return imagesx($this->image);
  42. }
  43. function getHeight() {
  44. return imagesy($this->image);
  45. }
  46. function resizeToHeight($height) {
  47. $ratio = $height / $this->getHeight();
  48. $width = $this->getWidth() * $ratio;
  49. $this->resize($width,$height);
  50. }
  51. function resizeToWidth($width) {
  52. $ratio = $width / $this->getWidth();
  53. $height = $this->getHeight() * $ratio;
  54. $this->resize($width,$height);
  55. }
  56. function scale($scale) {
  57. $width = $this->getWidth() * $scale/100;
  58. $height = $this->getheight() * $scale/100;
  59. $this->resize($width,$height);
  60. }
  61. function resize($width,$height) {
  62. $new_image = imagecreatetruecolor($width, $height);
  63. $bg = imagecolorallocate($new_image,255,255,255);
  64. imagefill($new_image,0,0,$bg);
  65. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  66. $this->image = $new_image;
  67. }
  68. function crop($filename, $width, $height)
  69. {
  70. list($w, $h, $type, $attr) = getimagesize($filename);
  71. $src_im = imagecreatefromjpeg($filename);
  72.  
  73. $src_x = '0'; // begin x
  74. $src_y = '0'; // begin y
  75. $src_w = $width; // width
  76. $src_h = $height; // height
  77. $dst_x = '0'; // destination x
  78. $dst_y = '0'; // destination y
  79.  
  80. $new_image = imagecreatetruecolor($src_w, $src_h);
  81. $white = imagecolorallocate($new_image, 255, 255, 255);
  82. imagefill($new_image, 0, 0, $white);
  83.  
  84. imagecopy($new_image, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  85. $this->image = $new_image;
  86. }
  87. }
  88.  
  89. $filename= "test.jpg";
  90.  
  91. $thumb = new SimpleImage();
  92. $thumb->load($filename);
  93. $thumb->resizeToWidth(190);
  94. $thumb->crop($filename, 190, 190);
  95. $thumb->save("test".$filename);
  96.  
  97.  
  98.  
  99.  
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement