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
Related
I need to convert all PDFs file of a directory into jpgs.
Like:
1.pdf
2.pdf
3.pdf
into
1.jpg
2.jpg
3.jpg
With this script I can find all PDFs.
$dir_name = "";
$pdfs = glob($dir_name."*.pdf");
foreach($pdfs as $pdf) {
echo $pdf;
}
But how can I convert multiple files?
$imagick = new Imagick();
$imagick->setResolution(300, 300);
$imagick->readImage('???');
$imagick->writeImages('???', false);
I have an app that generates svg and then converts that svg to png image. This works on the local virtual host with ubuntu os but when I am trying to make it work on my windows xampp it generates a corrupted file.
My code:
$svg = new SimpleXMLElement($data);
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
$svg = $svg->asXML();
if(!file_exists(TMP_DIRECTORY)) {
mkdir(TMP_DIRECTORY, 0775);
}
$filePath = TMP_DIRECTORY . DS . base64_encode(time()) . ".svg";
file_put_contents($filePath, $svg);
$contents = file_get_contents($filePath);
$im = new Imagick();
$im->setResolution(300, 300);
$im->setBackgroundColor(new ImagickPixel('transparent'));
$im->setFont("C:/xampp56/htdocs/daimler/fonts/DaimlerCS/DaimlerCS-Demi.otf");
try {
if (!$im->readImageBlob($contents)) {
echo 'Cannot read svg file!';
}
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
$im->setImageFormat("png24");
$filename = TMP_DIRECTORY . DS . 'test.png';
$im->writeImage($filename);
$im->clear();
$im->destroy();
expected result(generated in virtual host ubuntu os):
Actual Result in windows xampp server:
I hope someone can help me point out where the problem is because there are no error messages shown.
I followed the instructions on this site and installed image magick: https://mlocati.github.io/articles/php-windows-imagick.html
I have installed the version of image magick defined in the phpinfo imagick class description
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 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);
}
}
So what I'm trying to do is:
- given an image url -> convert image to png
- zip resulting png
I have the following code which successfully does the conversion and zipping (I'm going to expand it later to test the extension to auto convert formats):
$file = "../assets/test.jpg";
$img = imagecreatefromjpeg($file);
imagePng($img, "files/temp.png" );
$zip->addFile( "files/temp.png", "test.png" );
What I want to know is, is it possible to do this without creating a copy of image before it's zipped
See ZipArchive::addFromString().
$file = "../assets/test.jpg";
// capture output into the internal buffer
ob_start();
$img = imagecreatefromjpeg($file);
imagepng($img);
// get contents from the buffer
$contents = ob_get_clean();
$zip = new ZipArchive();
$zip->open('archive.zip', ZipArchive::CREATE);
// and put them in the zip file...
$zip->addFromString('name_in_the_zip.png', $contents);