I have created a script in which a i have to create a image at runtime using a 64bitencoded string .i m using imagecreatefromstring function of PHP but it works in my Windows XAMPP based PHP , but not on my cloud side applications which i deployed on Amazon cloud running SUSE version of Linux.
Can u give me any suggestion to overcome the problem.
Or is there any other function which is capable to create the image from the encoded string passed to it.
Thanks in adv
I am using following code
<?php
require ('../dbconfig/dbConfig.php');
$gameId = $_POST["gameId"];
$username = $_POST['email'];
$imagedata = $_POST['imagedata'];
$uploaddir = './../blogdata/i/';
$countSql = mysql_query("select max(_id) as fileName from blog_data ");
while($rowCommentData = mysql_fetch_assoc($countSql))
{
$num = $rowCommentData["fileName"];
$file = ++$num.".png";
$filedb = $uploaddir .$file;
}
/* $imagedata= 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';*/
$imagedata= base64_decode($imagedata);
if(($img = #imagecreatefromstring($imagedata)) !== FALSE)
{
if(imagepng($img,$filedb))
{
imagedestroy($img);
$sql="Insert into blog_data (game_id,text,type,username)".
"Values('$gameId','$file','i','$username')";
$result=mysql_query($sql);
if($result == 1)
{
echo $file;
}
else
{
echo "error2";
}
}
else {
echo "error1";
}
}
else
{
echo "error0";
}
?>
By running PHP info there i got this information
The PHP needs to have the libgd extension installed and loaded. Check phpinfo() if it's there. You probably can install it via yum. The package should be called php5-gd
From your code, it seems that you dont have the GD extension installed. Please check the phpinfo output and look for the GD extension
You will need root access to the server, which I don't think you get with Amazon's cloud service. You will need to recompile php with --with-gd flag.
http://www.php.net/manual/en/image.installation.php
Related
I'm trying to compress image after upload it in my server and everything going as expected but when I upload my changes from local machine to online server that gives me this error
Call to undefined function Tinify\curl_version()
so my source code in my local machine below:
require_once(APPPATH.'libraries/tinify-php-master/lib/Tinify/Exception.php');
require_once(APPPATH.'libraries/tinify-php-master/lib/Tinify/ResultMeta.php');
require_once(APPPATH.'libraries/tinify-php-master/lib/Tinify/Result.php');
require_once(APPPATH.'libraries/tinify-php-master/lib/Tinify/Source.php');
require_once(APPPATH.'libraries/tinify-php-master/lib/Tinify/Client.php');
require_once(APPPATH.'libraries/tinify-php-master/lib/Tinify.php');
\Tinify\setKey("4R8QNHlOax0Mcp7lho4QiOBUnTjJuZYj");
if($this->upload->do_upload("file")){
$imageDetailArray = $this->upload->data();
$pic = $imageDetailArray['file_name'];
$unoptimized_img_loc = 'uploads/'.$pic;
$img_url = base_url() . "include/" . $unoptimized_img_loc;
try {
$source = \Tinify\fromFile($img_url);
$optimized_img_name = 'compressed_imgs/users_profile_pic/profile_pic'.$pic;
$resized = $source->resize(array(
'method' =>'fit',
'width' => 300,
'height' =>300
));
$resized->toFile($_SERVER['DOCUMENT_ROOT']."/html/include/".$optimized_img_name);
}catch (\Tinify\Exception $e){
print_r($e);exit();
}
$dataIn['logo'] = $optimized_img_name;
}
and the same code uploaded to my VM server and that doesn't work
Any help please
And thanks in advance,
Update 2017-06-01 09:30AM CEST
Be sure that curl is activated in your php.ini (or maybe curl.ini?):
extension=curl.so
After adding this, restart your webserver.
You're missing an installed curl package. Install for example php-curl.
You didn't mention your OS and version, but here you'll find installation and configuration details: http://php.net/manual/en/book.curl.php
Below, citations from https://board.s9y.org/viewtopic.php?f=4&t=20857 with the same casus:
I just get this error when uploading an image with the new plugin installed: Fatal error: Call to undefined function Tinify\curl_version() in /var/www/vps.hommel-net.de/serendipity/plugins/serendipity_event_tinypng/tinify-php/lib/Tinify/Client.php on line 11. The image is in the media library after this error but it's not compressed.
Is it possible that you have no php curl module active? A package like php-curl?
I will have a look whether the Tinyfy-Client really needs it, but that is possible.
That was the thing. The debian package is php5-curl. After installing it the error is gone.
It seems that the API of TinyPNG has counted my tries with the error, too.
Keep in mind the API of TinyPNG counts your number of requests!
require_once("vendor/autoload.php");
\Tinify\setKey("B2JCcCK0FqVfDrPyrjX5QW1jYqF7n4vl"); //pass your actual API key
if (isset($_POST['submit'])) {
$supported_image = array('image/gif', 'image/jpg', 'image/jpeg', 'image/png');
if (in_array($_FILES['myfile']['type'], $supported_image)) {
$src_file_name = $_FILES['myfile']['name'];
if (!file_exists(getcwd().'/uploads')) {
mkdir(getcwd().'/uploads', 0777);
}
move_uploaded_file($_FILES['myfile']['tmp_name'], getcwd().'/uploads/'.$src_file_name);
//optimize image using TinyPNG
$source = \Tinify\fromFile(getcwd().'/uploads/'.$src_file_name);
$source->toFile(getcwd().'/uploads/'.$src_file_name);
echo "File uploaded successfully";
} else {
echo 'Invalid file format.';
}
}
I need to convert some pdf files into HTML. I downloaded pdftohtml for PHP but I don't know how to use it. I am trying to run it with this code:
<?php
include 'pdf-to-html-master/src/Gufy/PdfToHtml.php';
$pdf = new \Gufy\PdfToHtml;
$pdf->open('1400.pdf');
$pdf->generate();
?>
This results in a blank web page.
What do I need to modify? What is the correct code to run this script?
First option is using poppler utils
<?php
// if you are using composer, just use this
include 'vendor/autoload.php';
// if not, use this
include 'src/Gufy/PdfToHtml.php';
// initiate
$pdf = new \Gufy\PdfToHtml;
// opening file
$pdf->open('file.pdf');
// set different output directory for generated html files
$pdf->setOutputDirectory('/your/absolute/directory/path');
// do this if you want to convert in the same directory as file.pdf
$pdf->generate();
// you think your generated files is annoying? simple do this to remove the whole files
$pdf->clearOutputDirectory();
?>
Download library from here
Second option could be using pdf.js
PDFJS.getDocument('helloworld.pdf')
I'm the maintainer of the package. The package has updated. Have you already used the latest version? And, if you're using Windows, please read again the doc. Also, please do not download directly from github, use composer instead.
include 'vendor/autoload.php';
use Gufy\PdfToHtml\Pdf;
use PHPHtmlParser\Dom;
use DateTime;
public function parsepdf(Request $request)
{
$pdf = new Pdf($request->file('csv_file'));
$html = $pdf->html();
$dom = new Dom;
$total_pages = $pdf->getPages();
if ($total_pages == 1) {
$html->goToPage(1);
$dom->load($html);
$paragraphs = $dom->find('p');
$paragraphs = collect($paragraphs);
foreach($paragraphs as $p){
$datestring = preg_replace('/\xc2\xa0/', ' ', trim($p->text));
echo $datestring;
}
}
Above code for Convert pdf to html in laravel
composer require gufy/pdftohtml-php:~2
Poppler-Utils (if you are using Ubuntu Distro, just install it from
apt ) sudo apt-get install poppler-utils
I use wkhtmltopdf and it works okay. You can download it from here: http://wkhtmltopdf.org/downloads.html
I installed it in Linux and I use it like this:
$url = "https://www.google.com";
$command = "/usr/bin/wkhtmltopdf --load-error-handling ignore --disable-smart-shrinking -T 5mm -B 5mm -L 2mm -R 2mm --page-size Letter --encoding utf-8 --quiet";
$filename = '[file path].pdf';
if (file_exists($filename)) {
unlink($filename);
}
$output = shell_exec($command . " $url " . $filename);
echo $output;
Hope this helps.
I'm uploading images from my Android app to my server. The app uses the android camera intent and upload via PHP script is ok.
I want to verify if the uploaded files are real images, I'm not checking the extension but the mimetype (I suppose this is the best way to do it, tell me if I'm wrong).
I'm using a Slackware Linux Apache server and I'm trying this code:
....
$finfo = finfo_open(FILEINFO_MIME, '/etc/httpd/magic');
....
fwrite($fp, finfo_file($finfo, "file.jpg"));
....
But I'm getting "application/octet-stream; charset=binary" instead of "image/jpeg; charset=binary" which is given from "file -i file.jpg" (shell command).
What's the problem?
Solved using $finfo = finfo_open(FILEINFO_MIME); instead of the other line. I think the default magic file is not the same that I was specifing.
As refered on www.php.net/manual/en/ref.fileinfo.php:
<?php
function is_jpg($fullpathtoimage){
if(file_exists($fullpathtoimage)){
exec("/usr/bin/identify -format %m $fullpathtoimage",$out);
//using system() echos STDOUT automatically
if(!empty($out)){
//identify returns an empty result to php
//if the file is not an image
if($out == 'JPEG'){
return true;
}
}
}
return false;
}
?>
Alternately, if you've got execution rights and want to use a "hacky" solution, you can simply do what you've already done (using file -i path with shell_exec):
<?php
function shell_get_mime_type($path) {
if (is_readable($path)) {
$command = 'file -i "' . realpath($path) . '"';
$shellOutput = trim(shell_exec($command));
//eg. "nav_item.png: image/png; charset=binary"
if (!empty($shellOutput)) {
$colonPosition = strpos($shellOutput, ':');
if ($colonPosition !== false) {
return rtrim(substr($shellOutput, $colonPosition + 1));
}
return $shellOutput;
}
}
return false;
}
?>
Try to use function mime_content_type().
I have a model called photos_model.php which does the backend work to upload images to s3
here is the function that uploads to s3
public function uploadPhoto($photo){
$this->load->model("misc_model");
//required vars
$tmp_name = $photo["tmp_name"];
$name = $photo["name"];
$type = $photo["type"];
$size = $photo["size"];
$error_upload = $photo["error"];
$random_str = $this->misc_model->generateRandomNumber(20, true, true);
$this->s3->putBucket($this->config->item("bucket"), S3::ACL_PUBLIC_READ);
if($this->s3->putObjectFile($tmp_name, $this->config->item("bucket") , 'photos/'.$random_str.".jpg", S3::ACL_PUBLIC_READ) ){
echo 'ok';
}else{
echo 'fail';
}
}
the s3 library:
taken from here http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/
this will work on my local machine MAC OS X Lion
but when i try the script on an amazon ec2 machine it simply doesnt print anything... like nothing...
Solution:
its always the error_log... it was reporting PHP Fatal error: Call to undefined function imagecreatefromjpeg() so i installed GD library and we're good
I am using php 5.3.2 with uploadprogress extension to get a progressbar during upload using zend framework. Even the demo provided with zend is not working. code in zend example -
if (isset($_GET['uploadId'])) {
set_include_path(realpath(dirname(__FILE__) . '/../../../library')
. PATH_SEPARATOR . get_include_path());
require_once 'Zend/ProgressBar.php';
require_once 'Zend/ProgressBar/Adapter/JsPull.php';
require_once 'Zend/Session/Namespace.php';
$data = uploadprogress_get_info($_GET['uploadId']);
$bytesTotal = ($data === null ? 0 : $data['bytes_total']);
$bytesUploaded = ($data === null ? 0 : $data['bytes_uploaded']);
$adapter = new Zend_ProgressBar_Adapter_JsPull();
$progressBar = new Zend_ProgressBar($adapter, 0, $bytesTotal, 'uploadProgress');
if ($bytesTotal === $bytesUploaded) {
$progressBar->finish();
} else {
$progressBar->update($bytesUploaded);
}
}
uploadprogress_get_info always returns null. I thought something is wrong with my code so i downloaded the working sample available at http://labs.liip.ch/uploadprogresssimple/index.php but even in that case in uploadprogress_get_info always return null.
My uploadprogress config values
uploadprogress support enabled
Version 1.0.1
uploadprogress.file.contents_template /tmp/upload_contents_%s
uploadprogress.file.filename_template /tmp/upt_%s.txt
uploadprogress.get_contents 1
While googling around i found uploadprogress extension has some issue with Suhosin Patch < 0.9.26 but i am using Suhosin Patch 0.9.9.1
Are you testing this locally? I mean testing this on localhost? try upload to a remote host, or test a rather large file (make sure you bumped upload_max_filesize and post_max_size before).
While googling around i found uploadprogress extension has some issue with Suhosin Patch < 0.9.26 but i am using Suhosin Patch 0.9.9.1
I am on the same Suhosin version and having this issue. Also tried apc_fetch() which suhosin has also crippled.
What a stupid patch. To remove it I have to rebuild php. For f*** sake!
Same problem for me as well though I am using Zend_File_Transfer_Adapter to handle the upload. Its useful because you can validate the uploaded file(s) too :)
A couple of useful methods that could help you at least on the configuration of your server is:
Zend_File_Transfer_Adapter_Http::isApcAvailable();
Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable();
Run phpinfo to check if the UploadProgress is installed and the tmp directory is writable on the server. Meaning the "uploadprogress.file.filename_template" refers to the right tmp folder in the phpinfo.
You can run the following code to check if uploadprogress is writable.
<div id="status" style="border: 1px black solid;<?php
$templateini = ini_get("uploadprogress.file.filename_template");
$testid = "thisisjustatest";
$template = sprintf($templateini, $testid);
$templateerror = false;
if ($template && $template != $templateini && #touch($template) && file_exists($template)) {
// print '('.$templateini.' is writable. The realpath is ' . str_replace($testid,"%s",realpath($template)) .')';
unlink($template);
} else {
$templateerror = true;
}
if (function_exists("uploadprogress_get_info")) {
if ($templateerror) {
print 'background-color: red;"';
print ">Problem. ";
if ($template == $templateini) {
print "uploadprogress.file.filename_template ($templateini) doesn't have an %s in it for making unique temporary files. Please adjust.<br/>";
} else {
print "$templateini is NOT writable. <br/>Please make sure the directory exists and is writable for the webserver. <br/>
Or adjust the ini setting 'uploadprogress.file.filename_template' to a correct path.";
}
} else {
print 'background-color: green;">The uploadprogress extension is installed and initial checks show everything is good';
}
} else {
?>
background-color: red;">The uploadprogress extension is not installed.
<?php } ?>
</div>
If the above code gives error, point to the correct tmp directory in the php.ini file. The following line was added in the php.ini file for Xampp tmp directory on windows localhost machine. uploadprogress.file.filename_template = C:\xampp\tmp\some_name_%s.txt
Now run the Zend demo it should work or run the following to get var_dump manually.
var_dump(uploadprogress_get_info($_GET['uploadId']));