PHP dealing with Invalid argument and expects parameter's - php

Good Evening, i am trying to teaching myself php, as i go and decided to try build something for our LAN server at work.
i have the following code that works and displays the images from a directory, i am using this as i am building a booking in system for work and naming the image by the job number.
for this i'm naming the images test but having a issue if there inst any images.
<?php
$directory = "saved_images/";
$images = glob("" . $directory . "test*.jpg");
$imgs = '';
foreach($images as $image){ $imgs[] = "$image"; }
$imgs = array_slice($imgs, 0, 20);
$result = count($imgs);
if ($result == 0)
{
$img="No Photos";
echo $img;
}
} else {
foreach ($imgs as $img) {
echo "<img src='$img' /> ";
}}
?>
the issue is, if there is not any photos i would like it to echo no photos instead of the following errors
array_slice() expects parameter 1 to be array,
refering to this line
$imgs = array_slice($imgs, 0, 20);
and
Invalid argument supplied for foreach()
refering to this line
foreach ($imgs as $img)
I have seen someone with a similar problem, but sadly they was advised to ignore the issue and turn off the error reporting, which didn't seem right, I am only asking as this inst causing any problem for the rest of the project and like to know how to fix this problem so if i encounter it again i know what to do.

All you need to do is set $imgs as an array (not a string), and check whether $imgs is empty or not. While you're at it, you will want to check if $images is empty or not, too.
$directory = "saved_images/";
$images = glob("" . $directory . "test*.jpg");
// since the entire script relies on $images not being empty,
// we should check for that to be sure before moving on
// you can also test for glob() returning FALSE on error, if you anticipate that it might
if ( ! empty($images) ) {
$imgs = []; // this should be set as an array, not a string
foreach ($images as $image)
{
$imgs[] = $image;
}
if ( empty($imgs) ) {
echo 'No Photo';
}
else {
$imgs = array_slice($imgs, 0, 20);
$result = count($imgs);
foreach ($imgs as $img)
{
echo "<img src='$img'> ";
}
}
}
else {
echo 'No images in ' . $directory . ';
}

Why are you initializing $imgs as a string?
$imgs = '';
and then treating it as an array?
foreach($images as $image){ $imgs[] = "$image"; }
If you'd initialize it as an array, e.g.
$imgs = array();
Then even if the foreach doesn't add anythign to the array, it'll still be an (empty) array when you pass it into array_slice.
Basically, you create a pizza, then wonder why PHP is complaining that it's not a chocolate cake.

wrap
$imgs = array_slice($imgs, 0, 20);
inside an if(isset()) statement, like so
if(isset($imgs)){
$imgs = array_slice($imgs, 0, 20);
}
If it still doesn't work add
&& count($imgs) > 0
to it
Edit:
And what #MarcB said, replace $imgs = ''; with $imgs = array();

You can use the php function is_array($myArrayVar) (PHP Net) to test if $img is an array before the line where you are using array_slice($imgs, 0, 20). Something like:
if (is_array($imgs)){
// $imgs is an array
} else {
//$imgs is not an array
}

Related

How to check if namespace element exists in PHP

