File upload error while editing the file using exec command - php

I am using exec command to create a thumbnail of pdf.
At the time of adding code works fine:
File is uploading in folder
Added in db.
But with the following error message.
Warning
Warning: Failed to move file!
Error
Error moving file
At the time of edit:
File is uploading in folder
Not updated in db.
And getting error message with the following error message:
Warning
Warning: Failed to move file!
Error
Error moving file
Code:
$uploadPath = JPATH_ADMINISTRATOR
. '/components/com_ets_fast_track/assets/buildings/'
. $filename;
$fileTemp = $file['tmp_name'];
if(!JFile::exists($uploadPath)){
if (!JFile::upload($fileTemp, $uploadPath)){
JError::raiseWarning(500, 'Error moving file');
return false;
} else {
//name the thumbnail image the same as the pdf file
$pdfWithPath = $uploadPath;
$thumbDirectory = JPATH_ADMINISTRATOR
. '/components/com_ets_fast_track/assets/buildings/';
$thumb = basename($filename, ".pdf");
//add the desired extension to the thumbnail
$thumb = $thumb.".jpg";
$cmd="convert \"{$uploadPath}[0]\" -geometry 227x295 -density 222x294 -quality 100 -channel RGBA -bordercolor white -border 1x1 -fill none -draw matte 0,0 floodfill $thumbDirectory$thumb";
exec($cmd);
}
}

Related

PHP convert EPS file to PNG/SVG

I have an eps file and I want it to convert svg or png format.
I have installed PHP Imagick extension in my localhost and also installed the GhostScript.
I have tried the following code
for eps to svg code
$file_path = realpath('./sample.eps');
$dest_path = getcwd() . '/sample.svg';
$command = "inkscape --file=$file_path --export-plain-svg=$dest_path";
$output = shell_exec($command);
var_dump($output);
var_dump(is_file('./sample.svg'));
this code return null and bool(false)
for eps to png code
$path = 'http://localhost/Sample.eps';
$save_path = 'http://localhost/imprint_option_2E_c.png';
$image = new Imagick();
$image->setResolution(300,300);
$image->readimage($path);
$image->setBackgroundColor(new ImagickPixel('transparent'));
$image->scaleImage(600, 270);
$image->setImageFormat("png");
$image->writeImage($save_path);*/
error is : Uncaught ImagickException: Failed to read the file

Converting Pdf file to images with Imagick and PHP

