Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Klasa do manipulacji obrazkiem
Forum PHP.pl > Inne > Oceny
in5ane
Witam, kontynuując temat z "Przedszkola" chciałbym zaprezentować Wam moją klasę do manipulacji obrazkiem. Jest to (tak to nazwijmy) wersja alfa. Na razie obsługuje tylko plik JPG, nie robiłem jeszcze rozpoznawania typu pliku graficznego (docelowo zrobię JPG, GIF, PNG). Funkcje jakie są w niej dostępne to tworzenie miniatur, do tego (tak na próbę tylko dwa) efekty filtrów: smooth i brightness oraz tworzenie znaków wodnych (obrazkowych oraz tekstowych). Proszę o opinie na temat prób mojego programowania obiektowego.

class.manipicture.php
  1. <?php
  2. /**
  3. * @access public
  4. * @author Jakub Kubera <jakubkubera1992@gmail.com>
  5. * @copyright (c) 2013 http://jakubkubera.pl
  6. * @description Class for manipulating the picture
  7. * @version 1.0
  8. * @license http://creativecommons.org/licenses/by/3.0/ Creative Commons 3.0
  9. * @param string $fileName Path to image (JPG file)
  10. * @param integer $widthThumb Width of the thumbnail
  11. * @param integer $heightThumb Height of the thumbnail
  12. * @param string $fileNameWatermark Path to watermark image (PNG file) (for creating watermark)
  13. * @param integer $positionWatermark Position for watermark image (* 0: Centered, * 1: Top Left, * 2: Top Right, * 3: Footer Right, * 4: Footer left, * 5: Top Centered, * 6: Center Right, * 7: Footer Centered, * 8: Center Left)
  14. * @param string $textWatermark Text for watermark text
  15. * @param integer $sizeWatermark Font size for watermark text
  16. * @param integer $positionWatermarkText Position for watermark text (* 0: Centered, * 1: Top Left, * 2: Top Right, * 3: Footer Right, * 4: Footer left, * 5: Top Centered, * 6: Center Right, * 7: Footer Centered, * 8: Center Left)
  17. */
  18. class ManiPicture
  19. {
  20. public $name;
  21. private $img;
  22. private $width;
  23. private $height;
  24. private $imgNew;
  25. private $widthThumb;
  26. private $heightThumb;
  27. private $watermark;
  28. private $watermarkPosition = 0;
  29. private $watermarkText = 'Default text';
  30. private $watermarkTextSize = 15;
  31. private $watermarkPositionTextWidth;
  32. private $watermarkPositionTextHeight;
  33.  
  34. public function __construct($name)
  35. {
  36. $this->name = $name;
  37. }
  38.  
  39. /*
  40. * Creating the thumbnail
  41. */
  42. public function createThumb($widthThumb, $heightThumb)
  43. {
  44. $this->widthThumb = $widthThumb;
  45. $this->heightThumb = $heightThumb;
  46.  
  47. $this->img = imagecreatefromjpeg($this->name);
  48. $this->width = imagesx($this->img);
  49. $this->height = imagesy($this->img);
  50.  
  51. $this->imgNew = imagecreatetruecolor($this->widthThumb, $this->heightThumb);
  52. imagecopyresampled($this->imgNew, $this->img, 0, 0, 0, 0, $this->widthThumb, $this->heightThumb, $this->width, $this->height);
  53. imagejpeg($this->imgNew, 'thumb_'.$this->name, 100);
  54. imagedestroy($this->imgNew);
  55. imagedestroy($this->img);
  56. }
  57.  
  58. /*
  59. * Creating the effect of smooth
  60. */
  61. public function createSmooth()
  62. {
  63. $this->img = imagecreatefromjpeg($this->name);
  64. imagefilter($this->img, IMG_FILTER_SMOOTH, -5);
  65. imagejpeg($this->img, 'smooth_'.$this->name, 100);
  66. imagedestroy($this->img);
  67. }
  68.  
  69. /*
  70. * Creating the effect of brightness
  71. */
  72. public function createBrightness()
  73. {
  74. $this->img = imagecreatefromjpeg($this->name);
  75. imagefilter($this->img, IMG_FILTER_BRIGHTNESS, 100);
  76. imagejpeg($this->img, 'brightness_'.$this->name, 100);
  77. imagedestroy($this->img);
  78. }
  79.  
  80. /*
  81. * Positions for watermark (from 0 to 8)
  82. */
  83. public function getPositionsForWatermark()
  84. {
  85. switch ($this->watermarkPosition)
  86. {
  87. case 0:
  88. $positionX = (imagesx($this->img) / 2) - (imagesx($this->watermark) / 2);
  89. $positionY = (imagesy($this->img) / 2) - (imagesy($this->watermark) / 2);
  90. break;
  91. case 1:
  92. $positionX = 0;
  93. $positionY = 0;
  94. break;
  95. case 2:
  96. $positionX = imagesx($this->img) - imagesx($this->watermark);
  97. $positionY = 0;
  98. break;
  99. case 3:
  100. $positionX = (imagesx($this->img) - imagesx($this->watermark)) - 5;
  101. $positionY = (imagesy($this->img) - imagesy($this->watermark)) - 5;
  102. break;
  103. case 4:
  104. $positionX = 0;
  105. $positionY = imagesy($this->img) - imagesy($this->watermark);
  106. break;
  107. case 5:
  108. $positionX = ((imagesy($this->img) - imagesx($this->watermark)) / 2);
  109. $positionY = 0;
  110. break;
  111. case 6:
  112. $positionX = imagesx($this->img) - imagesx($this->watermark);
  113. $positionY = (imagesy($this->img) / 2) - (imagesy($this->watermark) / 2);
  114. break;
  115. case 7:
  116. $positionX = ((imagesx($this->img) - imagesx($this->watermark)) / 2);
  117. $positionY = imagesy($this->img) - imagesy($this->watermark);
  118. break;
  119. case 8:
  120. $positionX = 0;
  121. $positionY = (imagesy($this->img) / 2) - (imagesy($this->watermark) / 2);
  122. break;
  123. default:
  124. $positionX = (imagesx($this->img) / 2) - (imagesx($this->watermark) / 2);
  125. $positionY = (imagesy($this->img) / 2) - (imagesy($this->watermark) / 2);
  126. break;
  127. }
  128.  
  129. return array('x' => $positionX, 'y' => $positionY);
  130. }
  131.  
  132. /*
  133. * Creating the watermark with image
  134. */
  135. public function createWatermarkImage($fileNameWatermark, $position = 0)
  136. {
  137. $this->img = imagecreatefromjpeg($this->name);
  138. $this->watermark = imagecreatefrompng($fileNameWatermark);
  139.  
  140. $this->watermarkPosition = $position;
  141. $positions = $this->getPositionsForWatermark();
  142.  
  143. imagecopy($this->img, $this->watermark, $positions['x'], $positions['y'], 0, 0, imagesx($this->watermark), imagesy($this->watermark));
  144. imagejpeg($this->img, 'watermark_'.$this->name, 100);
  145. imagedestroy($this->img);
  146. }
  147.  
  148. /*
  149. * Positions for text (from 0 to 8)
  150. */
  151. public function getPositionsForText()
  152. {
  153. switch ($this->watermarkPosition)
  154. {
  155. case 0:
  156. $positionX = (imagesx($this->img) / 2) - ($this->watermarkPositionTextWidth / 2) - $this->watermarkPositionTextWidth * strlen($this->watermarkText) / 2;
  157. $positionY = imagesy($this->img) / 2;
  158. break;
  159. case 1:
  160. $positionX = 0;
  161. $positionY = $this->watermarkPositionTextHeight;
  162. break;
  163. case 2:
  164. $positionX = imagesx($this->img) - $this->watermarkPositionTextWidth * strlen($this->watermarkText);
  165. $positionY = $this->watermarkPositionTextHeight;
  166. break;
  167. case 3:
  168. $positionX = imagesx($this->img) - $this->watermarkPositionTextWidth * strlen($this->watermarkText);
  169. $positionY = imagesy($this->img) - $this->watermarkPositionTextHeight;
  170. break;
  171. case 4:
  172. $positionX = 0;
  173. $positionY = imagesy($this->img) - $this->watermarkPositionTextHeight;
  174. break;
  175. case 5:
  176. $positionX = (imagesx($this->img) / 2) - ($this->watermarkPositionTextWidth / 2) - $this->watermarkPositionTextWidth * strlen($this->watermarkText) / 2;
  177. $positionY = $this->watermarkPositionTextHeight;
  178. break;
  179. case 6:
  180. $positionX = imagesx($this->img) - $this->watermarkPositionTextWidth * strlen($this->watermarkText);
  181. $positionY = imagesy($this->img) / 2;
  182. break;
  183. case 7:
  184. $positionX = (imagesx($this->img) / 2) - ($this->watermarkPositionTextWidth / 2) - $this->watermarkPositionTextWidth * strlen($this->watermarkText) / 2;
  185. $positionY = imagesy($this->img) - $this->watermarkPositionTextHeight;
  186. break;
  187. case 8:
  188. $positionX = 0;
  189. $positionY = imagesy($this->img) / 2;
  190. break;
  191. default:
  192. $positionX = (imagesx($this->img) / 2) - ($this->watermarkPositionTextWidth / 2) - $this->watermarkPositionTextWidth * strlen($this->watermarkText) / 2;
  193. $positionY = imagesy($this->img) / 2;
  194. break;
  195. }
  196.  
  197. return array('x' => $positionX, 'y' => $positionY);
  198. }
  199.  
  200. /*
  201. * Creating the watermark with text
  202. */
  203. public function createWatermarkText($text, $size = 15, $position = 0)
  204. {
  205. $this->img = imagecreatefromjpeg($this->name);
  206. $this->watermarkText = $text;
  207. $this->watermarkTextSize = $size;
  208.  
  209. $this->watermarkPosition = $position;
  210. $this->watermarkPositionTextWidth = imagefontwidth(40);
  211. $this->watermarkPositionTextHeight = imagefontheight(40);
  212. $positions = $this->getPositionsForText();
  213.  
  214. $image = imagecreate(100, 100);
  215. $white = imagecolorallocate($image, 255, 255, 255);
  216.  
  217. imagettftext($this->img, $this->watermarkTextSize, 0, $positions['x'], $positions['y'], $white, 'arial.ttf', $this->watermarkText);
  218. imagejpeg($this->img, 'text_'.$this->name, 100);
  219. imagedestroy($this->img);
  220. }
  221. }
  222.  
  223.  
  224. /*
  225. * Example usage
  226. */
  227.  
  228. $fileName = '20120229028.jpg';
  229. $maniPicture = new ManiPicture($fileName);
  230.  
  231. $widthThumb = 150;
  232. $heightThumb = 150;
  233. $maniPicture->createThumb($widthThumb, $heightThumb);
  234.  
  235. $maniPicture->createSmooth();
  236.  
  237. $maniPicture->createBrightness();
  238.  
  239. $fileNameWatermark = 'stamp009.png';
  240. $positionWatermark = 4;
  241. $maniPicture->createWatermarkImage($fileNameWatermark, $positionWatermark);
  242.  
  243. $textWatermark = 'przykladowy tekst';
  244. $sizeWatermark = 15;
  245. $positionWatermark = 6;
  246. $maniPicture->createWatermarkText($textWatermark, $sizeWatermark, $positionWatermark);
