I have a database where images are saved as blob. Now, I want to convert this blob data into image files.
So I have made this code, but its not giving any result
$sql_icon = "SELECT $off_table.icon,$off_table.id
FROM $off_table,$cnt_table,$ctg_table
WHERE $off_table.id = $cnt_table.id
AND $off_table.id = $ctg_table.id
AND ".$cond_api."
AND ".$cond_nt."
AND ".$cond_cnt."
AND ".$cond_ctg;
$result_icon = mysql_query($sql_icon);
while($row = mysql_fetch_array($result_icon))
{
$image = $row["icon"];
$id = $row["id"];
$file = fopen("./image/".$id.".jpg","w");
fwrite($file, base64_decode($image));
fclose($file);
}
The issue is, I am getting files but no extensions and if I rename them to ".jpg" extension, then its displaying "no preview available"
NOTE:
The images are saved into database by this method
$sql = "INSERT INTO aw_offers_v2
(
id,name,description,payout_type,
payout,expiration_date,creation_date,preview_url,
tracking_url,categories,countries,countries_short,
api_key,network_id, icon,icon_size)
VALUES
('$id','$name','$description','$payout_type',
'$payout','$expiration_time','$creation_time','$preview_url',
'$tracking_url','$categories','$countries','$countries_short',
'$api','$api_url','".mysql_real_escape_string(file_get_contents($cover_image_src))."','".strlen(file_get_contents($cover_image_src))."')";
If I write this code
echo '<img src="data:image/jpeg;base64,'. base64_encode($image).'" />';
then I can see the images properly. That means the images are successfully saved.
But I can't store them in folder as files.
By looking at your code, you don't base64_encode your image.
So you can just do:
fwrite($file, $image);
You only need base64_encode when you display the image putting the data inside the src attribute of <img tag.
Also note that you can simplify this:
$file = fopen(...);
fwrite($file, $image);
fclose($file);
with a single line:
file_put_contents("./image/{$id}.jpg", $image);
Related
I am trying to create a form that uploads an image to a database and displays the image on the webpage. When I upload an image, the image is added to the database, however it is not displayed on the webpage. It only displays an image icon. I am not receiving any error messages. Here is the uploadpage.php code:
$file = $_FILES['image']['tmp_name'];
if(!isset($file))
echo "That is not an image.";
else
{
$image = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageSize = #getimagesize($_FILES['image']['tmp_name']);
if ($imageSize==FALSE)
echo "That is not an image.";
else
{
if (!$insert = mysql_query("INSERT INTO storeImage VALUES('','$imageName','$image')"))
echo "Problem Uploading Image";
else
{
$lastid = mysql_insert_id();
echo "Image Uploaded.<p/>Your image:<p/><img src=get.php?id=$lastid>";
}
}
}
And the get.php code:
$id = mysql_real_escape_string($_REQUEST['id']);
$image = mysql_query("SELECT * FROM 'storeImage' WHERE 'id'=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
Thanks so much!
you can upload your image to project folder like "Upload" and image name send to database, then you can get image name from database and you can give path to web page with image name
see https://www.formget.com/upload-images-using-php-and-jquery-via-form/
How about you store the image in the filesystem and save the url to the image in the database instead? Because, from my POV (though I am not well versed in saving binary data to databases), whatever you're doing seems confusing to me. I would rather save the image's location after it's uploaded in the filesystem and store it into the database.
You can then pull it from the database using <img src="<?php echo $image['image_url'];?>">.
Your database might not support images so I suggest you store the uploaded images into a folder like /images and name each one with an id like 176379.png then storing its path in a database. Then when you need to display it, you can use something like something like echo '<img src="{$image}"'>
I want to save images from url to my directory and its image name after downloaded into a csv.
I write the following code which store images. but its not working as I want.
in below code $img is variable which fetch url from csv.
suppose I have 1000+ products in csv and every products have image url like
www.example.com/images/image1.jpg
so I want to download this image to my local directory/server directory and store its image name after download to csv file so I can add only image name into database tables.
set_time_limit(1000);
while($url = $response->fetch()) {
$my_image = file_get_contents($img);
$my_file = fopen('/csvfiles/','w+');
fwrite($my_file,$my_image);
fclose($my_file);
}
The some solution I found on other similar questions is
$file = file_get_contents($img);
file_put_contents("/csvfiles/Tmpfile.zip", fopen($img, 'r'));
But it is not applicable in my condition or not working.
Any help would be appreciated.
Edit New -
As marked as duplicate I want to add some more info here -
As I have all products info in csv formats and while importing the products I also want to download images from its url. And it is not possible to download 1000+ product image manually. I checked the above solution but it is not applicable in this situation. As in above solution they download image from only particular page and rename it directly. so it is different scenario here.
New Code - I also tried this code still not working
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
$fp = fopen("/csvfiles/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
Try with the following code.
set_time_limit(1000);
while ($url = $response->fetch()) {
$my_image = file_get_contents($img);
// check stricly that you have content of image
if ($my_image !== false) {
// download the image and store in the database.
$my_file = fopen('/csvfiles/', 'w+');
fwrite($my_file, $my_image);
fclose($my_file);
}
}
You need a filename as well, not only a directory:
// download the inage from the internet:
$my_image = file_get_contents($url);
// get filename from url:
$filename = basename($url);
$path = '/csvfiles/' . $filename;
// save image to that file:
$my_file = fopen($path,'w+');
fwrite($my_file, $my_image);
fclose($my_file);
// save $filename to DB
Hi below code works for me .. hope it will help someone..
Thanks #Gavriel and #Alankar
if(!empty($img)){
$my_image = file_get_contents($img);
file_get_contents(filename)
$filename = $data[0] . ".jpg";
echo $filename. "\n";
if(!file_exists($filename))
{
$my_file = fopen($filename,'w+');
file_put_contents($filename, $my_image);
echo $filename . "\n";
}
}
$image = "/uploads/" . $filename;
echo $image . "\n";
Here $img store url of image. which is fetch from csv file.
Here data[0] is csv column which is used to store image name uniquely . so we can use file_exists(); otherwise whenever I run my script again and again it store images again and store it by random temp name.
When I save a image using the Image Intervention class my file saves but shows as blank. I send it a base64 string.
Currently My Code.
$file = Input::file($_POST['data']);
$img = Image::make($file);
$fileName = "profiles/".md5(time()).'.png';
$path = public_path($fileName);
if($img->save($path)){
$user = User::find(Auth::user()->id);
$user->location->start_image = $fileName;
$user->push();
}
print_r($_POST['data']) -> http://pastebin.com/BGbUeZhr
Thanks
I have tried $file = Input::file(base64_decode($_POST['data'])); This does not work
Maybe I'm missing something but using Input::file when you have the image as base64 looks just wrong to me. Input::file is for retrieving a file that has been upload with an html form.
Assuming $_POST['data'] is a base64 encoded image, try this:
$img = Image::make($_POST['data']);
Or even better:
$img = Image::make(Input::get('data'));
I'm working in a content based image retrieval php project.The user should upload an image to retrieve images similar to it . I need to display the query image without saving it in database. I tried two different ways to do that but it didn't work. The image appear as image icon.
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
$content = addslashes($content);
I tried this:
echo '<img src="data:image/jpeg;base64,' . base64_encode( $content ) . '" height=200 width=200 />';
and this:
header("content-type: image/jpeg");
echo $content;
take a look at this jquery plugin. See Documentation section and it's a PHP implementation example. Basically, the plugin upload the image, save temporally on server file system and then send back so client to be rendered. Once it's on your file system you can do whatever you want with it (make the query, delete it for example). I think it's a good aproach, it was to me!!.
Hope This Helps!!
Found several examples online on how to upload an image to a mysql database table, in binary
NOT a link or a folder in the server.
the imag isn't viewable when i try to print it
when I check the database table,it shows a bunch of data in weird format, i'm assuming the image data
here is code
if(!empty($_FILES['image']) && $_FILES['image']['size'] > 0 && !empty($_POST['name']))
{
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
code for displaying image
$sql = "SELECT * FROM `photos` WHERE userName = '$currentUser'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
$content = $row['image'];
echo $content;
echo '<p id="caption">'.$row['caption'].' </p>';
}
when i try to display the image i get nothing as output, i check the database via putty, and i see a massive amount of weird characters, i'm assuming the items for the image.
ideas?
you could eventually try to replace these two lines:
$content = $row['image'];
echo $content;
with:
echo '<img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" />';