/ch08/Thumbnail_04.php

https://github.com/lisawilliams/phpsols · PHP · 159 lines · 129 code · 10 blank · 20 comment · 30 complexity · e51728966cd7da08e75f0fe923ea2d7e MD5 · raw file

  1. <?php
  2. class Ps2_Thumbnail {
  3. protected $_original;
  4. protected $_originalwidth;
  5. protected $_originalheight;
  6. protected $_thumbwidth;
  7. protected $_thumbheight;
  8. protected $_maxSize = 120;
  9. protected $_canProcess = false;
  10. protected $_imageType;
  11. protected $_destination;
  12. protected $_name;
  13. protected $_suffix = '_thb';
  14. protected $_messages = array();
  15. public function __construct($image) {
  16. if (is_file($image) && is_readable($image)) {
  17. $details = getimagesize($image);
  18. } else {
  19. $details = null;
  20. $this->_messages[] = "Cannot open $image.";
  21. }
  22. // if getimagesize() returns an array, it looks like an image
  23. if (is_array($details)) {
  24. $this->_original = $image;
  25. $this->_originalwidth = $details[0];
  26. $this->_originalheight = $details[1];
  27. // check the MIME type
  28. $this->checkType($details['mime']);
  29. } else {
  30. $this->_messages[] = "$image doesn't appear to be an image.";
  31. }
  32. }
  33. public function setDestination($destination) {
  34. if (is_dir($destination) && is_writable($destination)) {
  35. // get last character
  36. $last = substr($destination, -1);
  37. // add a trailing slash if missing
  38. if ($last == '/' || $last == '\\') {
  39. $this->_destination = $destination;
  40. } else {
  41. $this->_destination = $destination . DIRECTORY_SEPARATOR;
  42. }
  43. } else {
  44. $this->_messages[] = "Cannot write to $destination.";
  45. }
  46. }
  47. public function setMaxSize($size) {
  48. if (is_numeric($size)) {
  49. $this->_maxSize = abs($size);
  50. }
  51. }
  52. public function setSuffix($suffix) {
  53. if (preg_match('/^\w+$/', $suffix)) {
  54. if (strpos($suffix, '_') !== 0) {
  55. $this->_suffix = '_' . $suffix;
  56. } else {
  57. $this->_suffix = $suffix;
  58. }
  59. } else {
  60. $this->_suffix = '';
  61. }
  62. }
  63. public function create() {
  64. if ($this->_canProcess && $this->_originalwidth != 0) {
  65. $this->calculateSize($this->_originalwidth, $this->_originalheight);
  66. $this->getName();
  67. $this->createThumbnail();
  68. } elseif ($this->_originalwidth == 0) {
  69. $this->_messages[] = 'Cannot determine size of ' . $this->_original;
  70. }
  71. }
  72. public function getMessages() {
  73. return $this->_messages;
  74. }
  75. /*
  76. public function test() {
  77. echo 'File: ' . $this->_original . '<br>';
  78. echo 'Original width: ' . $this->_originalwidth . '<br>';
  79. echo 'Original height: ' . $this->_originalheight . '<br>';
  80. echo 'Image type: ' . $this->_imageType . '<br>';
  81. echo 'Destination: ' . $this->_destination . '<br>';
  82. echo 'Max size: ' . $this->_maxSize . '<br>';
  83. echo 'Suffix: ' . $this->_suffix . '<br>';
  84. echo 'Thumb width: ' . $this->_thumbwidth . '<br>';
  85. echo 'Thumb height: ' . $this->_thumbheight . '<br>';
  86. echo 'Base name: ' . $this->_name . '<br>';
  87. if ($this->_messages) {
  88. print_r($this->_messages);
  89. }
  90. }
  91. */
  92. protected function checkType($mime) {
  93. $mimetypes = array('image/jpeg', 'image/png', 'image/gif');
  94. if (in_array($mime, $mimetypes)) {
  95. $this->_canProcess = true;
  96. // extract the characters after 'image/'
  97. $this->_imageType = substr($mime, 6);
  98. }
  99. }
  100. protected function calculateSize($width, $height) {
  101. if ($width <= $this->_maxSize && $height <= $this->_maxSize) {
  102. $ratio = 1;
  103. } elseif ($width > $height) {
  104. $ratio = $this->_maxSize/$width;
  105. } else {
  106. $ratio = $this->_maxSize/$height;
  107. }
  108. $this->_thumbwidth = round($width * $ratio);
  109. $this->_thumbheight = round($height * $ratio);
  110. }
  111. protected function getName() {
  112. $extensions = array('/\.jpg$/i', '/\.jpeg$/i', '/\.png$/i', '/\.gif$/i');
  113. $this->_name = preg_replace($extensions, '', basename($this->_original));
  114. }
  115. protected function createImageResource() {
  116. if ($this->_imageType == 'jpeg') {
  117. return imagecreatefromjpeg($this->_original);
  118. } elseif ($this->_imageType == 'png') {
  119. return imagecreatefrompng($this->_original);
  120. } elseif ($this->_imageType == 'gif') {
  121. return imagecreatefromgif($this->_original);
  122. }
  123. }
  124. protected function createThumbnail() {
  125. $resource = $this->createImageResource();
  126. $thumb = imagecreatetruecolor($this->_thumbwidth, $this->_thumbheight);
  127. imagecopyresampled($thumb, $resource, 0, 0, 0, 0, $this->_thumbwidth, $this->_thumbheight, $this->_originalwidth, $this->_originalheight);
  128. $newname = $this->_name . $this->_suffix;
  129. if ($this->_imageType == 'jpeg') {
  130. $newname .= '.jpg';
  131. $success = imagejpeg($thumb, $this->_destination . $newname, 100);
  132. } elseif ($this->_imageType == 'png') {
  133. $newname .= '.png';
  134. $success = imagepng($thumb, $this->_destination . $newname);
  135. } elseif ($this->_imageType == 'gif') {
  136. $newname .= '.gif';
  137. $success = imagegif($thumb, $this->_destination . $newname);
  138. }
  139. if ($success) {
  140. $this->_messages[] = "$newname created successfully.";
  141. } else {
  142. $this->_messages[] = "Couldn't create a thumbnail for " . basename($this->_original);
  143. }
  144. imagedestroy($resource);
  145. imagedestroy($thumb);
  146. }
  147. }