PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Dm/Image.php

https://github.com/demouth/DmImage
PHP | 296 lines | 139 code | 32 blank | 125 comment | 10 complexity | 6235203cd9e2068163df0d96440463f9 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Dm_Image
  4. * 画像を表すクラス。
  5. *
  6. * @example
  7. * Example 1:
  8. * $image = new Dm_Image(400,300, 0xFF0099FF);
  9. * $image->display();
  10. *
  11. * Example 2:
  12. * $image = new Dm_Image(400,300, 0xFF0099FF);
  13. * $image->graphics
  14. * ->lineStyle(1,0xFFFFFFFF)
  15. * ->fillStyle(0)
  16. * ->drawRect(10, 10, 190, 140)
  17. * ->drawCircle(100, 225, 50)
  18. * ->drawEllipse(300, 75, 150, 100)
  19. * ->drawPie(300, 225, 150, 100, 0, 135);
  20. * $image->display();
  21. *
  22. * Example 2:
  23. * $image = new Dm_Image(400,300, 0xFF0099FF);
  24. * $image->textGraphics
  25. * ->setColor(0xFFFFFFFF)
  26. * ->setFontSize(30)
  27. * ->textTo(100, 100, 'Hello world.');
  28. * $image->display();
  29. *
  30. * Example 2:
  31. * $image = new Dm_Image_File('/path/to/image/image.jpg');
  32. * $filter = new Dm_Image_Filter_InstagramLoFi(300,1);
  33. + $image->applyFilter($filter);
  34. * $image->display();
  35. *
  36. * @author demouth.net
  37. */
  38. class Dm_Image
  39. {
  40. /**
  41. * @var Dm_Image_Graphic_Shape
  42. */
  43. public $graphics;
  44. /**
  45. * @var Dm_Image_Graphic_Text
  46. */
  47. public $textGraphics;
  48. /**
  49. * @var resource
  50. */
  51. protected $_imageResource;
  52. /**
  53. * @var int
  54. */
  55. protected $_width;
  56. /**
  57. * @var int
  58. */
  59. protected $_height;
  60. /**
  61. *
  62. * @var string
  63. */
  64. protected $_tempDirPath = "";
  65. /**
  66. * コンストラクタ。
  67. * @param int 画像幅(px)
  68. * @param int 画像高さ(px)
  69. * @param int 背景画像色 例:0x0099FF
  70. */
  71. public function __construct($width , $height , $backgroundColor=0)
  72. {
  73. $imageResource = imagecreatetruecolor($width, $height);
  74. $this->graphics = new Dm_Image_Graphic_Shape($imageResource , $width , $height);
  75. $this->textGraphics = new Dm_Image_Graphic_Text($imageResource , $width , $height);
  76. $this->_imageResource = $imageResource;
  77. $this->_width = $width;
  78. $this->_height = $height;
  79. //背景色設定(透過を有効化)
  80. imagesavealpha($imageResource,true);
  81. // imagealphablending($imageResource, false);
  82. $colorId = Dm_Color::argb($backgroundColor)->imagecolorallocatealpha($imageResource);
  83. // imagefilledrectangle($imageResource, 0, 0, $width-1, $height-1, $colorId);
  84. imagefill($imageResource, 0, 0, $colorId);
  85. }
  86. /**
  87. * @return resource
  88. */
  89. public function getImageResource()
  90. {
  91. return $this->_imageResource;
  92. }
  93. public function setImageResource($resource)
  94. {
  95. $this->_imageResource = $resource;
  96. $this->_width = imagesx($resource);
  97. $this->_height = imagesy($resource);
  98. return $this;
  99. }
  100. /**
  101. * Return the width of the image.
  102. * @return int
  103. */
  104. public function getWidth()
  105. {
  106. return $this->_width;
  107. }
  108. /**
  109. * Return the height of the image.
  110. * @return int
  111. */
  112. public function getHeight()
  113. {
  114. return $this->_height;
  115. }
  116. /**
  117. * Output a PNG image to either the browser.
  118. * The raw image stream will be outputted directly.
  119. * @return bool
  120. */
  121. public function display($type='png', $quality=null)
  122. {
  123. return $this->outputTo(null, $type, $quality);
  124. }
  125. /**
  126. * Copy and merge part of an image
  127. * @param DmImage
  128. * @param int x-coordinate of source point.
  129. * @param int y-coordinate of source point.
  130. * @param int Source width.
  131. * @param int Source height.
  132. * @return bool Returns TRUE on success or FALSE on failure.
  133. */
  134. public function draw(Dm_Image $image,$x=0,$y=0,$width=null,$height=null)
  135. {
  136. $srcImageResource = $image->getImageResource();
  137. if (is_null($width)) $width = $image->getWidth();
  138. if (is_null($height)) $height = $image->getHeight();
  139. return imagecopy(
  140. $this->getImageResource(),
  141. $srcImageResource,
  142. $x,
  143. $y,
  144. 0,
  145. 0,
  146. $width,
  147. $height
  148. );
  149. }
  150. /**
  151. * The path to save the file to.
  152. * @param string
  153. * @param string filetype 'png' 'jpg' 'jpeg' 'gif'
  154. * @return bool
  155. */
  156. public function saveTo($path , $type='png', $quality=null)
  157. {
  158. if (!$path) return false;
  159. return $this->outputTo($path, $type, $quality);
  160. }
  161. /**
  162. *
  163. *
  164. * @return
  165. */
  166. public function startDownload($filename='image', $type='png', $quality=null)
  167. {
  168. header('Content-Type: application/octet-stream');
  169. header('Content-disposition: attachment; filename="'.$filename.'.'.$type.'"');
  170. return $this->outputTo(null, $type, $quality);
  171. }
  172. /**
  173. * 画像をブラウザあるいはファイルに出力する。
  174. * $pathがNULLならブラウザ出力する。
  175. * @param string or null nullならブラウザ出力する
  176. * @param string filetype 'png' 'jpg' 'jpeg' 'gif'
  177. * @return bool
  178. */
  179. protected function outputTo($path , $type='png', $quality=null)
  180. {
  181. switch ($type) {
  182. case 'jpg':
  183. case 'jpeg':
  184. if (is_null($quality)) $quality = 75;
  185. if (is_null($path)) header('Content-Type: image/jpeg');
  186. imagejpeg($this->_imageResource , $path , $quality );
  187. break;
  188. case 'gif':
  189. if (is_null($path)) header('Content-Type: image/gif');
  190. imagegif($this->_imageResource , $path );
  191. break;
  192. case 'png':
  193. default:
  194. if (is_null($quality)) $quality = 5;
  195. if (is_null($path)) header('Content-Type: image/png');
  196. imagepng($this->_imageResource , $path , $quality );
  197. break;
  198. }
  199. return true;
  200. }
  201. /**
  202. * Destroy an image.
  203. * @return void
  204. */
  205. public function destroy()
  206. {
  207. imagedestroy($this->_imageResource);
  208. $this->graphics->destroy();
  209. $this->graphics = null;
  210. $this->textGraphics->destroy();
  211. $this->textGraphics = null;
  212. }
  213. /**
  214. * Return data scheme URI.
  215. *
  216. * @example
  217. * data:image/png;base64,iVBORw0KGgoAAAANSUhEU...
  218. * @return string
  219. */
  220. public function toDataSchemeURI()
  221. {
  222. $md5 = md5(microtime(1).rand(10000, 99999));
  223. $filePath = $this->tempDirPath() . DIRECTORY_SEPARATOR . "temp".$md5.".png";
  224. $this->saveTo($filePath);
  225. $uri = 'data:' . mime_content_type($filePath) . ';base64,';
  226. $uri .= base64_encode(file_get_contents($filePath));
  227. unlink($filePath);
  228. return $uri;
  229. }
  230. protected function tempDirPath()
  231. {
  232. if($this->_tempDirPath===""){
  233. return dirname(__FILE__).DIRECTORY_SEPARATOR.'Image'.DIRECTORY_SEPARATOR.'temp';
  234. }else{
  235. return $this->_tempDirPath;
  236. }
  237. }
  238. /**
  239. * TEMP画像の保存先を指定する。
  240. * このメソッドを呼ばない場合、このライブラリ配置先と同階層に作成されます。
  241. *
  242. * @param string TEMP画像保存先のフルパス
  243. * @return void
  244. */
  245. public function setTempDirPath($path)
  246. {
  247. $this->_tempDirPath = $path;
  248. }
  249. public function applyFilter($filter)
  250. {
  251. $filteredResouce = $filter->execute($this);
  252. $this->_imageResource = $filteredResouce;
  253. $this->_width = imagesx($filteredResouce);
  254. $this->_height = imagesy($filteredResouce);
  255. return $this;
  256. }
  257. public function applyFilters(array $filters)
  258. {
  259. foreach ($filters as $filter) {
  260. $this->applyFilter($filter);
  261. }
  262. return $this;
  263. }
  264. }