/    Sign up×
Community /Pin to ProfileBookmark

trying to get the url of capture image after its saved to server with a random name

I’m trying to fetch/get the url or entire name of image i capture from a video. I’ve managed to save the capture as an image in the server with a random name but cannot get the url or entire name to echo or even am able to save it to the mysql instead i keep getting the name resource 4 or 2341 number repeating inside the mysql database however a new image with a new random name appears in the server. The post img val is the capture image.

[code=php]<?php
include(‘connect.php’);

$rawData = $_POST[‘img_val’];
$filteredData = explode(‘,’, $rawData);
$unencoded = base64_decode($filteredData[1]);
$randomName = rand(0, 99999);;
//Create the image
$fp = fopen($randomName.’.png’, ‘w’);

fwrite($fp, $unencoded);
$mb = fopen($fp, ‘r’);

fclose($fp);
$q=mysql_query(“insert into images (name) values($fp)”) or die (mysql_error());
?>[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@Error404May 17.2014 — I'm guessing $rawData is an image String, in which case, base64_decode won't return the image you're expecting as you still haven't made the image. When you enter it into your database, you're probably seeing part of the String. Assuming you know that the image will be a .png (or some other file type as long as it's known):

[code=php]
$rawData = $_POST['img_val'];
$unencoded = base64_decode($rawData);
$myImage = imagecreatefromstring($unencoded);
if($myImage !== false) {
$pngImage = imagepng($myImage);
$randomName = rand(0, 99999);
// save it as an image
file_put_contents($randomName."png", $pngImage);
// OR save just the image String and decode later:
// file_put_contents($randomName."png", $unencoded);
} else {
// do something, the image failed
}
[/code]


I removed the explode line as there's really no need to break apart the image String, unless you absolutely need to do that for a specific purpose.

If you plan to save the images, I wouldn't use a random number generator for a file name as you may end up having files with multiple images (or image Strings) and it would be hard to keep track of that in your database. Not to mention, when you read the file, you're going to have to check whether there are multiple images (or image Strings), all of which creates unnecessary work. I would be easier and more effective to increment the file names. Doing it this way, you'd just need to get the last row to find out which number to increment from when you want to insert files later on.
×

Success!

Help @lookingofroit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...