Generating different content when creating picture on different machines - php

I want to create a picture from a passed binary string:
$fileName = uniqid().".".$imgType;
$fileName = "../tmp/".$fileName;
$f = fopen($fileName,'wb');
$picture = mb_convert_encoding($picture, "UUENCODE", "UTF-8");
fwrite($f, $picture);
fclose($f);
This works quite well on one machine with PHP 5.3.10-1ubuntu3.4. The picture is created properly. If try it on a different machine with PHP 5.3.19 the output is very strange. E.g. if you open the file with less then you will find \0 instead of desired ^# characters.
Why does this happen?
The binary string is part of a post request from a website using HTML5 Formdata encoded in both cases with UTF-8.

$picture = convert_uudecode($picture);

Related

Converting Base64 image to file return invalid image/data

I have been trying to convert a base64 string to image file using different methods for the past two days but every time the output file shows no image (it does show size and some encoded data).
Notes:
1. The browser interpreting the file as document and not image mime but I'm pretty sure this is not the problem as I tried to download the file localy and it is the same result.
2. I tried to compare the output results of online tools as codebeautify.org/base64-to-image-converter as it seems the data in the image file is different from my data.
3. Directory and files have 775 permissions and apache have chown
Base64 string here: https://dpaste.de/gP7D/raw
Method A:
list($type, $profile_image) = explode(';', $profile_image);
list(,$extension) = explode('/',$type);
list(,$profile_image) = explode(',', $profile_image);
if($extension == 'jpeg'){$extension = "jpg";}
$filePath = '../uploads/profile_images/'.$uname.'.'.$extension; // using uname instead of unique id to not expose the uqniue key/session
$profile_image = base64_decode($profile_image);
file_put_contents($filePath, $profile_image);
Method B:
list($type, $profile_image) = explode(';', $profile_image);
list(,$extension) = explode('/',$type);
list(,$profile_image) = explode(',', $profile_image);
$filePath = '../uploads/profile_images/'.$uname.'.'.$extension;
$ifp = fopen($filePath, 'wb');
fwrite($ifp, base64_decode($profile_image));
fclose($ifp);
What I'm doing wrong?
Update:
Apparently, Apache/php.ini had a maximum max_input_vars of 5000 while my base64 string were much higher. This post can be marked as resolved.
Seems that you have to remove the begining of your base64 encoded string, that isn't part of the base64 encoded data.
Remove the "data:image/jpeg;base64" from your base64 string.
I did some simple tests and got the image...
Apache/Php.ini file had a maximum max_input_vars of 5000 while my base64 string were much higher.

Coverting Hex to Image in PHP?

I am developing mobile app which talks with server via PHP Webservice. This is my first time using PHP. I managed to upload data in to database. Now i need to send an image to store it in ftp server. For that i converted image->hex and sent from my app.
Server Side
I got the hex code but not sure how to convert it in to an image and store in in ftp server. I am really struggling here. I googled it but couldn't find exact one.
Any help is much appreciated.
Convert the HEX string to binary:
$binary = pack("H*", $hex);
pack("H*", ...) is equivalent to hex2bin, which is available since PHP 5.4.
Write it to disk:
file_put_contents("file.png", $binary);
Suppose you have received a hex string in a page where you want to convert this hex to real image. Please check this code snippet will help you or not.
<?php
$hexpic=".......................
.....................";
# convert the hex string to binary
$data = pack("H" . strlen($hexpic), $hexpic);
#write the binary string into an image file
file_put_contents("sample.png", $data);
?>

export an image in base64

I want to create an image in php having the data encoded in base64
Use the code: file_put_contents('export/MyFile.png', base64_decode($img));
I read on the browser, but 7483 should be a larger number.
if I open the image was created only half (the other half is transparent)
if the variable $img contains a short string, it works.
if it contains a string too long the image will be created only partially.
why?
PS: if I use
$img = base64_decode($img);
$fp = fopen("export/MyFile.png", "w");
fwrite($fp, $img);
fclose($fp);
I have the exact same problems
thanks
file_put_contents('export/MyFile.png', base64_decode($img));
Should be:
file_put_contents('export/MyFile.png', base64_encode($img));
If you want to encode the image, you can't use the decode function.

Editing a .doc in PHP

