Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit d9c4b4e

Browse files
author
Flo Faber
committed
Fixed #19. New DB::get_picture function
1 parent ab77e8d commit d9c4b4e

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

src/DB.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,28 +205,55 @@ public function read_comments(string $uri)
205205

206206

207207
/**
208-
* Returns a picture of `$uri` by reading embedded pictures from binary tags.
208+
* Returns data and content of a picture embedded in `$uri`.
209+
*
210+
* If `$uri` is not found the function returns `false`.
211+
*
212+
* If `$uri` is found but does not contain a picture the function will return `[ "size" => 0, "type" => null, "binary" => null ]`.
213+
*
214+
* If `$uri` is found and contains a picture the returned array will contain information about the picture and
215+
*
216+
* if `$include_binary` is `true` the array also contains the picture itself.
209217
* @param string $uri Song URI.
210-
* @return false|string `false` on failure otherwise `string` containing either the picture or an empty string in case the file does not contain a picture.
218+
* @param bool $include_binary If `true` the array's `binary`-item will contain the picture. If `false` the array's `binary`-item is `null`.
219+
* @return array|false Returns `false` on failure and an associative array containing `size`,`type` and (optionally) `binary` on success.
211220
*/
212-
public function read_picture(string $uri)
221+
public function get_picture(string $uri, bool $include_binary = true)
213222
{
223+
214224
$offset = 0;
215225
$binary_data = "";
216226
do{
217227

218-
$aa = $this->mphpd->cmd("readpicture", [$uri, $offset]);
219-
if($aa === false){ return false; }
220-
if(!isset($aa["size"])){ return ""; }
228+
$picture_data = $this->mphpd->cmd("readpicture", [$uri, $offset]);
229+
if($picture_data === false){ return false; }
230+
if(!isset($picture_data["size"])){ return [ "size" => 0, "type" => null, "binary" => null ]; }
221231

222-
$binary_size = $aa["size"];
232+
$size = $picture_data["size"];
233+
$type = $picture_data["type"];
223234

224235
$offset = $offset + $this->mphpd->get_binarylimit();
225-
$binary_data .= $aa["binary_data"];
236+
$binary_data .= $picture_data["binary_data"];
226237

227-
}while($offset < $binary_size);
238+
}while($offset < $size && $include_binary === true);
228239

229-
return $binary_data;
240+
return [
241+
"size" => $size,
242+
"type" => $type,
243+
"binary" => ($include_binary ? $binary_data : null)
244+
];
245+
246+
}
247+
248+
249+
/**
250+
* Returns a picture of `$uri` by reading embedded pictures from binary tags.
251+
* @param string $uri Song URI.
252+
* @return false|string `false` on failure otherwise `string` containing either the picture or an empty string in case the file does not contain a picture.
253+
*/
254+
public function read_picture(string $uri)
255+
{
256+
return $this->get_picture($uri, true)["binary"] ?? false;
230257
}
231258

232259

tests/DBTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,47 @@ public function testCount()
8282
public function testRead_picture()
8383
{
8484
$this->assertNotEmpty($this->mphpd->db()->read_picture("test-song1.mp3"));
85+
$this->assertEmpty($this->mphpd->db()->read_picture("test-song3.mp3"));
86+
$this->assertFalse($this->mphpd->db()->read_picture("test-song-non-existent.mp3"));
87+
}
88+
89+
public function testGet_picture()
90+
{
91+
$ret = $this->mphpd->db()->get_picture("test-song1.mp3", true);
92+
$this->assertArrayHasKey("size", $ret);
93+
$this->assertArrayHasKey("type", $ret);
94+
$this->assertArrayHasKey("binary", $ret);
95+
96+
$this->assertNotEmpty($ret["size"]);
97+
$this->assertNotEmpty($ret["type"]);
98+
$this->assertNotEmpty($ret["binary"]);
99+
100+
101+
102+
$ret = $this->mphpd->db()->get_picture("test-song1.mp3", false);
103+
$this->assertArrayHasKey("size", $ret);
104+
$this->assertArrayHasKey("type", $ret);
105+
$this->assertArrayHasKey("binary", $ret);
106+
107+
$this->assertNotEmpty($ret["size"]);
108+
$this->assertNotEmpty($ret["type"]);
109+
$this->assertNull($ret["binary"]);
110+
111+
112+
113+
$ret = $this->mphpd->db()->get_picture("test-song3.mp3", false);
114+
$this->assertArrayHasKey("size", $ret);
115+
$this->assertArrayHasKey("type", $ret);
116+
$this->assertArrayHasKey("binary", $ret);
117+
118+
$this->assertEquals(0, $ret["size"]);
119+
$this->assertEmpty($ret["type"]);
120+
$this->assertNull($ret["binary"]);
121+
122+
123+
$ret = $this->mphpd->db()->get_picture("test-song-non-existent.mp3", true);
124+
$this->assertFalse($ret);
125+
85126
}
86127

87128
// ToDo

0 commit comments

Comments
 (0)