Reputation: 705
I'm trying to echo images using php, my images are stored in array , so I can't skip empty value, my php code is
$images = 'imgs/001.jpg,imgs/001.gif,imgs/002.png,imgs/003.jpg,imgs/004.jpg,
imgs/005.jpg,imgs/006.png,imgs/007.png,';
$expl = explode (',', $images);
foreach ($expl as $path){
if (!empty($path) || getimagesize($path)!== false){
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
echo '<img src="'.$base64.'" width="300" height="300">' . '<br>';
var_dump (getimagesize($path));
}
}
because of the last comma in my $images I get error
Warning: getimagesize(): Filename cannot be empty in I:\zzerver\Uwamp -port 84\uw-cms-p8585\www\test\php\images\dataimage-explode1-1.php on line 9
I tried : if (!empty($path) || getimagesize($path)!== false)
but seems that not working, I get the same error
Upvotes: 0
Views: 272
Reputation: 41810
In addition to fixing the logic error, you can simplify your condition
if ($path && getimagesize($path) !== false) { ...
empty
is overkill as it includes an isset
check that is not necessary considering $path
will always set by the foreach loop. If $path
is an empty string it will evaluate as false.
Upvotes: 0
Reputation: 34914
@u_mulder is right but you should also use trim($images,',')
to remove unnecessary ,
comma at last
$expl = explode (',', trim($images,','));
Read about trim() and one example
Upvotes: 1
Reputation: 54831
Problem is here
if (!empty($path) || getimagesize($path)!== false) {
When $path
is empty, php checks second condition getimagesize($path)
, which gives you a warning, because you check getimagesize('')
.
So, the correct rule is:
if (!empty($path) && getimagesize($path) !== false) {
Upvotes: 3
Reputation: 1909
I can suggest do this
$expl = preg_split('@,@', $images, null, PREG_SPLIT_NO_EMPTY);
This will remove all empty entries automatically
Upvotes: 1