Advertisement
plas71k

fb app system: inc/resize.php

Dec 4th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. <?php
  2. Class resize
  3. {
  4. private $image;
  5. private $width;
  6. private $height;
  7. private $imageResized;
  8. function __construct($fileName)
  9. {
  10. $this->image = $this->openImage($fileName);
  11. $this->width  = imagesx($this->image);
  12. $this->height = imagesy($this->image);
  13. }
  14. private function openImage($file)
  15. {
  16. $extension = strtolower(strrchr($file,'.'));
  17. switch($extension)
  18. {
  19. case '.jpg':
  20. case '.jpeg':
  21. $img = @imagecreatefromjpeg($file);
  22. break;
  23. case '.gif':
  24. $img = @imagecreatefromgif($file);
  25. break;
  26. case '.png':
  27. $img = @imagecreatefrompng($file);
  28. break;
  29. default:
  30. $img = false;
  31. break;
  32. }
  33. return $img;
  34. }
  35. public function resizeImage($newWidth,$newHeight,$option="auto")
  36. {
  37. $optionArray = $this->getDimensions($newWidth,$newHeight,$option);
  38. $optimalWidth  = $optionArray['optimalWidth'];
  39. $optimalHeight = $optionArray['optimalHeight'];
  40. $this->imageResized = imagecreatetruecolor($optimalWidth,$optimalHeight);
  41. imagecopyresampled($this->imageResized,$this->image,0,0,0,0,$optimalWidth,$optimalHeight,$this->width,$this->height);
  42. if ($option == 'crop') {
  43. $this->crop($optimalWidth,$optimalHeight,$newWidth,$newHeight);
  44. }
  45. }
  46. private function getDimensions($newWidth,$newHeight,$option)
  47. {
  48. switch ($option)
  49. {
  50. case 'exact':
  51. $optimalWidth = $newWidth;
  52. $optimalHeight= $newHeight;
  53. break;
  54. case 'portrait':
  55. $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  56. $optimalHeight= $newHeight;
  57. break;
  58. case 'landscape':
  59. $optimalWidth = $newWidth;
  60. $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  61. break;
  62. case 'auto':
  63. $optionArray = $this->getSizeByAuto($newWidth,$newHeight);
  64. $optimalWidth = $optionArray['optimalWidth'];
  65. $optimalHeight = $optionArray['optimalHeight'];
  66. break;
  67. case 'crop':
  68. $optionArray = $this->getOptimalCrop($newWidth,$newHeight);
  69. $optimalWidth = $optionArray['optimalWidth'];
  70. $optimalHeight = $optionArray['optimalHeight'];
  71. break;
  72. }
  73. return array('optimalWidth'=>$optimalWidth,'optimalHeight'=>$optimalHeight);
  74. }
  75. private function getSizeByFixedHeight($newHeight)
  76. {
  77. $ratio = $this->width / $this->height;
  78. $newWidth = $newHeight * $ratio;
  79. return $newWidth;
  80. }
  81. private function getSizeByFixedWidth($newWidth)
  82. {
  83. $ratio = $this->height / $this->width;
  84. $newHeight = $newWidth * $ratio;
  85. return $newHeight;
  86. }
  87. private function getSizeByAuto($newWidth,$newHeight)
  88. {
  89. if ($this->height <$this->width)
  90. {
  91. $optimalWidth = $newWidth;
  92. $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  93. }
  94. elseif ($this->height >$this->width)
  95. {
  96. $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  97. $optimalHeight= $newHeight;
  98. }
  99. else
  100. {
  101. if ($newHeight <$newWidth) {
  102. $optimalWidth = $newWidth;
  103. $optimalHeight= $this->getSizeByFixedWidth($newWidth);
  104. }else if ($newHeight >$newWidth) {
  105. $optimalWidth = $this->getSizeByFixedHeight($newHeight);
  106. $optimalHeight= $newHeight;
  107. }else {
  108. $optimalWidth = $newWidth;
  109. $optimalHeight= $newHeight;
  110. }
  111. }
  112. return array('optimalWidth'=>$optimalWidth,'optimalHeight'=>$optimalHeight);
  113. }
  114. private function getOptimalCrop($newWidth,$newHeight)
  115. {
  116. $heightRatio = $this->height / $newHeight;
  117. $widthRatio  = $this->width /  $newWidth;
  118. if ($heightRatio <$widthRatio) {
  119. $optimalRatio = $heightRatio;
  120. }else {
  121. $optimalRatio = $widthRatio;
  122. }
  123. // ================================== PAYMENT ====================================//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement