tinypng error while trying to compress image - php

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.';
}
}

Related

PHP / Laravel - Tesseract OCR - Full path to file

I have successfully installed tesseract OCR and Imagick on my Forge server. However when I try to read an image, I get below error:
This is the error:
Error! The image "/home/forge/domain.com/storage/app/temp_files/ocrtotext_ijP1II9th2.jpeg" was not found.
The current __DIR__ is /home/forge/domain.com/vendor/thiagoalessio/tesseract_ocr/src
This is my code:
$text = (new TesseractOCR(storage_path() . '/app/temp_files/'.$imageName.'.jpeg'))
->lang('eng')
->psm($psm)
->run();
return $text;
What am I doing wrong? Why is tesseract looking in the /vendor/ folder and not in my storage/.. folder?
If you check the code at https://github.com/thiagoalessio/tesseract-ocr-for-php/blob/ea31d13143683c1b76e622f2b76be4c3e2e6c1af/src/FriendlyErrors.php
You'll see:
public static function checkImagePath($image)
{
if (file_exists($image)) return;
$currentDir = __DIR__;
$msg = array();
$msg[] = "Error! The image \"$image\" was not found.";
$msg[] = '';
$msg[] = "The current __DIR__ is $currentDir";
$msg = join(PHP_EOL, $msg);
throw new ImageNotFoundException($msg);
}
As you can see, TesseractOCR use the function file_exists
This function is returning false, which means that you setted the wrong filepath, or it's a broken symlink, or the user (Apache?) doesn't have access to the file, or other reason.

Uploading images from Windows Phone 8 app to a website

I want to be able to upload images from my Windows Phone 8 app, to my website.
For this, I followed the tutorial from this website:
https://vortexwolf.wordpress.com/2013/06/04/windows-phone-select-and-upload-image-to-a-website-over-http-post/
It all worked good, on the windows phone app side. But I have problems getting the website upload.php file working. In that tutorial, the author is using http://posttestserver.com/post.php?dir=wp7posttest to get the response of upload. In my case, using that URL for testing was working good, but when I put my website url, I get no response, and a crash in visual studio, with following error:
Additional information: The remote server returned an error: NotFound.
This error happens is thrown on this line:
response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
The upload.php on my website, looks like this:
if(isset($_GET['dir']))
{
$dir = $_GET['dir'];
if($_FILES['photo']['name'])
{
if(!$_FILES['photo']['error'])
{
$new_file_name = strtolower($_FILES['photo']['tmp_name']);
if($_FILES['photo']['size'] > (1024000))
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
if($valid_file)
{
move_uploaded_file($_FILES['photo']['tmp_name'], '../$dir/' . $new_file_name);
$message = 'Congratulations! Your file was accepted.';
}
}
else
{
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
}
}
echo $message;
}
I am out of ideas, and stuck on this problem for the second day. Any answers/ideas of how to get the website upload.php file working properly?
Thanks!

codeigniter s3 image upload works locally but not on production

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

PHP error on file upload

I keep recieving a PHP error, "Call to undefined function getallheaders() in /home/jbird11/public_html/grids/upload.php on line 8"
The upload script basically takes an image that is dragged into an area, and uploads it. When I drag the image, I get this message.
Here is the first 40 or so lines of the php file:
<?php
// Maximum file size
$maxsize = 1024; //Kb
// Supporting image file types
$types = Array('image/png','images/gif','image/jpeg');
$headers = getallheaders();
// LOG
$log = '=== '. #date('Y-m-d H:i:s') . ' ========================================'."\n"
.'HEADER:'.print_r($headers,1)."\n"
.'GET:'.print_r($_GET,1)."\n"
.'POST:'.print_r($_POST,1)."\n"
.'REQUEST:'.print_r($_REQUEST,1)."\n"
.'FILES:'.print_r($_FILES,1)."\n";
$fp = fopen('log.txt','a');
fwrite($fp, $log);
fclose($fp);
header('content-type: plain/text');
// File size control
if($headers['X-File-Size'] > ($maxsize *1024)) {
die("Max file size: $maxsize Kb");
}
// File type control
if(in_array($headers['X-File-Type'],$types)){
// Create an unique file name
$filename = sha1(#date('U').'-'.$headers['X-File-Name']).'.'.$_GET['type'];
// Uploaded file source
$source = file_get_contents('php://input');
// Image resize
imageresize($source, $filename, $_GET['width'], $_GET['height'], $_GET['crop'], $_GET['quality']);
} else die("Unsupported file type: ".$headers['X-File-Type']);
// File path
$path = str_replace('upload.php','',$_SERVER['SCRIPT_NAME']);
// Image tag
echo '<img src="'.$path.$filename.'" alt="image" />';
Any idea what is causing this error? Permissions perhaps? Permission are set to 755. You can see a working demo of this here: http://pixelcakecreative.com/grids/
Any idea how to fix this? Thanks in advance
From the docs:
This function is an alias for apache_request_headers(). Please read the apache_request_headers() documentation for more information on how this function works.
If you're not using apache (with php as a module), this function is not available.
It's an apache related function. Maybe You don't have needed extensions installed?
from the hosting company: It appears that that function is only supported when PHP is run as an Apache module. Our Shared and Reseller servers run PHP as CGI, and unfortunately this cannot be changed. We apologize for any inconvenience.
If that function is absolutely required for your site, you will need to consider upgrading to a VPS, in which case PHP can be installed however you like.
you can use this code to be sure you have such a function not depending on server software configuration:
if (!function_exists("getallheaders"))
{
function getallheaders()
{
$headers = "";
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == "HTTP_")
{
$headers[str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($name, 5)))))] = $value;
}
}
return $headers;
}
}

PHP's imagecreatefromstring function not working on Amazon Linux Server

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

Categories