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");
-----------------------------------------------^
Related
We are converting my videos using FFmpeg using the bellow PHP code and we are tring to add a water mark or the company logos while converting.
We searched the internet for a code or a command to solve our issue but unfortunately we cannot
The watermark that we want to add is located on: https://propeview.com/wp-content/uploads/2021/08/logo-light-5.png
PHP code we use
<?php
$uploads_dir = 'original/';
$file_name = basename($_FILES['file']['name']);
$output_name = explode('.', $file_name)[0];
$uploaded_file = $uploads_dir . $file_name;
$convert_status = ['mp4' => 0, 'webm' => 0];
if(isset($_POST['submit'])) {
if(move_uploaded_file($_FILES['file']['tmp_name'], $uploaded_file)) {
// Make sure to get the correct path to ffmpeg
// Run $ where ffmpeg to get the path
$ffmpeg = '/bin/ffmpeg';
// MP4
$video_mp4 = $output_name . '.mp4';
exec($ffmpeg . ' -i "' . $uploaded_file . '" -vcodec h264 -acodec libfdk_aac "./converted/' . $video_mp4 . '" -y 1>convert.txt 2>&1', $output, $convert_status['mp4']);
// Debug
// echo '<pre>' . print_r($output, 1) . ' </pre>';
// Debug
// echo '<pre>' . print_r($output, 1) . ' </pre>';
}
}
?>
I am working on a php code as shown below which is converting mp4 files into mp3 using ffmpeg. After conversion, everything goes into outgoing_folder.
<?php
const DS = '/';
$src_dir = 'incoming_folder'; /* Place where mp4 file is present */
$destination = 'outgoing_folder'; /* Place where mp3 files come after conversion from mp4 */
$mp3_processed = 'podcast_mp3_processed'; /* Place where I also want mp3 files */
foreach ($mp4_files as $f)
{
$parts = pathinfo($f);
switch ($parts['extension'])
{
case 'mp4' :
$filePath = $src_dir . DS . $f;
system('ffmpeg -i ' . $filePath . ' -map 0:2 -ac 1 ' . $destination . DS . $parts['filename'] . '.mp3', $result); /** Place where mp3 file goes after conversion from mp4 **/
if ($result)
{
// Do something with result if you want
// log for example
}
break;
case 'mp3' :
// copy($f, $destination. DS . $parts['filename']. '.' . $parts['extension']);
copy($f, $destination . DS . $parts['filename'] . '.mp3');
copy('outgoing_folder', 'podcast_mp3_processed'); /* Line #A */
break;
}
}
?>
Problem Statement:
I am wondering what changes I need to make in the php code above so that when all mp4 files have been converted to mp3 then it should also go to podcast_mp3_processed folder.
At this moment it is going outgoing_folder but I also want it to go to podcast_mp3_processed folder. I tried with the code at Line#A but it doesn't seem to work.
You are trying to copy a directory and the PHP copy function only handles files.
Ref: https://www.php.net/manual/en/function.copy.php
You can change your Line #A by duplicating the line before it and simply changing the directory variable $destination with $mp3_processed.
copy($f, $mp3_processed. DS . $parts['filename'] . '.mp3');
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).
I'm unable to make imagick works with local xampp.
Error
Fatal error: Uncaught exception 'ImagickException' with message 'NoDecodeDelegateForThisImageFormat
Code
$im = new imagick();
$im->setResolution(300, 300);
$im->readimage($base_dir . 'files/PDF/test.pdf');
$im->setIteratorIndex($i);
$im->setImageFormat('jpeg');
$newimgname = time();
$im->resizeImage(500, 500, Imagick::FILTER_LANCZOS, 1);
$im->cropImage(100, 100, 0, 0);
$thumbnail = $im->getImageBlob();
Imagick calls the ImageMagick library to do all it's processing of images. The Image Magick library does not actually handle PDFs itself, it calls GhostScript to process them and generate a PNG or Jpeg which Image Magick then reads.
The NoDecodeDelegateForThisImageFormat is saying that Image Magick is unable to call the delegate program that it thinks it should be delegating the decoding to i.e. GhostScript.
The solution is to install GhostScript through yum or apt, and it 'should' work.
If it still doesn't work you should check what is in the delegates file for Image Magick (http://www.imagemagick.org/source/delegates.xml) for the PDF entry and make sure that it is callable from a command prompt - i.e. to check that Image Magick will also be able to find it.
From my Server admin:
You may want to try GraphicsMagick as an alternative to Image Magick.
Graphics Magick Homepage
Even after installing GhostScript, we could not get the imagick code to work after switching from AWS to Azure, getting the Delegate error. We ended up converting the PHP code to Image Magick command line, using the function execInBackground to run the command (PHP.net Exec() Page)
NOTE: The command line did not work using exec alone. The php script would not terminate normally.
//from PHP.net
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
//create a thumbnail from the first page of PDF
//old php code
/*
$image_magick = new imagick();
$image_magick->setbackgroundcolor('white');
$image_magick->readImage($file_path . "[0]");
$image_magick = $image_magick->flattenImages();
$image_magick->setResolution(300,300);
$image_magick->thumbnailImage(102, 102, true);
$image_magick->setImageFormat('jpg');
$image_magick->writeImage($thumbnail_path);
*/
//command line syntax
$cmd = "magick convert " . chr(34) . $file_path . "[0]" . chr(34) . " -background white -flatten -resample " . chr(34) . "300x300" . chr(34) . " -thumbnail " . chr(34) . "102x102" . chr(34) . " -format jpg -write " . chr(34) . $thumbnail_path . chr(34);
execInBackground($cmd);
Is there a way to "pause" php to wait for a file to upload before manipulating it?
I am using this to upload photos:
move_uploaded_file($_FILES[$PhotoField]["tmp_name"], $BrandDirectory . "/" . $NewPhotoName);
That part seems to be working well. The issue is that after that I am resizing the photo that is uploaded using:
$image = new SimpleImage();
$image->load($BrandDirectory . "/" . $NewPhotoName);
$image->resizeToWidth(1024);
$image->save($BrandDirectory . "/" . $NewPhotoName);
That part seems to work as well to, as long as the photo has finished uploading.
So, it seems that I need some way for everything to wait before the upload finishes.
if(move_uploaded_file($_FILES[$PhotoField]["tmp_name"], $BrandDirectory . "/" . $NewPhotoName)){
$image = new SimpleImage();
$image->load($BrandDirectory . "/" . $NewPhotoName);
$image->resizeToWidth(1024);
$image->save($BrandDirectory . "/" . $NewPhotoName);
}