generate thumbs


/ Published in: PHP
Save to your folder(s)

define ur thumbx


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3.  
  4. class Thumbnail
  5. {
  6.  
  7. const PARENT_PATH = 'C:/Users/gonzaram/Desktop/';
  8.  
  9. protected $_directoryPath;
  10.  
  11. protected $_width;
  12.  
  13. protected $_height;
  14.  
  15. protected $_targetPath;
  16.  
  17. protected $_result = array();
  18.  
  19. protected $_files = array();
  20.  
  21. public function __construct($path)
  22. {
  23. $this->_directoryPath = $path;
  24. $this->_readDir($this->_directoryPath);
  25. return $this;
  26. }
  27.  
  28. public function generate()
  29. {
  30. if (!is_dir($this->_targetPath)) {
  31. mkdir($this->_targetPath, 0777);
  32. }
  33.  
  34. foreach ($this->_files as $index => $file) {
  35. //This will set our output to 45% of the original size
  36. //$size = 0.45;
  37. //$thumb_w = $this->_width * $size;
  38. //$thumb_h = $this->_height * $size;
  39. $fileLocation = self::PARENT_PATH
  40. . $this->_directoryPath
  41. . DIRECTORY_SEPARATOR
  42. . $file;
  43.  
  44. // Setting the resize parameters
  45. list($width, $height) = getimagesize($fileLocation);
  46.  
  47. // Calculate the new size
  48. $ratio1 = $width / $this->_width;
  49. $ratio2 = $height / $this->_height;
  50.  
  51. if($ratio1 > $ratio2) {
  52. $thumb_w = $this->_width;;
  53. $thumb_h = $height / $ratio1;
  54. } else {
  55. $thumb_h = $this->_height;
  56. $thumb_w = $width / $ratio2;
  57. }
  58.  
  59. // Resizing the Image
  60. $tn = imagecreatetruecolor($thumb_w, $thumb_h);
  61. $image = imagecreatefromjpeg($fileLocation);
  62. imagecopyresampled($tn, $image, 0, 0, 0, 0, $thumb_w, $thumb_h, $width, $height);
  63.  
  64. // Outputting a .jpg, you can make this gif or png if you want
  65. //notice we set the quality (third value) to 100
  66. $destinationThumbnail = $this->_targetPath . 'thumb_' . $file;
  67. imagejpeg($tn, $destinationThumbnail, 100);
  68.  
  69. imagedestroy($image);
  70. }
  71. }
  72.  
  73. public function setWidth($width)
  74. {
  75. $this->_width = $width;
  76. return $this;
  77. }
  78.  
  79. public function setHeight($height)
  80. {
  81. $this->_height = $height;
  82. return $this;
  83. }
  84.  
  85. public function setTargetDirectory($target)
  86. {
  87. $this->_targetPath = $target;
  88. return $this;
  89. }
  90.  
  91. private function _readDir($path) {
  92. $fullPath = self::PARENT_PATH . $path;
  93. $resources = scandir($fullPath);
  94.  
  95. foreach ($resources as $resource) {
  96. if ($resource != "." && $resource != "..") {
  97. $resourceDir = $fullPath . '/'. $resource;
  98. if (is_dir($resourceDir)) {
  99. $this->_files[$resource] = $this->_readDir($path . '/' . $resource);
  100. } else {
  101. $this->_files[] = $resource;
  102. }
  103. }
  104. }
  105.  
  106. return $this->_files;
  107. }
  108.  
  109. }
  110.  
  111. $thumbs = new Thumbnail('test');
  112.  
  113. $thumbs->setWidth(250)
  114. ->setHeight(250)
  115. ->generate();
  116.  
  117. echo "<pre>";
  118. var_dump($thumbs);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.