I am trying to replace strings in a word document by reading the file into a variable $content and then using str_ireplace() to change the string. I can read the content from the file but I str_ireplace() does not seem to be able to replace the string. I assumed it would because the string is 'binary safe' according to the PHP documentation. Sorry, I am a beginner with PHP file manipulation so all this is quite new to me.
This is what I have written.
copy('jack.doc' , 'newFile.doc');
$handle = fopen('newFile.doc','rb');
$content = '';
while (!feof($handle))
{
$content .= fread($handle, 1);
}
fclose($handle);
$handle = fopen('newFile.doc','wb');
$content = str_ireplace('USING_ICT_BOX', 'YOUR ICT CONTENT', $content);
fwrite($handle, $content);
fclose($handle);
When I download the new file, it opens as it should in MS Word but it shows the old string and not the one that should be replaced.
Can I fix this issue? Is there any better tool I can use for replacing strings in MS Word thourgh PHP?
I have same requirement for Edit .doc or .docx file using php and i have find solution for it.
And i have write post on It :: http://www.onlinecode.org/update-docx-file-using-php/
copy('jack.doc' , 'newFile.doc');
$full_path = 'newFile.doc';
if($zip_val->open($full_path) == true)
{
// In the Open XML Wordprocessing format content is stored.
// In the document.xml file located in the word directory.
$key_file_name = 'word/document.xml';
$message = $zip_val->getFromName($key_file_name);
$timestamp = date('d-M-Y H:i:s');
// this data Replace the placeholders with actual values
$message = str_replace("client_full_name", "onlinecode org", $message);
$message = str_replace("client_email_address", "ingo#onlinecode.org", $message);
$message = str_replace("date_today", $timestamp, $message);
$message = str_replace("client_website", "www.onlinecode.org", $message);
$message = str_replace("client_mobile_number", "+1999999999", $message);
//Replace the content with the new content created above.
$zip_val->addFromString($key_file_name, $message);
$zip_val->close();
}
Maybe this would point you to the right direction: http://davidwalsh.name/read-pdf-doc-file-php
Solutions I've found so far (not tested though):
Docvert - works for Doc, free, but not directly usable
PHPWordLib - works for Doc, not free
PHPDocX - DocX only, needs Zend.
I am going to opt for PHPWord www.phpword.codeplex.com as I believe teachers are going to get Office 2007 next year and also I will try and find some way to convert between .docx and .doc through PHP to support them in the mean time.
If you can reach a web-service, look at Docmosis Cloud services since it can mailmerge a doc file with your data and give you back a doc/pdf/other. You can https post to the service to make the request so is pretty straight forward from PHP.
There is many way to handle word document file on linux
antiword - not much effective as it converts into plain text.
pyODconvert
open-office or liboffice - through UNO
unoconv utility - need to installation permission on server
There is one python script which is most usable for online file conversion but you need to convert those file through command line.
There is no specific and satisfied solution to handle word files by only using php code.
I hunted for a long time to reach at this suggestion.

PHP fputcsv with UTF-8 Problem

I'm trying to allow my clients view some of the MySQL data in Excel. I have used PHP's fputcsv() function, like:
public function generate() {
setlocale(LC_ALL, 'ko_KR.UTF8');
$this->filename = date("YmdHis");
$create = $this->directory."Report".$this->filename.".csv";
$f = fopen("$create","w") or die("can't open file");
fwrite($f, "\xEF\xBB\xBF");
$i = 1;
$length = count($this->inputarray[0]);
fwrite($f, $this->headers."\n");
// print column titles
foreach($this->inputarray[0] as $key=>$value) {
$delimiter = ($i == $length) ? "\n\n" : ",";
fwrite($f, $key.$delimiter);
$i++;
}
// print actual rows
foreach($this->inputarray as $row) {
fputcsv($f, $row);
}
fclose($f);
}
My clients are Korean, and a good chunk of the MySQL database contains values in utf8_unicode_ci. By using the above function, I successfully generated a CSV file with correctly encoded data that opens fine in Excel on my machine (Win7 in English), but when I opened the file in Excel on the client computer (Win7 in Korean), the characters were broken again. I tried taking the header (\xEF\xBB\xBF) out, and commenting out the setlocale, to no avail.
Can you help me figure this out?
If, as you say, your CSV file has "correctly encoded data" - i.e. that it contains a valid UTF-8 byte stream, and assuming that the byte stream of the file on your client's site is the same (e.g. has not been corrupted in transit by a file transfer problem) then it sounds like the issue Excel on the client's machine not correctly interpreting the UTF-8. This might be because it's not supported or that some option needs to be selected when importing to indicate the encoding. As such, you might try producing your file in a different encoding (using mb_convert_encoding or iconv).
If you get your client to export a CSV containing Korean characters then you'll be able to take a look at that file and determine the encoding that is being produced. You should then try using that encoding.
Try encoding the data as UTF-16LE, and ensure that the file has the appropriate BOM.
Alternatively, send your clients an Excel file rather than a CSV, then the encoding shouldn't be a problem
Try wrapping the text in each fwrite call with utf8_encode.
Then use what is suggested here: http://www.php.net/manual/en/function.fwrite.php#69566

Categories