I am using the following code to link to an image on Facebook:
foreach($photos["data"] as $photo)
{echo $photo['source'];
echo "<img src=’{$photo['source']}’ />", "<br />";
}
The echo $photo['source'] shows the correct URL, and so does the src= in the image tag, but it tries to load from my site. So:
If the path to the image is http://a1.myimagepath.jpg
It tries to load http://www.mysite.com/%E2%80%99http://a1.myimagepath.jpg
take care,
lee
Your code contains curly quotes. You need to replace them with single quotes.
foreach($photos["data"] as $photo)
{echo $photo['source'];
echo "<img src='{$photo['source']}' />", "<br />";
}
Try to post the URL manually:
foreach ($photos["data"] as $photo) {echo $photo['source'];
echo "<img src=’http://a1.myimagepath.jpg’ />", "<br />";
}
You can also try without the foreach.
Related
I'm trying to get this so when I click the image, the image then shows in a new tab so it's able to be saved
I've tried adding the a href tags inside and around.
Code:
echo "<img src=" . random_image('css/avatars') . " />";
This is what you want:
echo '<img src="' . random_image('css/avatars') . '" />';
You should try
echo "<a target='_blank' href='".random_image('css/avatars')."'>"."<img src=" . random_image('css/avatars') . " /></a>";
I think this can help you
The problem is that you are not properly concatenating your string with what you get from random_image function.
You can try following code, here I am assuming that your random_image function returns complete path to your image
function random_image($path)
{
// returning dummmy image
return "https://images.unsplash.com/photo-1535498730771-e735b998cd64?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=623ed05afcc5a3fa1e444ffca95854dc&w=1000&q=80";
}
echo "<a href='".random_image('css/avatars')."' target='_blank'> <img src='".random_image('css/avatars')."' /></a>";
I have a home page that shows some uploaded images.
I take it from the database and I use strpos() to check the URL due to directory problems, and it works fine:
if(strpos($row['cImage'],"http://") === FALSE){
echo "<img src='serintercement/".$row['cImage']."' style='width: 107px; height:102px;' />";
}else{
echo "<img src='".$row['cImage']."' style='width: 107px; height:102px;' />";
}
I need to use the same logic in a page that shows the clicked image, but it has a variable for it and I'm struggling to fix this since it's a different way to write:
<img src='<?php echo $resultData->cImage; ?>'/>
I can't fix the directory problem. How can I use strpos() similarly for this second code?
You can do it like this.
if(strpos($resultData->cImage,"http://") === FALSE){
echo "<img src='serintercement/".$resultData->cImage."' style='width: 107px; height:102px;' />";
}else{
echo "<img src='".$resultData->cImage."' style='width: 107px; height:102px;' />";
}
Better option is you can define a function like this and call it
checkImage($row['cImage']);//to be called in your first page
checkImage($resultData->cImage);//to be called in your second page
function checkImage($image)
{
if(strpos($image,"http://") === FALSE){
echo "<img src='serintercement/".$image."' style='width: 107px; height:102px;' />";
}else{
echo "<img src='".$image."' style='width: 107px; height:102px;' />";
}
}
You should be able to check the path similarly - as long as the property of the object is set and a string, strpos() can be used.
if(strpos($resultData->cImage,"http://") === FALSE){
echo "<img src='serintercement/".$resultData->cImage."' />";
}else{
echo "<img src='".$resultData->cImage."' />";
}
If these cImage strings are coming from your database, you could just correct them in the query call.
For example, if you are using MySQL, you could prepare the data using LOCATE() AND CONCAT() like this:
SELECT
IF(LOCATE('http',`cImage`)!=1,CONCAT('serintercement/',`cImage`),`cImage`) `new_cImage`
FROM ...
Moving the conditional process to your query, will make your "displaying" code read more smoothly because all of the preparations are completed ahead of time.
Outside of that, you can make your code more DRY by not repeating the html parts that come before and after the image variable...
This is an echo with an inline condition (not everyone likes this style):
echo "<img src=\"",(strpos($resultData->cImage,"http")===0?'':'serintercement/'),"{$resultData->cImage}\" style=\"width:107px;height:102px;\" />\n";
This is a separated conditional:
echo "<img src=\"";
if(strpos($resultData->cImage,"http")!==0){
echo 'serintercement/'
}
echo "{$resultData->cImage}\" style=\"width:107px;height:102px;\" />\n";
This is a function call if you have to do this multiple times in the same script:
function fixPath($img){
if(strpos($img,"http")===0){
return $img;
}
return "serintercement/$img";
}
echo "<img src=\"",fixPath($resultData->cImage),"\" style=\"width:107px;height:102px;\" />\n";
Notice that I am only checking for http and that I am very accurately checking that it comes from the first position in the string. Across all these alternatives, the point I am making is DRYness (which is a typical pursuit of all programmers). How you chose to implement it comes down to your personal preference.
Im trying to print a image using printf, but it doesnt work.
The column "imagem" show the directory of the image.
function listaCDs() {
$sql = "SELECT * FROM `cds`";
if(($rs=$this->bd->executarSQL($sql))){
while ($row = mysql_fetch_array($rs)) {
printf("
<img src=%s height="100" width="100" />", $row['imagem']);
echo "<br>";
}
}
else {
return false;
}
}
Try:
printf("<img src='%s' height='100' width='100' />", $row['imagem']);
Use this
printf("<img src=%s height='100' width='100' />", $row['imagem']);
instead of
printf("<img src=%s height="100" width="100" />", $row['imagem']);
You have double quotes inside double quotes for attributes, which is the problem. Check this demo.
Make sure the image's directory is public (i.e copy and paste $row['imagem'] in a browser to see if it's accessible).
I have stored images path in database and images in project folder. Now i am trying to view all IMAGES with their PRODUCTS in my HTML template. I have written the following code and its given me an empty images box in the output and when i open that image box in the new tab, it opens the following URL.
http://localhost/cms/1
Kindly tell me the way of referring images in 'img src ' tag.
include 'connect.php';
$image_query = "select * from product_images";
$image_query_run = mysql_query($image_query);
$image_query_fetch = mysql_fetch_array($image_query_run);
if (!$query=mysql_query ("select product_id,name from products")) {
echo mysql_error();
} else {
while ($query_array = mysql_fetch_array($query)) {
echo '</br><pre>';
$product_id = $query_array['product_id'];
echo "<a href='single_product.php?product_id=$product_id' >";
print_r ($query_array['name']);
echo "</a>";
echo "<img src=". $image_query_fetch
['images'] ." alt=\"\" />";
echo '</pre>';
}
}
} else {
echo "You need to Log in to visit this Page";
}
Add your local image path before source
example
echo "<img src='http://localhost/cms/1/". $image_query_fetch['images'] .'" alt=\"\" />";
*Use PHP *
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
You can use the HTML base tag to set a base URL that relative URLs will be resolved from.
See -> http://www.w3schools.com/tags/tag_base.asp
I have a PHP script to echo the contents of a Mysql table.
I'm using it as a small CMS for a static page.
What I want to know is how can I go about displaying an Image in my PHP script.
I have a form that submits the Date, Title, Message, and Image.
I'm using the Image field to insert the URL of an image.
Whats the correct way of displaying the image on a page.
Below is my code:
<?php
$con = mysql_connect("localhost","name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo "<br />";
echo $row['Title'];
echo "<br />";
echo $row['Message'];
echo "<br />";
echo $row['Image'];
echo "<br />";
echo "<br />";
echo "<br />";
}
mysql_close($con);
?>
This is the outcome:
17th Feb
Title
Here is my Message
http://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
18th Feb
Title
Here is my Message
http://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
but I want the page to display the image not the URL.
I'm guessing just using the <img></img> tags, but I'm not sure where in the PHP.
You can write code in image tag instead of echo directly.
Try this code.
echo "<img src='".$row['Image']."'/>";
Try this code, but give image folder path perfectly
<?php
$imagepath = "../uploads/";
echo "<img src='".$imagepath.$row['Image']. "' alt='' height='200' width='200' /> ";
?>
According to need , you can change height and width of the image. but clarity differs.
Thanks
echo $row['Image'];
replace like this
echo '<img src="'.$row['Image'].'" alt="" />';