b4rt3kk
Z tego co widzę to właściwości width i height dla thumba są niepotrzebne (bo są używane jedynie wewnątrz funkcji). Równie dobrze mogłyby pozostać lokalnymi zmiennymi.

  1. public function createThumb($widthThumb, $heightThumb)
  2. {
  3. $this->widthThumb = $widthThumb;
  4. $this->heightThumb = $heightThumb;


Za to używany wielokrotnie

  1. $this->img = imagecreatefromjpeg($this->name);


mógłby trafić do konstruktora, by nie było konieczności nadawania wartości (i to zresztą tej samej) przy wywołaniu większości funkcji.

I ogólnie niektóre właściwości, które używasz tylko wewnątrz jednej funkcji i nigdzie więcej nie są wykorzystywane, mogłyby być tylko lokalnymi zmiennymi funkcji.

in5ane
Dziękuje z opinie, dostosuję się do nich. Ogólnie OOP w miarę dobrze mi się przyswaja? Proszę jeszcze o inne opinie.
redeemer
Nie za bardzo mam teraz czas się przyjrzeć na kod, ale rzuciło mi się w o czy, że namieszałeś z dokumentacją. M.in. tag @param służy do opisywania argumentów metod/funkcji. Jeszcze zamiast liczb w $watermarkPosition mógłbyś użyć stałych, które bardziej by opisywały co one znaczą.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.