Program to load pdf image and at the same time convert them to jpg using Imagick.But couldnt convert and load it in Destination directory.
$name = $_FILES['file']['name'];
$fileName = substr($_FILES['file']['tmp_name'], 5).".".$ext;
date_default_timezone_set('UTC');
$fileDate = date('d.m.Y');
$fileSize = $_FILES['file']['size'];
$folder = $_POST['folder'];
$uploadfile1="$media_dir/$fileName";
$imagick = new imagick();
$imagick->readImage($uploadfile1);//line 149
$imagick->setImageFormat('jpg');
foreach($imagick as $i=>$imagick)
{
$imagick->writeImage($uploadfile1. " page ". ($i+1) ." of ". $pages.".jpg");
}
Error
Fatal error: Uncaught ImagickException: unable to open image
`/opt/ama/mediaFiles/phpe765pr.pdf': No such file or directory #
error/blob.c/OpenBlob/2701 in
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/ama/modules/mediaFiles/uploadFile.php:149Stack
trace:#0
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/ama/modules/mediaFiles/uploadFile.php(149):
Imagick->readimage('/opt/gpssi/medi...')#1 {main} thrown in
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/gpssi/modules/mediaFiles/uploadFile.php on line 149
You have a problem with the path of
/opt/ama/mediaFiles/phpe765pr.pdf
Make sure the path exists and the necessary privileges are given to all the folders in the path along the file to read it.

Create PDF thumbnail with Imagick and write to file

I'm trying to create a pdf thumbnail with Imagick and save it on the server in the same location as the pdf. The code below works fine as is. The problem is that I don't want to echo the image. But if I remove the echo statement, the resulting jpg file contains errors and is unreadable. How can I create the thumbnail and write to a file without sending it to the browser?
$pdfThumb = new \imagick();
$pdfThumb->setResolution(10, 10);
$pdfThumb->readImage($filePath . $fileName . $fileExt . '[0]');
$pdfThumb->setImageFormat('jpg');
header("Content-Type: image/jpeg");
echo $pdfThumb;
$fp = fopen($filePath . $fileName . '.jpg', "x");
$pdfThumb->writeImageFile($fp);
fclose($fp);
DaGhostman Dimitrov provided some helpful code on #16606642, but it doesn't work for me for some reason.
I would try:
$pdfThumb = new imagick();
$pdfThumb->setResolution(10, 10);
$pdfThumb->readImage($filePath . $fileName . $fileExt . '[0]');
$pdfThumb->setImageFormat('jpg');
$fp = $filePath . $fileName . '.jpg';
$pdfThumb->writeImage($fp);
Bonzo's answer requires imagick on the webserver. If imagick is not on the webserver you can try to execute imagemagick from the commandline by php command exec():
exec('convert -thumbnail "178^>" -background white -alpha remove -crop 178x178+0+0 my_pdf.pdf[0] my_pdf.png')
And if you like to convert all pdfs in one step from same folder where your script is located try this:
exec('for f in *.pdf; do convert -thumbnail "178^>" -background white -alpha remove -crop 178x178+0+0 "$f"[0] "${f%.pdf}.png"; done');
In this examples I create a png thumbnail 178x178 pixel from the first page (my_pdf.pdf[0] the 0 means first pdf page).

Uploaded image: converting format

I want to convert a uploaded image to its original format, (jpg, jpeg rpng, etc.) but I need to re-size these images to be 150px width and 210px height. Is it possible to change the size while copying, or do I have to convert it?
This was unsucessful:
$uploaddir1 = "/home/myweb/public_html/temp/sfds454.png";
$uploaddir2 = "/home/myweb/public_html/images/sfds454.png";
$cmd = "/usr/bin/ffmpeg -i $uploaddir1 -vframes 1 -s 150x210 -r 1 -f mjpeg $uploaddir2";
#exec($cmd);
You can use gd instead of ffmpeg. To convert or resize an image, see this example: http://ryanfait.com/resources/php-image-resize/resize.txt
Php lib for gd:
http://php.net/manual/en/function.imagecopyresampled.php
There is also some samples of resizing scripts in that page.
I recently had to solve just this problem, and implemented this simple caching solution:
<?php
function send($name, $ext) {
$fp = fopen($name, 'rb');
// send the right headers
header("Content-Type: image/$ext");
header("Content-Length: " . filesize($name));
// dump the picture and stop the script
fpassthru($fp);
exit;
}
error_reporting(E_ALL);
ini_set('display_errors', 'On');
if (isset($_REQUEST['fp'])) {
$ext = pathinfo($_REQUEST['fp'], PATHINFO_EXTENSION);
$allowedExt = array('png', 'jpg', 'jpeg');
if (!in_array($ext, $allowedExt)) {
echo 'fail';
}
if (!isset($_REQUEST['w']) && !isset($_REQUEST['h'])) {
send($_REQUEST['fp']);
}
else {
$w = $_REQUEST['w'];
$h = $_REQUEST['h'];
//use height, width, modification time and path to generate a hash
//that will become the file name
$filePath = realpath($_REQUEST['fp']);
$cachePath = md5($filePath.filemtime($filePath).$h.$w);
if (!file_exists("tmp/$cachePath")) {
exec("gm convert -quality 80% -colorspace RGB -resize " .$w .'x' . $h . " $filePath tmp/$cachePath");
}
send("tmp/$cachePath", $ext);
}
}
?>
Some things that I noticed:
Graphicsmagick converted much faster than imagemagick, although I did not test conversion with cuda processing.
For the final product I re-implemented this code in ASP using the language's native graphics library. This was much faster again but would break if out-of-memory errors occurred (worked fine on my workstation, but wouldn't work on 4GB RAM server).

Image creation from pdf

I am creating a cron job in which I copy files from one directroy to another. The cron job works fine it copies the files to the import directory. The structure of import directory is like this.
import
folder1
pdf_file1
folder2
pdf_file2
I am trying to create a thumbnail image for each pdf file that is copied to the folders, I installed ImageMagik and my php code to create a thumnail image is
if ($preview != "") {
$copy_res = ftp_get("files/upload/" . $ftp_upload["path"] . "/" . $preview, "files/import/" . $ftp_upload["preview"] . "/" . $preview);
$md5_checksum = md5("files/import/" . $ftp_upload["path"] . "/" . $preview);
} else {
//$pdf_file = 'files/import/folder1/pdf_file1';
$pdf_file = 'files/import/' . $ftp_upload["path"]."/".$filename_new;
if (file_exists($pdf_file)){
echo 'I am here';
exec("convert -density 300 " . $pdf_file . "[0]" . $filename_new . ".jpg");
}
}
When I run the code it comes to the else statement and echo the line but its not executing the exec command.
I checked the convert command from Command Prompt for that I navigated to
C:\wamp\www\project\files\import\folder1
and then I run this command
exec("convert -density 300 test.pdf[0] test.jpg");
From the command prompt it worked but in my code it s not working .
Is there any issue with the path ? because the file is alreday copied when I try to create the thumbnail for it.
The ImageMagik is installed in
C:\Program Files(x86)\ImageMagik
Any ideas what I am doing wron ? and is there any other faster way to create thumbail image for pdf ?
Thanks in advance
I now write my code seperating the code form the exec() so that you can display the information you are sending to Imagemagick.
I would also try setting the filenames outside the exec()
$input = $pdf_file . "[0]";
$output = $filename_new . ".jpg"
$cmd = " -density 300 $input -thumbnail 200x300 $output ";
// echo $cmd."<br>";
exec("convert $cmd");
Added a resize but it will keep the image proportions - if you need an exact size you could crop the image or pad it with a colour.
There seems to be a missing space that separates the path to the pdf file and the new name for the image file.
exec("convert -density 300 " . $pdf_file . "[0]" . $filename_new . ".jpg");
Should be:
exec("convert -density 300 " . $pdf_file . "[0] " . $filename_new . ".jpg");
-----------------------------------------------^

Categories