PageRenderTime 73ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/external/SimpleImage/SimpleImage.php

https://github.com/unm-art/LibRooms
PHP | 143 lines | 104 code | 16 blank | 23 comment | 21 complexity | aa2f06942ec6b2f029e8dcb3f68ed453 MD5 | raw file
Possible License(s): GPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause, MIT
  1. <?php
  2. /*
  3. * File: SimpleImage.php
  4. * Author: Simon Jarvis
  5. * Copyright: 2006 Simon Jarvis
  6. * Date: 08/11/06
  7. * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details:
  18. * http://www.gnu.org/licenses/gpl.html
  19. *
  20. */
  21. class SimpleImage
  22. {
  23. var $image;
  24. var $image_type;
  25. function load($filename)
  26. {
  27. $image_info = getimagesize($filename);
  28. $this->image_type = $image_info[2];
  29. if ($this->image_type == IMAGETYPE_JPEG)
  30. {
  31. $this->image = imagecreatefromjpeg($filename);
  32. }
  33. elseif ($this->image_type == IMAGETYPE_GIF)
  34. {
  35. $this->image = imagecreatefromgif($filename);
  36. }
  37. elseif ($this->image_type == IMAGETYPE_PNG)
  38. {
  39. $this->image = imagecreatefrompng($filename);
  40. }
  41. }
  42. function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 75, $permissions = null)
  43. {
  44. // do this or they'll all go to jpeg
  45. $image_type = $this->image_type;
  46. if ($image_type == IMAGETYPE_JPEG)
  47. {
  48. imagejpeg($this->image, $filename, $compression);
  49. }
  50. elseif ($image_type == IMAGETYPE_GIF)
  51. {
  52. imagegif($this->image, $filename);
  53. }
  54. elseif ($image_type == IMAGETYPE_PNG)
  55. {
  56. // need this for transparent png to work
  57. imagealphablending($this->image, false);
  58. imagesavealpha($this->image, true);
  59. imagepng($this->image, $filename);
  60. }
  61. if ($permissions != null)
  62. {
  63. chmod($filename, $permissions);
  64. }
  65. }
  66. function output($image_type = IMAGETYPE_JPEG)
  67. {
  68. if ($image_type == IMAGETYPE_JPEG)
  69. {
  70. imagejpeg($this->image);
  71. }
  72. elseif ($image_type == IMAGETYPE_GIF)
  73. {
  74. imagegif($this->image);
  75. }
  76. elseif ($image_type == IMAGETYPE_PNG)
  77. {
  78. imagepng($this->image);
  79. }
  80. }
  81. function getWidth()
  82. {
  83. return imagesx($this->image);
  84. }
  85. function getHeight()
  86. {
  87. return imagesy($this->image);
  88. }
  89. function resizeToHeight($height)
  90. {
  91. $ratio = $height / $this->getHeight();
  92. $width = $this->getWidth() * $ratio;
  93. $this->resize($width, $height);
  94. }
  95. function resizeToWidth($width)
  96. {
  97. $ratio = $width / $this->getWidth();
  98. $height = $this->getheight() * $ratio;
  99. $this->resize($width, $height);
  100. }
  101. function scale($scale)
  102. {
  103. $width = $this->getWidth() * $scale / 100;
  104. $height = $this->getheight() * $scale / 100;
  105. $this->resize($width, $height);
  106. }
  107. function resize($width,$height,$forcesize='n') {
  108. /* optional. if file is smaller, do not resize. */
  109. if ($forcesize == 'n') {
  110. if ($width > $this->getWidth() && $height > $this->getHeight()){
  111. $width = $this->getWidth();
  112. $height = $this->getHeight();
  113. }
  114. }
  115. $new_image = imagecreatetruecolor($width, $height);
  116. /* Check if this image is PNG or GIF, then set if Transparent*/
  117. if(($this->image_type == IMAGETYPE_GIF) || ($this->image_type==IMAGETYPE_PNG)){
  118. imagealphablending($new_image, false);
  119. imagesavealpha($new_image,true);
  120. $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
  121. imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
  122. }
  123. imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  124. $this->image = $new_image;
  125. }
  126. }
  127. ?>