I've used the below code to store and get the images from the backend. The images are getting stored but it is not getting converted to base64 format...
$fruit_img = addslashes($_FILES['fruit_img']['tmp_name']);
$name = addslashes($_FILES['fruit_img']['name']);
$fruit_img = file_get_contents($fruit_img);
$fruit_img = base64_encode($image);
while($row = mysqli_fetch_array($res))
{
echo '<img height ="300" width="300" src="data:image/jpg;base64,'.$row[2].' "> ';
echo $row[0];
}
I presume your code represents storing the data here:
$fruit_img = addslashes($_FILES['fruit_img']['tmp_name']);
$name = addslashes($_FILES['fruit_img']['name']);
$fruit_img = file_get_contents($fruit_img);
$fruit_img = base64_encode($image);
And retrieving the data here:
while($row = mysqli_fetch_array($res))
{
echo '<img height ="300" width="300" src="data:image/jpg;base64,'.$row[2].' "> ';
echo $row[0];
}
If that is the case, you have simply mislabeled a variable in this line:
$fruit_img = base64_encode($image);
$image has not been assigned any value previously.
$fruit_img = base64_encode($fruit_img);
If that is not the case, please post additional (complete insert and read) code.
Related
I am having problem with displaying default image. I do not know where exactly to place it.
$result = $conn->query("SELECT * FROM adoption;");
if($result->num_rows !=NULL){
while($rows = $result->fetch_assoc()) {
$AAnimalName = $rows['AAnimalName'];
$Abreed = $rows['Abreed'];
$Asex = $rows['Asex'];
$Acolor = $rows['Acolor'];
$image = $rows ['image'];
?>
<div class="container-custom1">
<?php echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?>
<?php echo "<i><h1 class='junction'><a style='cursor:pointer' class='junction'>".$AAnimalName."</a></h1></i>"."<br>".$Asex." /".$Abreed."<br>".$Acolor."<br>"?></div>
<?php
}
}
try this inside while loop :
$image_location = "admin/function/upload/".$image;
if(file_exists($image_location )) {
echo '<img src = "'.$image_location .'" width = "248" height="190" class="age1" title>';
}
else {
echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>';
}
Change the line like this:
<?php echo '<img onerror="this.src=\'img/logo.png\'" src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?>
img/logo.png would be the default image
Try this:
If(file_exist('your_file_path'))
{
echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>';
}
else
{
echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>';
}
This will check if the file exist or not. If exist, it will display given file. Otherwise a default file.
$image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ;
Use ternary operator and set image
<?php
$image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ;
$image_location = "admin/function/upload/".$image;
echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>';
?>
This is a bit confusing and hard to explain but I will try my best to explain it.
Basically, I have a directory of images (test) which holds all the images for my products.
The images look like this:
999999999Image1.jpg
999999999Image2.jpg
999999999Image3.jpg
999999999Image4.jpg
999999999Image5.jpg
555555555Image6.jpg
555555555Image7.jpg
555555555Image8.jpg
555555555Image9.jpg
555555555Image10.jpg
etc etc...
The numbers before Image is a field in MYSQL database called STOCK.
Now, I'm trying to use glob() to display the images for each item in mysql database like so:
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2 .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2 = '';
}
}
/////rest of my code..../////
}
However, when I run my code, I get a very strange output...
The issue that I have is that with each output of MYSQL result, the images that being found by glob() is being added to the next result too! So, its like the image of the first result are being added to the next result and then the images of first result and the second results are being added to the third result and so on so forth...
I hope this makes sense and someone could point me in the right direction.
Any help would be appreciated.
I suggest:
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
$pic_list2 = ''; // you need this line also here !! only change
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2 .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2 = '';
}
}
/////rest of my code..../////
}
What about this? This creates for each mysql output a new array key.
<?php
$i = 0;
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$STOCK = $row['STOCK'];
foreach(glob('test/'.$STOCK.'*') as $image) {
if($image != ""){
$pic_list2[$i] .= '<img data-src="'.$image.'" class="smallImg" width="57" height="43" data-id="'.$STOCK.'" data-details="'.$ADVERT_DESCRIPTION_main.'" data-name="'.$MAKE2.' '.$MODEL2.' '.$DERIVATIVE2.'" data-price="'.$PRICE2.'" src="'.$image.'" alt="" />';
}
else{
$pic_list2[$i] = '';
}
}
$i++;
/////rest of my code..../////
}
?>
So I have a standardized mail form that has certain generated details that must accompany it including a header image.
I am trying to find out how to add the image to the text form for when I send it.
I have
$img_path = 'image12.jpg'; //declaration
while($row = mysql_fetch_array($result))
{
echo "<tr>";
$emailContent=str_replace("\\r\\n","",$row["generic_email_text"]);
echo 'Test';
echo '<td width="450"><textarea name="genericemail">'.$img_path .$emailContent.'</textarea></td>';
echo "</tr>";
}
echo "</tr>";
echo "</table><p>";
I tried adding img_path to it but it won't work. I'm taking over from someone elses code which I always find difficult. All I get with this is the name of the img ("image12")
And yes I havnt done PHP in a while
Here's the PHP for pulling the form info
$fullName = $firstName . " " . $lastName;
$imageFile = str_replace = '<img src="http://css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />'; //Header here is just a place holder
$updatedEmailText = str_replace("%name%", fieldHtmlFormat($firstName), $updatedEmailText);
$updatedEmailText = str_replace("%firstname%", fieldHtmlFormat($firstName), $updatedEmailText);
$updatedEmailText = str_replace("%lastname%", fieldHtmlFormat($lastName), $updatedEmailText);
$updatedEmailText = str_replace("%duration%", fieldHtmlFormat($durationOfStay), $updatedEmailText);
$updatedEmailText = str_replace("%destination%", fieldHtmlFormat($arrivalCity), $updatedEmailText);
$updatedEmailText = str_replace("%travel%", fieldHtmlFormat($travel), $updatedEmailText);
return $updatedEmailText; .
edit:
changed code:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
$emailContent=str_replace("\\r\\n","",$row["generic_email_text"]);
echo 'Test';
//LINE 50'<td width="450"><textarea name="genericemail"><imgsrc="'.$img_path.'"/>'.$emailContent.'</textarea></td>' }//LINE50
echo "</tr>";
echo "</table><p>";
You need to add <img> tag in order to show the photo.
Replace the line:
echo '<td width="450"><textarea name="genericemail">'.$img_path .$emailContent.'</textarea></td>';
With this line:
echo '<td width="450"><textarea name="genericemail"><img src="'.$img_path.'"/>'.$emailContent.'</textarea></td>';
And also make sure that you have the correct file/url path for $img_path.
I would like to display multiple images on my index page. Doesn’t matter if they are displayed in any order.
Each of the images have their image path and their unique ID. I have the following code but it is not displaying anything:
<?php
$result = mysql_query("SELECT * FROM my_image", $connection);
while($row = mysql_fetch_array($result))
{
$ID = $row['name'];
$file = $row['file_path'] ;
echo '<img style="margin: -32.5px 0 0 0;" alt="" src="'.$file.'">' ;
}
?>
Anyone know what could be wrong?
you should start to debug your code, as example :
while($row = mysql_fetch_array($result))
{
$file = $row['file_path'] ;
echo $file; continue;
//...
}
and see if you get expected result.
EDIT:
what about var_dump($result) ?
I am trying to .load a script called 'refreshImages.php'. Inside that script is a while loop pulling from the database. I have got it to load a single echo function but it wont load anything inside the while loop I have on the script... this is what the php file has...
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid='$pid'");
//Cant Post Images So Leaving The Echo Content Out//
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo "<li>$img</li>";
}?>
I am using .load('refreshImages.php') on the page I need it to show up on.
Any explanation I am not seeing?
Your $img is an array, not a string. You will get output like <li>Array</li>, if you have stuff coming from the database. Is that what you mean? Or are you getting an empty result?
If empty - what does your mysql_num_rows tell you when ran against the result resource?
try changing this:
echo "<li>$img</li>";
to
echo "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
You may not be getting any results from the database. Try using this code which will display a message if there is something wrong with your sql query.
<?php
include 'includes/config.php';
$pimages = mysql_query("SELECT * FROM property_images WHERE pid=" . $pid );
if (mysql_num_rows($pimages) > 0) { // checks to see if you are getting results from db
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
echo '<li><a class="thumb" href="{$image}"><img src="{$image}" width="50px" height="50px" alt="{$image_alt}"></a></li>';
}
} else {
echo "no results returned from database";
} // end of mysql_num_rows check
?>
You might be better off concatenating all the images and then echo-ing it out rather than echo-ing each one e.g
$htmlOutput = '';
while($img = mysql_fetch_array($pimages)){
$image = $img['image'];
$image_alt = $img['image_alt'];
$htmlOutput .= "<li><img src=\"{$image}\" alt=\"{$image_alt}\" /></li>";
}
echo $htmlOutput ;