I tried converting a data URI generated using
canvas.Canvas.toDataURL();
I am just trying to store the data URI as image using the following code
<?php $data = $_POST['image_designed'];
echo $data; //Data URI
echo '<img src="'.$data.'"/>'; //Getting the image perfectly
//removing the "data:image/png;base64," part
$uri = substr($data,strpos($data,",")+1);
header('Content-type: image/png');
echo base64_decode($uri);exit; //Just image place holder displaying instead of full image
I am getting the following data
$data = data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPoAAAEsCAYAAADq/K67AAAgAElEQVR4Xky9WY8l6Xnn90acPffMqspauqqrit3sZrO5z2g0FATJIw0G8L0/gr
//Very longer data than this which cannot be pasted here
The final image result is wrong. Just displaying an error image placeholder. I guessed the reason is "I found long space in the middle of my $data". But it renders the image well in src tag.
Related
I'm trying to echo an image content from an uploaded file with this code:
<?php
$imgContent = addslashes(file_get_contents($_FILES['image']['tmp_name']));
header("Content-type: image/png");
echo $imgContent;
?>
But it just shows an small empty square. I must upload and fetch a BLOB field from mysql to show on the browser, but it's not saving correctly.
As commented by Lars Stegelitz and RiggsFolly, I corrupted the file by adding addslashes on the file content. By removing it I could see the image being returned back.
I wroted that simple script just to test if I getting the image content correctly. In the real code I'm checking the file type.
I'm working with some binary image data given to me by another developer and trying to figure out how to display the images on the screen. The binary images all look like this:
0xFFD8FFE000104A46494...[TRUNCATED]...F6145145690D8E4ACFDE3FFFD9
I've tried the following PHP code to get the image to save:
$data = '0xFFD8FFE000104A46494...F6145145690D8E4ACFDE3FFFD9'; //full data entered here
$string = pack('H*', $data);
$result = imagecreatefromstring($string);
imagejpeg($result, 'test.jpg');
This saves an image called test.jpg, but it is a jumbled image and not the actual image it should be.
I also tried using base64 like this:
<img src="data:image/jpeg;base64,<?php echo base64_encode($data) ?>">
which also did not work. Any suggestions would be appreciated, thanks!
i am trying to save a video thumbnail from dailymotion i can bring the image URL but the actual problem i am getting is to save that thumbnail on my server by renaming image. Secondly i want to check that name if the name is already exist on the server it can replace the older one this function is basically used on updating video id.
Here is my php code to grab dailymotion video thumbnail url but i don't know to to save it
<?php
$related_dm_code = "x2rxn6i"; //Video Code
$thumbd_item='https://api.dailymotion.com/video/'.$related_dm_code.'?fields=thumbnail_240_url';
$json_thumbnail2_item = file_get_contents($thumbd_item);
$get_thumbnail2_item = json_decode($json_thumbnail2_item, TRUE);
$thumb2_item=$get_thumbnail2_item['thumbnail_240_url'];
echo $thumb2_item; //display thumbnail url
?>
e.g. here is the thumbnail url:
anothersite.com/images/demo-image.jpg
How can i save image in this path of my URL:
mysite.com/video_thumbs/
and renaming demo-image.jpg to anyothername.jpg
and then a last part of the function to check and replace if anyothername.jpg is already exist then replace with the older one this would be used to updating mysql
You mean something like:
if (!file_exists('http://www.example.com/video_thumbs/' . $file)) {
$img = file_get_contents("http://www.anothersite.com/images/demo-image.jpg");
file_put_contents('http://www.example.com/video_thumbs/' . $file, $img);
}
So i think this question has been addressed before but none of the answers seem to help me.
I have uploaded a .jpeg file to my database by extracting the content from the file and uploaded that to a BLOB field in my database. When it comes to retrieving the data I search the database using an id set by the query string ?id=146 (I've inserted a value that relates to a specific entry in my database just to check the functionality). When I then echo the image['image'] the binary data displays fine (well the browser renders the data how it interprets it) so its finding the entry I want, and it displays the data fine. When I add header("Content-type: image/jpeg"); and reload the page the browser tells me the image is missing. ![screen shot of missing image display][1]
The entire functionality will work as i will reference the php file in the src of an tag on the page I want the image to display. But I can't even get the image to load onto the php page when I type in its URL(inc the correct query string).
Here is a my code for the php page to find the image:
$id = intval(addslashes($_REQUEST['id']));
header("Content-type: image/jpeg");
$result = $dbh->prepare("SELECT * FROM `cefk_profile` WHERE `id`=$id");
$result->execute();
$image = $result->fetch(PDO::FETCH_ASSOC);
echo $image['img'];
And here is a shot of the page that will display the picture:
echo '<img src="php/get_img.php?id=146">';
i want to read .ico-Images from a Website, store the Information to the Database and show the Images later on a global Website.
I have managed to save the images in a string into the Database. The step to show the Images on a Website is my problem.
For reading the Contents:
$data=file_get_contents("http://www.google.com/favicon.ico");
$data = base64_encode($data);
What is the right way to show that image in a single div on a website?
Fesp
You basically need to tell your script to output the content as an ico type.
<?php
//Getting your image
$data=file_get_contents("http://www.google.com/favicon.ico");
$data = base64_encode($data);
//If your storing in the db then you do the query ect togo get the data string
//Then echo out like this
header('Content-Type: image/vnd.microsoft.icon');
echo base64_decode($data);
?>
Remember you cant output anything before setting the header so perhaps you will need a seperate script to grab the file and ouput so
<img src="get_ico.php?id=1"/>
Then in get_ico.php
<?php
//Connect db ect
//Query db for $_GET['id']
//Then echo out like this
header('Content-Type: image/vnd.microsoft.icon');
echo base64_decode($row['image_data']);
?>