Consider I have an image named "car.jpg".
I have multi-language stores in my site and client wants to show the same image in various languages. Like car is called voiture in French so the image name should be "voiture.jpg", but it will show the same image i.e. car.jpg.
Also while saving / downloading the image it should get saved as voiture.jpg from the French store and car.jpg from the English store.
I have plans to input the names of the images from the client via a back-end panel, but can’t get ideas about how to implement this in front-end.
Note:- The image is not on my server
I have copied the image on fly and can use it as below, using class upload, but need a better option than coping image as there are above 4000 images.
<img src='./media/scene7/car.jpg' alt='car'>
<?php
include('class.upload.php');
$img = new Upload('media/scene7/car.jpg');
if ($img->uploaded) {
$img->file_new_name_body = 'voiture';
$img->file_auto_rename = false;
$img->file_overwrite = true;
$img->Process('./media/scene7/translated/');
}else{
}
echo "<br /><br />Car is called voiture in French, you are seeing the image of voiture.<br />";
?>
<img src='./media/scene7/translated/voiture.jpg' alt='car'>
<?php
if ($img->uploaded) {
$img->file_new_name_body = 'auto';
$img->file_auto_rename = false;
$img->file_overwrite = true;
$img->Process('./media/scene7/translated/');
}else{
echo "not uploaded";
}
echo "<br /><br />Car is called auto in German, you are seeing the image of auto.<br />";
?>
<img src='./media/scene7/translated/auto.jpg' alt='car'>
<?php
if ($img->uploaded) {
$img->file_new_name_body = 'coche';
$img->file_auto_rename = false;
$img->file_overwrite = true;
$img->Process('./media/scene7/translated/');
}else{
echo "not uploaded";
}
echo "<br /><br />Car is called coche in Spanish, you are seeing the image of coche.<br />";
?>
<img src='./media/scene7/translated/coche.jpg' alt='car'>
<?php
$img = new Upload('http://s7d7.scene7.com/is/image/zeon/Stack_Mouse');
if ($img->uploaded) {
echo 'uploaded image from url';
}else{
echo $img->error;
}
?>
you can have images as blobs in your database and give names as you like it.
pros and cons for storing images as blobs can be find out from the below link
php:Store image into Mysql blob, Good or bad?
Have a database table that lists all the image filenames that you want to display to the user, along with their real URLs. For example, the table might have
+-------------+--------------------------+
|FILENAME |URL |
+-------------+--------------------------+
|car.jpg |./media/scene7/car.jpg |
|voiture.jpg |./media/scene7/car.jpg |
|auto.jpg |./media/scene7/car.jpg |
|truck.jpg |./media/scene7/truck.jpg |
|camion.jpg |./media/scene7/truck.jpg |
|lastwagen.jpg|./media/scene7/truck.jpg |
+-------------+--------------------------+
Now write a page image.php, which expects a filename as a query parameter. It should take the query parameter and look it up in the database table, then do a redirect to the URL that it finds.
You can then include your image in the page as something like
<img src="image.php?file=voiture.jpg" alt="voiture.jpg"/>
When the browser requests this image, your page will be called, and the image will end up coming from .media/scene7/car.jpg.
You can make a hash table like 'car' => 'car.jpg' and 'voiture' => 'car.jpg'. when every time the clients visits voiture.jpg you can return car.jpg.
You can achive this by implemetlnting one cofig file for each language. Here is explanation:-
Suppose you have some images like user.jpg, profile.jpg etc.
Now create one french.php file.
Structure of french.php file:-
<?php
$lang=array();
$lang['user']='french_transalation_of_user';
$lang['profile']='french_transalation_of_profile';
?>
In same way add all your image names which you want to display in french. Now call these names dynamically in webpage. You can also create as many as language.php as you want.
I hope you understand the logic. Sorry for bad english.
Related
I am trying to insert an image from database to twitter card (link) but the links appearing in the tweet without an image
example of the tweet card (link)
tried to make a default image for the tweet card and it work perfectly
but when i try to retrieve the name of the image from the database and put it in the <meta> it shows up as an empty image in twitter
tried also echoing $picture and the path shows up as its in the database : /image.png
<head>
<?php
//identify the user
$un = $_SESSION['active_user'];
//query to get that specific users information
$query = "SELECT * FROM users WHERE username='$un'";
//save the query in result variable
$result = $db->prepare($query);
//excecute the variable result
$result->execute();
//if the result has records in the database save the users info in the variables below
if ($row = $result->fetch())
{
//validation variables
$emptyFields = false;
$error = false;
$success = false;
$n = $row['name'];
$usn = $row['username'];
$picture=$row['picture'];
}
echo "<meta property='twitter:card' content='summary'>";
echo "<meta property='twitter:site' content=''>";
echo "<meta property='twitter:title' content=''>";
echo "<meta property='twitter:description' content=''>";
echo "<meta property='twitter:image' content='https://website.com/".$picture."'>";
echo "<meta name='twitter:creator' content='#abc'>";
echo "<meta name='twitter:text:title' content='website'>";
?>
what am i missing here ? is the way i am retrieving data ? please i would try any suggestion
i do not know how you saved the image to the location or to the database, but based on your question,
Read this and see if you missed something
twitter:image is a url to a unique image representing the content of the page. You should not use a generic image such as your website logo, author photo, or other image that spans multiple pages. Images for this Card support an aspect ratio of 1:1 with minimum dimensions of 144x144 or maximum of 4096x4096 pixels. Images must be less than 5MB in size. The image will be cropped to a square on all platforms. JPG, PNG, WEBP and GIF formats are supported. Only the first frame of an animated GIF will be used. SVG is not supported.
and also one more thing, it seems like you saved the entire or part of the image directory on the database, based on database : /image.png in your question. Note that this is not good practice since you might have to access the image from different pages of your website, and even though its not impossible, it's not worth it, just sve the image name only like image.png
I don't know if I'm doing it right, but I have a bunch of images I'm retrieving from the page and since I don't wan a page to have too many images of big sizes, I have displayed them with a much smaller size but I have attached each of them to a link so that when a user click on a picture it opens that image with its original size. The problem is that those images are really big and my client wants the ability to zoom in and out which I don't know how to do. The client thought about resizing the size of the window (in the browser) but sadly it resizes all other windows (for the application) and this is not ok because he needs to see the image and compare it with some information on the app. SO Below is the code of the images displayed and after the user have clicked on the image.
small images
$count = 0;
echo " <div class=\"row\">";
while($row = $result->fetch_assoc()) {
$ext = $row['Extension'];
$ImageID=$row['ImageID'];
if(($count%3) ==0){
echo "</div>";
echo " <div class=\"row\">";
echo " <div class=\"col-sm-2\">";
echo " <a href=\"viewimage.php?ImageID=$ImageID\" class=\"thumbnail\">";
echo '<img id=\"myImg\" src="data:image/$ext;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}else{
echo " <div class=\"col-sm-2\">";
echo " <a href=\"viewimage.php?ImageID=$ImageID\" class=\"thumbnail\">";
echo '<img id=\"myImg\" src="data:image/$ext;base64,'.base64_encode( $row['Image'] ).'" style=\"width:130px;height:130px\"/>';
echo"</a></div>";
++$count;
}
}
echo "</div>" ;
Image after link is clicked
<?php
$ImageID = $_GET['ImageID'];
$query = "Select * from $dbname.Images where ImageID = $ImageID";
$result = mysqli_query($conn,$query);
$row = $result->fetch_assoc();
$ext = $row['Extension'];
echo '<img src="data:image/$ext;base64,'.base64_encode( $row['Image'] ).'"/>';
?>
I don't know what to do at this point, how can I provide that zoom in/out functionality?
First things first: Generally don't add base64 encoded images directly into your html. Link to them, and host them on your server. It is quite an expensive way of making images appear, both for the server, database, and for the client. It also makes it impossible for the client to cache the images, and it means that each repeated page visit causes the entire data to be sent.
Make two folders on your webservers:
images/
thumbnails/
Put your small images in "thumbnails" and large images in "images"
And if you need to, store the image-names in your database, so you can do something more like this:
echo '<img src="images/'+$imageName+'">'
If you want to, you can do an on-demand resizing of your images, using gd-lib.
The basic idea being, in pseudocode:
//Before the echo command, but after fetching the filename from database
if thumbnails/$imageName exists
then use gdlib to read images/$imageName and save a small version to thumbnails/$imageName
This approach is also applicable if you want to use client-side javascript to show larger versions on the same page. See my page finalkey.net for an example http://finalkey.net/gallery
I'm building a simple CMS and have the login, edit pages etc.
Those of you who have built your own CMS, how do you store user uploaded images with an alt tag?
File name/path in the database and alt tag there too?
Then grab the id when writing out the image and create a php page that acts as an image?
eg:
<?php include image.php?id=1; ?>
image.php outputs:
get id
connect to db
echo '<img src="nameofimageandextension" alt="alttag">';
Or would that not be a good idea?
Storing the name and the alt text seems like the best solution. The path can then be configured to be static, ie, /user/upload or something similar.
The images when required will then have to be routed through php to ensure the alt text is displayed.
<img src="<?php echo $imagePath . $imageNameFromDB; ?>" alt="<?php echo $imageAltFromDB; ?>"/>
Where is the problem in creating a table ( or an extension to your user table ) something like this?
+----+----------------+------------------+
| id | name | alt_tag |
+----+----------------+------------------+
| 1 | myimage.png | My image |
| 2 | otherimage.jpg | Some other image |
+----+----------------+------------------+
And access it like this:
$query = "SELECT name, alt_tag FROM images WHERE id = ?";
$params = array( 1 );
$path = "http://mysite.com/path/to/image/";
$stmt = $db->prepare( $query );
$stmt->execute( $params );
$row = $stmt->fetchAll( PDO::FETCH_ASSOC );
echo '<img src="{$path.$row[name]}" alt="{$row[alt_tag]}" title="{$row[alt_tag]}" />';
I believe this is the cleanest and easiest ( best? ) way to do it.
And to answer to the 2nd question:
Depends on how you want to protect your images, but normally, no, it's better to show the real image link. You don't want users to see all images as myimage.php?id=1 style unless those are captcha or other one time use only images.
Using PHP as image url messes up some browser cache, so this will mean that a user has to load your entire site every time (s)he wisits it.
Not pleasant for the user or your server traffic.
The path is what i store in the database like-- resources/myimage.png . But for it to work you have to insert it into the database mysql as-- resources . myimage.png I do this this way--->
$upload_path = 'resources/'; // The place the files will be uploaded to (currently a 'files' directory).
$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
$avatar = $upload_path . $filename; //here is the key. Asighn a var for both and the dot= your /(forward-slash)between them. Then sanitise and check it then when you insert it using the query like so-- mysql_query('update users set avatar=" '.$avatar.' " where id= and thats it.
I have been working on a project but I have reached a point where I am stuck. I have a database that contains the the working status of some mahcines. The values for the status go from 1-5. I need to be able to display a different image for each machine in a webpage based off of the value that appears in the database for that Mahcine. I am drawing a big blank on how to do this. Im using a MySQL DB and everything is written in PHP.
Basically it this. If a machine has a status value of 1 then it shows a green image. If the value is 2 then it would be yellow and so on. . .
Hope you guys can help
You can try something like this:
// your mysql select, wich contains the machine data.
$query = mysql_query("select the data about machines...");
// you iterate on the result set and fetch each row to $data
while($data = mysql_fetch_array($query))
{
switch($data['machine'])
{
case "machine type 1": // you can put integer values here as well, like case 1:
echo '<img src="first_machine.jpg" alt = "first machine" />'
break;
case "machine type 2":
echo '<img src="second_machine.jpg" alt = "second machine" />'
break;
default: // undefinied
echo '<img src = "undefinied.jpg" alt = "undefinied" />'
}
}
Don't use the img tag, instead create a div for which you apply a style class same as the machine status value
<div class="machine status<?php echo $status;?>" ></div>
now in your css,
.status1{
background-image:url(red.jpg);
}
.status2{
background-image:url(green.jpg);
}
.status3{
background-image:url(jpg.jpg);
}
.machine{
width:50px;
height:50px;
}
Ok you can't display multiple images within a image/jpeg page...
You're telling the browser that the page is image/jpeg (in other words, the page is AN IMAGE) but you're echoing out multiple image data
You should rather use the gallery page to show all images like this:
<?php
// $images = result from database of all image rows
foreach ($images as $img) echo '<img src="img.php?id='.$img["id"].'">';
?>
and in img.php:
// Load the image data for id in $_GET['id'];
header("Content-type: image/jpeg");
echo $data;
I create a component in my joomla website. the component shows some photos (not big, only 8KB). the photos are stored in mysql blob. i can upload the photos to the joomla database but i cannot display it on the website. whatever i do it only show some encoding character or blank. I tried to create a separate page but but the result is same. Here is what i have done :
mycomp is my joomla component.
admin.mycomp.php
<?php
function showDetail($option)
{
$db = &JFactory::getDBO();
$id = mysql_real_escape_string(JRequest::getVar('id'));
$query = "select id,myphoto from jos_myphotos where id = ".$id;
$db->setQuery($query);
$rows = $db->loadObjectList();
HTML_myphoto::showPhoto($rows,$option);
}
?>
admin.mycomp.html.php
<?php
class HTML_myphoto
{
...
function showPhoto($row,$option)
{
...
header("Content-type: image/jpeg");
echo $row->myphoto; //this will show some encoding character
echo base64_decode($row->myphoto); //this will show blank page
//change echo with print get the same result.
...
}
...
}
I tried to create a separate page like this :
admin.mycomp.html.php
<?php
class HTML_myphoto
{
...
function showPhoto($row,$option)
{
...
?>
<img src="show_image.php?myphoto=<?php echo $row->myphoto;?>" width=200 height=300>
<?php
...
}
...
}
show_image.php
<?php
$myphoto = (isset($_GET['myphoto'])) $_GET['myphoto'] : false;
if($myphoto)
{
header("Content-type: image/jpeg");
echo $myphoto; //this will show some encoding character
echo base64_decode($myphoto); //this will show blank page
//change echo with print get the same result.
}
?>
the result is same.
I think you have 2 options:
Either you make an image tag with its source in a PHP file receiving only a ID parameter and retrieving the photo's string in the DB and echoing it.
Or you echo directly your photo's string in your tag:
<img src="<?php echo base64_decode($myphoto); ?>" />
EDIT
I just checked in an old app where I store the favicons in a DB. You don't need to base64_decode when you display your image inline (my option 2).
So FYI, this image works:
<img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAQAAAAEABcxq3DAAACkElEQVQ4y42TTWyUZRSFn/e+n6W00E4l9ScSCZGODXFLIIZENyZu1ITEGBdE3ejGVQksunDlwoUrE0ICCQuomZAYF4YNNvEPlFpjiJRgKq10HDKdmVJxEOh0vu+9h0UrMSyAk9zVOffczXPRI8hdciVNTZ/X6MjLOnrkK3XTLbk64lEK5FIhaf87HyqznYKy5qsLyuUPL/D1OXzkC8U4IhhViC9o7OAnSkkKksQDVG81GR//iAvTbS7OXIQQ1p0Oc3O/YlCAAAcSQIHIEc43Z8/z6r5xfrsiBp9+ArK4FgprNye//oEMDIJDMBDU6jc4d/YXzpz5kcuzC5SGBvBkoMT257ah5FTn/4Qgpqd+InM3LEDr+j8cO/45pyrnmLk0w0uv7KV/cIDCI4GIhUAgY3h4C9W5Kma9LF//l2xpqc33301xcuJLTp+ehLiJbSNlcpxoEanAolF4okB0lRAiuVOt1cje/+AAly8tcHWhDnEDeAfXKo/FTbg7ZLdwCrDI0PAAK7fvQATkYIbN/l4jWh8pAb4BrJ/abI32301MXULKoIhE9bKxt8RKJ4D1gXpIitj+d19j6PGN7CiXQavgHYir3G5fY+tT/Wx/cgulnh6su0JYvUPodiHvAGJz32YoJC222qo1lvTH1b906NBngq06cPBjFZKkXIuNmhZbN3RlvqUTlUnt2fumsGc0NvapkJLcpeRrzF+r31R5dLd2PP+i6s3if//gcndJUqPRUKVSUbPZFCnlkpJcSWmd+dffeE/BntWJk9/eW7q/RJJSSjIzQxKBhIUcgN17dhFCxsREheXlZSStZUIghEBRFACYGRlwz3Q5FuDtt/Yx/fMFSqVB8jwnrPP/Xy7LMtwdM+Mu+2gfA7SP0igAAAAASUVORK5CYII=" style="margin-right: 5px; vertical-align: middle;" class="bbns_itemDragger">
And it is stored in my DB like this (base64 encoded):
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAQAAAAEABcxq3DAAACkElEQVQ4y42TTWyUZRSFn/e+n6W00E4l9ScSCZGODXFLIIZENyZu1ITEGBdE3ejGVQksunDlwoUrE0ICCQuomZAYF4YNNvEPlFpjiJRgKq10HDKdmVJxEOh0vu+9h0UrMSyAk9zVOffczXPRI8hdciVNTZ/X6MjLOnrkK3XTLbk64lEK5FIhaf87HyqznYKy5qsLyuUPL/D1OXzkC8U4IhhViC9o7OAnSkkKksQDVG81GR//iAvTbS7OXIQQ1p0Oc3O/YlCAAAcSQIHIEc43Z8/z6r5xfrsiBp9+ArK4FgprNye//oEMDIJDMBDU6jc4d/YXzpz5kcuzC5SGBvBkoMT257ah5FTn/4Qgpqd+InM3LEDr+j8cO/45pyrnmLk0w0uv7KV/cIDCI4GIhUAgY3h4C9W5Kma9LF//l2xpqc33301xcuJLTp+ehLiJbSNlcpxoEanAolF4okB0lRAiuVOt1cje/+AAly8tcHWhDnEDeAfXKo/FTbg7ZLdwCrDI0PAAK7fvQATkYIbN/l4jWh8pAb4BrJ/abI32301MXULKoIhE9bKxt8RKJ4D1gXpIitj+d19j6PGN7CiXQavgHYir3G5fY+tT/Wx/cgulnh6su0JYvUPodiHvAGJz32YoJC222qo1lvTH1b906NBngq06cPBjFZKkXIuNmhZbN3RlvqUTlUnt2fumsGc0NvapkJLcpeRrzF+r31R5dLd2PP+i6s3if//gcndJUqPRUKVSUbPZFCnlkpJcSWmd+dffeE/BntWJk9/eW7q/RJJSSjIzQxKBhIUcgN17dhFCxsREheXlZSStZUIghEBRFACYGRlwz3Q5FuDtt/Yx/fMFSqVB8jwnrPP/Xy7LMtwdM+Mu+2gfA7SP0igAAAAASUVORK5CYII=
I'm sorry, did you skip some lines from show_image.php?!
Cause $myphoto is just the id of the photo. You can't base64_decode an ID.