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).
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.
I'm trying to retrieve all images from a folder in the server to my web page through PHP.
At local server, they're displaying fine but at main server, images are not displaying and showing error 404 not found
even though the images are there.
Please help me to get rid of this. The images are in Big Rock server.
Here is my code:
$filename = $_POST['file'];
echo '<table class="table1" border="0" width="100%" >';
echo "<tr>";
echo "<td class='td1'>";
$images = glob($_SERVER["DOCUMENT_ROOT"].
"/admin/gallery/".$filename.
"/*.*");
$count = 0;
foreach($images as $image) {
echo '<a href="'.$image.
'" target="_blank"><img src="'.$image.
'"class="img-rounded mySlides" width="200" height="150"/></a><br />';
if ($count < 3) {
$count++;
echo "</td>";
echo "<td class='td1'>";
} else {
echo "</tr>";
$count = 0;
echo "<tr>";
echo "<td class='td1'>";
}
}
The HTTP 404 Not Found Error means that the webpage you were trying to reach could not be found on the server. It is a Client-side Error which means that either the page has been removed or moved and the URL was not changed accordingly, or that you typed in the URL incorrectly. Give extensions in giving url.
There is no issue with the permission on the file. The error is due to mismatch of the Relative Path of the image. 404 means the resource doesn't exist.
You can check my answer here [can't find correct relative paths of CSS background-image
The problem is glob($_SERVER["DOCUMENT_ROOT"]."/admin/gallery/".$filename."/.").$_SERVER["DOCUMENT_ROOT"] will take path as home/xxxx/public_html/admin/gallery.which cannot open in the browser.The image will open only by the path like this "http://example.com/folder/img.jpg".so i have cropped the path till admin folder like home/xxx/public_html/admin/gallery to admin/gallery and in the img src i have given src="http://example.com/".$image as shown in the code
$filename=$_POST['file'];
echo '<table class="table1" border="0" width="100%" >';
echo "<tr>";
echo "<td class='td1'>";
$curdir=getcwd();
$images = glob($curdir."/admin/gallery/".$filename."/*.*");
$count=0;
foreach($images as $image)
{
$dir="http://screccs.com".str_replace($curdir,"",$image);
echo "<a href='".$dir."' target='_blank'>"; echo "<img src='".$dir."'class='img-rounded' width='200' height='150'/></a><br />";
if($count<3)
{
$count++;
echo "</td>";
echo "<td class='td1'>";
}
else
{
echo "</tr>";
$count=0;
echo "<tr>";
echo "<td class='td1'>";
}
}
echo '</tr>';
echo '</table>';
}
I believe issue is related to folder permissions. Give proper read permission and it will work.
I have a image in database stored in this format
$link = this is my photo <img data-liked='0' data-reblogged='0' data-attachment-id="299971" data-medium-file="http://my.files.wordpress.com/2013/12/img_6697.png?w=394" width="73" height="130" src="http://my.files.wordpress.com/2013/12/img_6697.png?w=73&h=130" class="attachment-thumbnail" alt="IMG_6697" />, school photo;
i was wondring if this is possible to just get output
this is my photo <img src="http://my.files.wordpress.com/2013/12/img_6697.png?w=73&h=130" />, school photo
Just image source nothing else ...
I have tryed this and got image url but i need above result
$str = preg_replace('#<img.+?src=[\'"]([^\'"]+)[\'"].+/>#i', "$1", $link);
echo $str;
$str = preg_replace('#<img.*?src="(.*?)".*?\/>#i', "<img src=\"$1\" />", $link);
echo $str;
You might want to keep the alt:
$str = preg_replace('#<img.*?src="(.*?)".*?alt="(.*?)".*?\/>#i', "<img src=\"$1\" alt=\"$2\" />", $link);
echo $str;
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.