Php showing pictures using array - php

I'm trying to show images from some directory using foreach.. But the problem is it's showing results in array, so if i want to print out first image I have to use $imag['0']..
Is there any way that I can bypass this number in this brackets?
Here's my code...
<?php
$domena = $_SERVER['HTTP_HOST'];
$galerija = $_POST['naziv'];
$galerija = mysql_real_escape_string($galerija);
define('IMAGEPATH', 'galleries/'.$galerija.'/');
foreach(glob(IMAGEPATH.'*') as $filename){
$imag[] = basename($filename);
?>
<img src="http://<?php echo $domena; ?>/galerija/galleries/<?php echo $galerija; ?>/<?php echo $imag['0']; ?>">

If you only need the first filename, then you could avoid the loop and directly access the first element of the array and use it afterwards:
$files = glob(IMAGEPATH.'*');
$filename = array_shift(array_values($files));
$image = basename($filename);
And to display it, you could use sprintf():
echo sprintf('<img src="http://%s/galerija/galleries/%s/%s"/>',
$domena, $galerija, $image);

Well you could first not create the array in the foreach statement and instead just print the img:
echo '<img src="', $domena ,'/galerija/galleries/', $galerija ,'/', $filename,'">';
Or you could iterate the array.
foreach($imag as $img): ?>
<img src="http://<?php echo $domena; ?>/galerija/galleries/<?php echo $galerija; ?>/<?php echo $img ?>">
<?php endforeach; ?>

Related

How to Display Remote Image?

I need to have a working remote IMG display using PHP, let's say like this:
<img src='$src' />
And I need to fetch the remote URL based on $id, where
<?php
$id = "brown_fox";
$url = "http://exampl.com/$id";
get_remote_img($url) {
// some code to get image which SRC is dynamic:
<img id="pic" src="sjf5d85v258d.jpg" />
$src="sjf5d85v258d.jpg";
return $src
}
?>
I hope I explained it understandably.
If I understand you correctly then you can do something like this:
<?php
...
get_remote_img($url) { ...
$src = get_remote_img($url);
// Concatenating the result to the elements src attribute:
echo '<img src='.$src.' />';
?>
What you're looking for is something like this:
<?php
$id = "brown_fox";
$url = "http://exampl.com/" . $id;
...
function get_remote_img($url) {
// some code to get image which SRC is dynamic:
$src="sjf5d85v258d.jpg";
echo "<img id=\"pic\" src=" . "\"" . $src . "\"" . "/>";
return $src;
}
?>
Also, if you want to send and receive query parameters in the URI dynamically through a form, you can take a look at GET Request in PHP.

php inserting link parts into http

I am trying to retrieve parts of the getcwd() method and inserting them into window.open()
the current getcwd() gives me this C:\wamp\www\qa4u\qa4u_working\Presenter
Using this code :
<?php
if(isset($_POST['genPDF'])){
foreach($_POST['email'] as $email)
{
$eid=$_POST['eid'];
?>
<script type="text/javascript" language="Javascript">
<?
$stringlink = getcwd();
$pieces = explode('\\', $stringlink);
?>
window.open("http://"+"<?php echo $_SERVER['HTTP_HOST']?>"+"/"+"<?php $pieces[3]?>"+"/"+"<?php $pieces[4]?>"+"/"+"<?php $pieces[5]?>"+"/genPDF.php?eid=<?php echo $eid ?>&email=<?php echo $email ?>");
</script>
<?php
}
}
?>
I am trying to achieve this instead :
window.open("http://qna.nyp.edu.sg/qa4u/qa4u_working/presenter/genPDF.php?eid=<?php echo $eid ?>&email=<?php echo $email ?>");
without forming static links is there a way to get the code to work ?
2 issues regarding your code, that I believe prevents it from working.
You're not printing the pieces values. You should use echo, print or a shorthand <?=$var ?>
Since those are PHP variables, you don't need to use the JS + symbol
So, you should update your code:
window.open("http://<?php echo $_SERVER['HTTP_HOST']; ?>/<?php echo $pieces[3]; ?>/<?php echo $pieces[4]; ?>/<?php echo $pieces[5]; ?>/genPDF.php?eid=<?php echo $eid; ?>&email=<?php echo $email; ?>");

PHP echo to display image HTML

I am trying to display an image on my webpage using a PHP script to determine which image is displayed.
The image link is as follows:
......
My PHP script is thus:
<?php
$result = $_GET['image'];
echo '<img src="images/gallery/'.$result.'.jpg">';
?>
So what I am trying to achieve in terms of HTML is:
<img src="images/gallery/image01.jpg">
The result I am getting is '"; ?>' displayed on the page.
Any help would be much appreciated!
You have to change your code like this
<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
<?php
$result = filter_input ( INPUT_GET , 'image' );
if (isset($result) && !empty($result)) {
echo '<img src="images/gallery/'.$result.'.jpg">';
}
?>
You used echo wrong, here is how you should use it.
<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result ?>.jpg">
I would change the gallery.php to this:
<?php $result = $_GET['image']; ?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
That would simply it a little bit. You should echo out the result to see what you are getting when the variable is passed to the gallery page.
echo"<img src='{$image}'>";
$image = uploads/myImage.jpg
I think this is the simplest code. To use a php variable while echoing out html, use curly {} brackets to insert any php variable. For instance, a file upload...
<?php
if(isset($_POST['submit'])){
$filename=$_FILES['file']['name'];
$temp_dir=$_FILES['file']['tmp_name'];
$image = "img/".$filename;
}
?>
<?php if($row2['pack1']==1){ echo "<img src=".BASE_URL."images/1seo.png"; } ?>

php echo add text end of the line

foreach($divs as $element)
{
$img = $element->find('a', 0)->href.'<br>';
$img = str_replace("http://www.youtube.com/watch?v=", "http://img.youtube.com/vi/", $img);
echo $img."/mqdefault.jpg";
}
Now output is :
http://img.youtube.com/vi/UYkHcfrxxtk
/mqdefault.jpghttp://img.youtube.com/vi/ZgjaveCUsSg
/mqdefault.jpghttp://img.youtube.com/vi/TFVbehwl1Ns
/mqdefault.jpg
I want like this each img:
http://img.youtube.com/vi/UYkHcfrxxtk/mqdefault.jpg
How can get fix this? i used simple html dom.
echo $img."/mqdefault.jpg<br/>";
But if you are trying to make image elements on a page:
echo "<img src='".$img."' /><br/>";
use this.
remove .'<br>' from end of $img = $element->find('a', 0)->href.'<br>'; then add it to echo $img."/mqdefault.jpg";
<br> in html mean newline
$img = $element->find('a', 0)->href;
$img = str_replace("http://www.youtube.com/watch?v=", "http://img.youtube.com/vi/", $img);
echo $img."/mqdefault.jpg".'<br>';

how to get images to print in loop

This is probably a very simple solution but I am new to PHP I have been searching google to find out how to get it to work to no avail. Here is my problem I want to be able to use the glob function along with extract(pathinfo) to find all images in a folder and print them into an html page. I can only get one image to print to the screen I figured it would print them in order it finds the files. Here is my code:
<?php
$images = glob('*.{png,jpg,jpeg}', GLOB_BRACE);
foreach($images as $img) {
extract(pathinfo($img));
$thumb_name = "$filename.$extension";
//$thumb_name = $info['filename'] . '.' . $info['extension'];
echo $thumb_name . "\n";
}
?>
And finally the html file:
<?php include 'index.php' ?>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<?php echo "<img src=\"$thumb_name\" title=\"bar\" alt=\"foo\" />"; ?>
<?php echo "<img src=\"$thumb_name\" title=\"bar\" alt=\"foo\" />"; ?>
</body>
</html>
not actual working code, but just for u to get an idea
Put the echo img inside the for loop
$images = glob('*.{png,jpg,jpeg}', GLOB_BRACE);
foreach($images as $img){
extract(pathinfo($img))
$thumb_name = "$filename.$extension";
echo '<img src=\'.$thumb_name\.' title=\bar\ alt=\foo\ />';
}

Categories