ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MemoryDrawing.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use GdImage;
7 
9 {
10  // Rendering functions
11  const RENDERING_DEFAULT = 'imagepng';
12  const RENDERING_PNG = 'imagepng';
13  const RENDERING_GIF = 'imagegif';
14  const RENDERING_JPEG = 'imagejpeg';
15 
16  // MIME types
17  const MIMETYPE_DEFAULT = 'image/png';
18  const MIMETYPE_PNG = 'image/png';
19  const MIMETYPE_GIF = 'image/gif';
20  const MIMETYPE_JPEG = 'image/jpeg';
21 
27  private $imageResource;
28 
35 
41  private $mimeType;
42 
48  private $uniqueName;
49 
53  public function __construct()
54  {
55  // Initialise values
56  $this->renderingFunction = self::RENDERING_DEFAULT;
57  $this->mimeType = self::MIMETYPE_DEFAULT;
58  $this->uniqueName = md5(mt_rand(0, 9999) . time() . mt_rand(0, 9999));
59 
60  // Initialize parent
61  parent::__construct();
62  }
63 
64  public function __destruct()
65  {
66  if ($this->imageResource) {
67  imagedestroy($this->imageResource);
68  $this->imageResource = null;
69  }
70  }
71 
72  public function __clone()
73  {
74  parent::__clone();
75  $this->cloneResource();
76  }
77 
78  private function cloneResource(): void
79  {
80  if (!$this->imageResource) {
81  return;
82  }
83 
84  $width = imagesx($this->imageResource);
85  $height = imagesy($this->imageResource);
86 
87  if (imageistruecolor($this->imageResource)) {
88  $clone = imagecreatetruecolor($width, $height);
89  if (!$clone) {
90  throw new Exception('Could not clone image resource');
91  }
92 
93  imagealphablending($clone, false);
94  imagesavealpha($clone, true);
95  } else {
96  $clone = imagecreate($width, $height);
97  if (!$clone) {
98  throw new Exception('Could not clone image resource');
99  }
100 
101  // If the image has transparency...
102  $transparent = imagecolortransparent($this->imageResource);
103  if ($transparent >= 0) {
104  $rgb = imagecolorsforindex($this->imageResource, $transparent);
105  if ($rgb === false) {
106  throw new Exception('Could not get image colors');
107  }
108 
109  imagesavealpha($clone, true);
110  $color = imagecolorallocatealpha($clone, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']);
111  if ($color === false) {
112  throw new Exception('Could not get image alpha color');
113  }
114 
115  imagefill($clone, 0, 0, $color);
116  }
117  }
118 
119  //Create the Clone!!
120  imagecopy($clone, $this->imageResource, 0, 0, 0, 0, $width, $height);
121 
122  $this->imageResource = $clone;
123  }
124 
130  public function getImageResource()
131  {
132  return $this->imageResource;
133  }
134 
142  public function setImageResource($value)
143  {
144  $this->imageResource = $value;
145 
146  if ($this->imageResource !== null) {
147  // Get width/height
148  $this->width = imagesx($this->imageResource);
149  $this->height = imagesy($this->imageResource);
150  }
151 
152  return $this;
153  }
154 
160  public function getRenderingFunction()
161  {
163  }
164 
172  public function setRenderingFunction($value)
173  {
174  $this->renderingFunction = $value;
175 
176  return $this;
177  }
178 
184  public function getMimeType()
185  {
186  return $this->mimeType;
187  }
188 
196  public function setMimeType($value)
197  {
198  $this->mimeType = $value;
199 
200  return $this;
201  }
202 
208  public function getIndexedFilename()
209  {
210  $extension = strtolower($this->getMimeType());
211  $extension = explode('/', $extension);
212  $extension = $extension[1];
213 
214  return $this->uniqueName . $this->getImageIndex() . '.' . $extension;
215  }
216 
222  public function getHashCode()
223  {
224  return md5(
225  $this->renderingFunction .
226  $this->mimeType .
227  $this->uniqueName .
228  parent::getHashCode() .
229  __CLASS__
230  );
231  }
232 }
getIndexedFilename()
Get indexed filename (using image index).
setRenderingFunction($value)
Set rendering function.