I made a little trial using JS upon canvas and images. The final part of it calls a page where at the beginning I try to write upon the server the final image.
The process supposes that a previous file exists, not complete:
<?php
$filename = explode(".", $_POST["trans_file"]); // name of the image file
if (!unlink("transit/" . $_POST["trans_file"])) echo "File " . $_POST["trans_file"] . "not found!<br />"; // remove the partial file
$filesave = $filename[0] . ".png"; // I will save a PNG file
$filejpg = $filename[0] . ".jpg"; // but I will transform it into JPG
$data = $_POST["base64img_data"]; // here I receive the base64 image by a previous process
$data = explode(";", $data); // I remove the first part of it
$data = explode(",", $data[1]); // $data[1] now contains the base64 image complete
$image=base64_decode(chunk_split($data[1])); // $image is now a PNG file
$handle = fopen("transit/$filesave", "wb"); // Create a PNG file
fwrite($handle, $image); // write it
fclose($handle); // Close, and transform it into JPG
png2jpg("transit/$filesave", "transit/$filejpg", 100);
unlink("transit/$filesave"); // remove PNG image
I've put this at the beginning of the page, but the browser always displays the previous file, the one cancelled at the beginning, and being itself a JPG.
Strange enough (for me) this sequence is working perfectly on one server, not working on a different one!
I suppose it could be a case of asynchronous execution of PHP and JS inside the page.
How may I synchronize server and browser?
Thank you.
what happens if one process writes to the file and in the middle of the operation another process deletes it?
use .lock files so only 1 process tries to manipulate the files in question.
Related
I have a question about the application generate QR code image.
I have an application when clients click a button there will generate a QR code image, my way is store in the project library, then print <img> with the url to the screen. then clients can see it.
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
my code is bellow:
function generate_qrcode($url){
$filename = 'hante_qrcode.png';
$errorCorrectionLevel = 'L';
$matrixPointSize = 4;
//generate QR code image
$o = QRcode::png($url, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
echo "<pre>";
print_r($o);
print_r('<img src="hante_qrcode.png">');
}
if there get mix, how to solve this problem?
But I have a doubt, if there are multi clients using the QR code at the same time, whether there will get a mix?
yes
how to solve this problem?
there are two ways to solve this problem
you can provide unique name for every files like using timestamp using time() function or with user ID. cause as per you are passing parameters while generating qr code you need to store the file. without saving file also possible but in that case you can't configure pixel size and frame size. you can refer this for PHP QR code-Examples
don't store image on server and find some js to generate qr code directly from client side.
having a one demo for that check if you can use it
var qrcode = new QRCode("qrcode");
qrcode.makeCode('https://stackoverflow.com');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
<div id="qrcode"></div>
Of course it will be overwritten.
Solution 1
Create unique filename for every image. This way you can save your images for use later. Another benefit of this, you don't have to create image again for same url.
$filename = md5($url) . ".png";
if(!file_exists($filename)){
$o = QRcode::png($url, $filename, ...);
}
echo '<img src="'.$filename.'">';
Solution 2
If you don't want to save images for disk space reasons you can serve image directly. In your code, user sends request to index.php and fetch image address as response. After then browser makes another request to get image. You can return image rather than returning html.
// image.php
// Still we want to give uniqe filename because we can get another request while one request is processing
$filename = md5(microtime) . "_qr.png";
$o = QRcode::png($url, $filename, ...);
$image = file_get_contents($filename);
// remove the file after stored in a variable
unlink($filename);
header('Content-Type: image/jpeg');
header('Content-Length: ' . filesize($image));
echo $image;
// index.html
<img src="image.php?url=someurl">
I'm working on uploading a large file to my server. I have the iOS code that sets up an input and output stream. I can see that it successfully uploads to my videoUploader.php file because I have it making a response with the temporary file size and location. However, it executes the code, or so it seems before it gets the rest of the file. I cannot find any where online a full example of the server side handling.
$upload_directory = 'uploads/';
$temp_file = $_FILES['uploaded']['tmp_name'];
$filename = "testme.mp4";//basename($_FILES['uploaded']['name']);
$target_location = $upload_directory . $filename;
$size = $_FILES['uploaded']['size'];
move_uploaded_file($temp_file, $target_location);
All my files that it "moves" are 0 kb. It seems like there needs to be a way for me to tell php to wait until the temporary file is complete and then execute this code. I am an extreme novice at php, help please!
I have an image that is severed by a php script. I call it as such.
<img src="/index.php/image-name.jpg">
If the image is more than 5 minutes old my script will retrieve a new copy of the image from a data provider and then display the new image.
When the website that provides the images has a load and this script goes to fetch a new copy, it will often only display the top part of the image. Firebug will tell me that the image is corrupt or truncated. If I open the image in a new tab, my sever has a full copy. If I run the script a second time within 5 minutes it works perfectly.
It is looking like if it takes more than a certain amount of time to get the image it fails and only shows the top part. Any thoughts on how to make it wait longer before giving up? Or maybe I am completely on the wrong track with what is going wrong.
<?php
// get the image name from the uri
$path= $_SERVER['REQUEST_URI'];
$image = explode("/", $path);
$image=$image[3];//Get the file name
$image=str_replace('%20',' ', $image); //make it all spaces
$localimage='./road_images/'.$image; //where to find the image on the sever
// check if the image exists, this prevents some kinds of attacks
if (is_file($localimage)) {
$age = filemtime($localimage); // get the file age
if ($age < time() - (60*5)) { // 5 mins old
$simage='http://www.someplace/cams/'.$image;
$simage=str_replace(' ', '%20', $simage);//need to remove the spaces for URLs
copy($simage, $localimage);
}
// serve the image to the user.
$fp = fopen($localimage, 'r');
// send the right headers
header("Content-Type: image/jpg");
header("Content-Length: " . filesize($localimage));
// dump the picture and stop the script
fpassthru($fp);
exit();
}
else
{
echo("Error, no such file: '$image'");
}
?>
EDIT: Have discovered that by editing out
header("Content-Length: " . filesize($localimage));
It works as expected. Still trying to figure out why.
That was painful. I was passing the wrong Content-Length header value. Editing out the content length solved it so that it worked just fine. Considering it is static content, I do not know why what I have above does not work.
With more research figured out a way that works.
I put ob_start() near the start.
The new Content-Length header header('Content-Length: ' . ob_get_length());goes at the bottom, just before script exits.
Done that way it works every time and is nice to the browser.
I have created a app in php. How i can save the profile pictures of the users? Will that be a good idea to save them in db using base64 encode, like this
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAWgBaAAD/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAAB..." />
or anything else
It's a good idea/practice usually only for very small CSS images that are going to be used together (like CSS sprites) when IE compatibility doesn't matter, and saving the request is more important than cacheability.
It has a number of notable downsides:
Doesn't work at all in IE6 and 7.
Works for resources only up to 32k in size in IE8. This is the limit
that applies after base64 encoding. In other words, no longer than
32768 characters.
It saves a request, but bloats the HTML page instead! And makes
images uncacheable. They get loaded every time the containing page or
style sheet get loaded.
Base64 encoding bloats image sizes by 33%.
If served in a gzipped resource, data: images are almost certainly
going to be a terrible strain on the server's resources! Images are
traditionally very CPU intensive to compress, with very little
reduction in size.
Store images in your server and store the path to images in the database. This will reduce extra overload on database server.
Its better to store file path in database rather storing files in databases. Datbases are for structured data, not for blobs. Moreover database storage is usually more expensive than file system storage. Servers doesn't need any special coding or processing to access images in the file system
Copy base64 image "src" to a textbox using javascript. Post a form with base64 url textbox. Its better to save url in database rather saving a file in database.
You can save your base64 image by using this PHP code:
<?php
define('UPLOAD_DIR', 'images/');
$base64img=$_POST['base64img'];
if (stristr($base64img, "data:image/jpeg;base64,")) {
$base64img = str_replace('data:image/jpeg;base64,', '', $base64img);
$uid=time();
$data = base64_decode($base64img);
$file = UPLOAD_DIR . $uid.'.jpg';
file_put_contents($file, $data);
}elseif (stristr($base64img, "data:image/png;base64,")) {
$base64img = str_replace('data:image/png;base64,', '', $base64img);
$uid=time();
$data = base64_decode($base64img);
$file = UPLOAD_DIR . $uid.'.png';
file_put_contents($file, $data);
}elseif (stristr($base64img, "data:image/jpg;base64,")) {
$base64img = str_replace('data:image/jpg;base64,', '', $base64img);
$uid=time();
$data = base64_decode($base64img);
$file = UPLOAD_DIR . $uid.'.jpg';
file_put_contents($file, $data);
}
?>
I have this code that has an image tag. The img src is equal to "https://graph.facebook.com/<?php echo $user_id; ?>/picture". Is there anyway that I could get that image file and upload it to my server using the move_uploaded_image function with php?
No, move_uploaded_image moves files that were uploaded in a POST request.
If you want to get an image from a URL you need to make an HTTP request for it, e.g. via cURL.
Using PHP's imagecreate and file_get_contents and file_put_contents, you can create a blank image, get the image from the remote URL, then replace the blank image you created.
Similar to,
// Create a blank image
$imPath = "path/to/your/desired/image.jpg";
$im = imagecreatetruecolor(180, 151);
imagejpeg($im,$imPath);
// Download their fb image
$url = 'https://graph.facebook.com/' . $user['username'] . '/picture?type=large';
$bytes = file_put_contents($imPath, file_get_contents($url));
there is no function like move_uploaded_image . correct function is move_uploaded_file.
no. you can not get image file by this function .
first argument of this function is temp file name which is uploaded through Input type = file.
and another argument is the location where you want to put file....
you should refer $_FILES of php manual for more information....
http://php.net/manual/en/function.move-uploaded-file.php