I'm trying to check if the element media:content exists so that a thumbnail image can be shown from an rss feed but not sure how to validate it's existence.
I can get the media:content url and show the image fine but checking to see if it exists isn't working out. I've tried isset and defined but I'm clearly doing something wrong.
Main line i'm concerned about below is:
if(defined($item->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content')->item(0)->getAttribute('url')))
<?php
$feed = new DOMDocument();
$feed->load('http://rss.cnn.com/rss/cnn_topstories.rss');
$json = array();
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
$json['item'] = array();
foreach($items as $item) {
$title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$text = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
if(defined($item->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content')->item(0)->getAttribute('url'))){
$image = $item->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content')->item(0)->getAttribute('url');
}
else{
$image = '';
}
echo $image;
}
?>
When you use getElementsByTagName, this always returns a DOMNodeList. The main thing is to check if the node list has 0 elements.
So rather than defined() or isset(), use ->length...
$nodes=$domDocument->getElementsByTagName('book') ;
if ($nodes->length==0) {
// no results
}
( Example from http://php.net/manual/en/domdocument.getelementsbytagname.php)
Nigel was right, using ->length is the way to do it.
if($item->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content')->length > 0){
$image = $item->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content')->item(0)->getAttribute('url');
}

Foreach loop for fotorama

im trying to use a foreach loop with the plugin fotorama.
What im trying to do is load one half sized image for the main gallery image. Which i have working in a foreach, but i want to use a full image for the data-full tag but i cant get it to work.
This is the working code.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
</div>
this is what im trying to do.
<div class="fotorama"
data-allowfullscreen="native"
data-nav="thumbs"
data-fit="scaledown"
data-width="100%"
data-height="100%"
data-arrows="true"
data-click="true"
data-swipe="true">
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$fullImgs = "<img data-full=".$image2." src=".$image." /><br />";
foreach($fullImgs as $fullImg) {
echo $fullImg;
}
?>
</div>
thanks in advanced guys
Try this:
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
$array = array_merge($images, $images2);
// Supossing both array have same length
$length = count($images);
for($i = 0; $j = $length; $i < $length; $i++, $j++) {
echo '<img data-full=".$images2[$j]." src=".$images[$i]." /><br />';
}
I think what you want is this:
<?php
$dirname = "admin/image-upload/uploads/";
$images = glob($dirname."*.*");
$dirname2 = "admin/image-upload/full/";
$images2 = glob($dirname2."*.*");
//assuming $images and $images2 are the same length...
foreach ($images as $k => $v) {
echo "<img data-full=".$images2[$k]." src=".$v." /><br />";
}
?>
Haven't tested, but should give the idea...
Your code won't work like this. If I understood you correctly you have the same image in big and small format in two different directories. To show them as pair you need to make a connection between those two files - because how should your script know which images belong together? You could use a database, but in your case I think it is easier to make a connection in the filename. For example, name the pictures in the directory for the small images
Image_7263.png
Image_0172.png
And so on. For the big images you simply append _BIG to the end of the name.
Then you use your foreach loop to loop through the directory for the small images. For each image in there, you append _BIG to the end of the filename and include it from the directory for the big ones.
$smallImages = glob ("/path/to/small/images/*.*");
foreach ($smallImages as $img)
{
$name = substr ($img, 0, strlen ($img)-4); //Remove the .png or .jpg
$bigImg = "/path/to/big/images/".name."_BIG.jpg"; // or whatever image type you are using
echo "<img data-full=\"".$bigImg."\" src=\"".$img."\" />
}

PHP & Lightbox (href in for loop links to last file of lightbox set)

EDITED: Debugged some of the code but issue persists:
The issue i'm having with the following code is that the link always takes me to the last image in the set. I've tried reversing the array of photo's but it had no effect. Any help would be appreciated.
Thanks.
<?php
$dir = 'pic';
$max_albums=9;
$albums = array_diff(scandir($dir),array('..', '.', 'thumbs'));
foreach ($albums as $album) {
$albumdir = $dir.'/'.$album;
$coverdir = $albumdir.'/thumbs';
$thumbs = array_diff(scandir($coverdir),array('..', '.'));
//re-index $thumbs
$thumbs = array_values($thumbs);
//1 random cover image from each album
$rnd_min = 0;
$rnd_max = count($thumbs)-1;
$rnd_i = mt_rand($rnd_min, $rnd_max);
$covers = $thumbs[$rnd_i];
//re-index $covers
echo $rnd_i.'<br>';
//populate hrefs
$photos = array_diff(scandir($albumdir),array('..', '.', 'thumbs'));
//re-index $photos
$photos = array_values($photos);
foreach ($photos as $photo) {
echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'">';
}
//display cover images
echo '<img src="'.$coverdir.'/'.$covers.'" class="img-responsive"></a>';
}
?>
hmmm try reversing scandir
$photos = array_diff(scandir($albumdir,1),array('..', '.', 'thumbs'));
Not a very elegant solution but it seems to work:
<?php
$dir = 'pic';
$max_albums=9;
$albums = array_diff(scandir($dir),array('..', '.', 'thumbs'));
foreach ($albums as $album) {
$albumdir = $dir.'/'.$album;
$coverdir = $albumdir.'/thumbs';
$thumbs = array_diff(scandir($coverdir),array('..', '.'));
//re-index $thumbs
$thumbs = array_values($thumbs);
//1 random cover image from each album
$rnd_min = 0;
$rnd_max = count($thumbs)-1;
$rnd_i = mt_rand($rnd_min, $rnd_max);
$covers = $thumbs[$rnd_i];
//populate hrefs
$photos = array_diff(scandir($albumdir),array('..', '.', 'thumbs'));
//re-index $photos
$photos = array_values($photos);
$countphoto = 0;
foreach ($photos as $photo) {
if ($countphoto==0) {
echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'">'."\n";
//display cover images
echo '<img src="'.$coverdir.'/'.$covers.'" class="img-responsive"></a>';
}
else {
echo ''."\n";
}
$countphoto++;
}
}
?>
So i solved this issue with a simple if $countphotos==0 statement within the foreach ($photos as $photo) loop, if 0, it would display the thumbnail, else it would just output anchors to the other imgs.
I've also done away with the random thumbnail as cover, and just pulled the first thumbnail of the set.
I now another issue, but I will make a separate thread for that.
Thanks!

php directory iterator, shuffle images

<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach ($dir as $file)
{
$images [] = array (
$file->getPathname() . "\n";
$file->getFilename(). "\n";
);
}
?>
shuffle($images);
Can you help me with the code above? I want to add images to an array using DirectoryIterator, and then shuffle images to generate randomized images. Thank you for your valuable inputs.
This depends on what you are doing with the images, and what path your in at the time, but the following will put their full pathname into an Array.
<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach($dir as $file){
$images[] = $file->getPathname();
}
shuffle($images); $imgs = '';
foreach($images as $v){
$imgs .= "<img scr='http://{$_SERVER['SERVER_NAME']}/$v' alt='randomImage' />";
}
//put the below wherever you want in your code
echo $imgs;
?>

having problems passing array values

I'm building a PHP program that basically grabs only image links from my twitter feed and displays them on a page, I have 3 components that I have set up that all work fine on their own.
The first component is the twitter oauth component which grabs the tweet text and creates an array, this works fine by itself.
The second is a function that processes the tweets and only returns tweets that contain image links, this as well works fine.
The program breaks down during the third section when the links are processed and an image is displayed, I had no issues running this on its own and from my attempts to trouble shoot it appears that it breaks down at the $images(); array, as that array is empty.
I'm sure I've made a silly mistake but I've been trying to find this for over a day now and can't seem to fix it. Any help would be great! Thanks guys!
code:
<?php
if ($result['socialorigin']== "twitter"){
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($result['oauthtoken'], $result['oauthsecret']);
$tweets = $twitterObj->get('/statuses/home_timeline.json',array('count'=>'200'));
$all_tweets = array();
$hosts = "lockerz|yfrog|twitpic|tumblr|mypict|ow.ly|instagr";
foreach($tweets as $tweet) {
$twtext = $tweet->text;
if(preg_match("~http://($hosts)~", $twtext)){
preg_match_all("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", $twtext, $matches, PREG_PATTERN_ORDER);
foreach($matches[0] as $key2 => $link){
array_push($all_tweets,"$link");
}
}
}
function height_compare($a1, $b1)
{
if ($a1 == $b1) {
return 0;
}
return ($a1 > $b1) ? -1 : 1;
}
foreach($all_tweets as $alltweet => $tlink){
$doc = new DOMDocument();
// Okay this is HTML is kind of screwy
// So we're going to supress errors
#$doc->loadHTMLFile($tlink);
// Get all images
$images_list = $doc->getElementsByTagName('img');
$images = array();
foreach($images_list as $image) {
// Get the src attribute
$image_source = $image->getAttribute('src');
if (substr($image_source,0,7)=="http://"){
$image_size_info = getimagesize($image_source);
$images[$image_source] = $image_size_info[1];
}
}
// Do a numeric sort on the height
uasort($images, "height_compare");
$tallest_image = array_slice($images, 0,1);
$mainimg = key($tallest_image);
echo "<img src='$mainimg' />";
}
print_r($all_tweets);
print_r($images);
}
Change the for loop where you fetch the actual images to move the images array OUTSIDE the for loop. This will prevent the loop from clearing it each time through.
$images = array();
foreach($all_tweets as $alltweet => $tlink){
$doc = new DOMDocument();
// Okay this is HTML is kind of screwy
// So we're going to supress errors
#$doc->loadHTMLFile($tlink);
// Get all images
$images_list = $doc->getElementsByTagName('img');
foreach($images_list as $image) {
// Get the src attribute
$image_source = $image->getAttribute('src');
if (substr($image_source,0,7)=="http://"){
$image_size_info = getimagesize($image_source);
$images[$image_source] = $image_size_info[1];
}
}
// Do a numeric sort on the height
uasort($images, "height_compare");
$tallest_image = array_slice($images, 0,1);
$mainimg = key($tallest_image);
echo "<img src='$mainimg' />";
}

Categories