I'm using Imagick and trying to convert a pdf to a png. It fails. My error_log says "Failed to read the file".
Example code:
$fileone = $_SERVER['DOCUMENT_ROOT'] . '/' . 'test.pdf';
$image = new Imagick($fileone);
$image->readImage($fileone);
$image->thumbnailImage(300, 0);
echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
Thoughts?
You need to install ghostscript
sudo apt-get install ghostscript
I would first use realpath() to check your file path and then see if the file is readable.
$fileone = realpath('test.pdf');
if (!is_readable($fileone)) {
echo 'file not readable';
}
Then if it is a multiple page pdf try this
$image = new Imagick($fileone.'[0]');
Related
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 display a png image in PHP (Laravel)
So far I've tried
$response = Response::make(readfile(public_path() .
"/img/$image_name.png", 200))->header('Content-Type', 'image/png');
return $response;
and this
$file = public_path() . "/img/$image_name.png";
$im = imagecreatefrompng($file);
header('Content-Type: image/png');
imagePNG($im);
imagedestroy($im);
I always get something like this instead of an actual image
�PNG IHDR:�c PLTE����>tRNS�* �< pHYs���+uIDAT(�eұ� AJs *́[P��#8Ҍ��Y�:����s�A�"D�!B�"D�!B�"D�!C~����}��Q��N�+'��bP�.a&^O)%5Y\�L����.ޜ9��IEND�B`�
When I use jpeg header and jpeg image it starts showing the actual image in browser, but for some reason it doesn't work for png images. Have anyone faced a similar problem?
Try it! Keep it simple :)
$img = file_get_contents(public_path('yourimage.jpg'));
return response($img)->header('Content-type','image/png');
You should find the mime dynamicaly using laravel default file facade File::mimeType($path) because as you mentioned that it is working fine with another image type like jpeg. I think this may happen because sometime we change file extension for testing or may be file have some issue with its header etc. You can use the following code :
Method 1 use Response::stream:
$path = public_path() . "/img/$image_name.png";
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::stream(function() use($file) {
echo $file;
}, 200, ["Content-Type"=> $type]);
Method 2 use Response::make:
$path = public_path() . "/img/$image_name.png";
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
Hope it will help.
perhaps you forgot install php-gd extension on your server
sudo apt-get install php-gd
or
sudo yum install php-gd
I suggest you use this plugin: http://image.intervention.io/
I want to show an IMG in my Images folder,
I have the correct path, but I get a question when I show the image.
Code:
$filename = 'Images/img1.png';
if (file_exists($filename)) {
echo '<img src="' . $filename . '"/>';
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
Did you try to check file permissions? This is the most recent problem, when you see that the file exists but it cannot be displayed because of permissions.
If you are using IIS try to assign read permissions for IIS_IUSRS on the file or images folder.
If you are using Linux, just change file permissions for the file (chmod command). As a starting point try to assign 777 permissions to the file just to check if it changed something.
I have tried the same code you have give above and its working perfect. So, try after giving permission to your images folder.
And, you can also check the exact issue by executing the image path url prefixed with your local directory path in the browser.
Use absolute path...
$filename = '/absolute/path/to/your/folder/www/Images/img1.png';
/*
* OR better...
* $filename = dirname(__FILE__).DIRECTORY_SEPARATOR."Images".DIRECTORY_SEPARATOR."img1.png";
* dirname(__FILE__) is the directory of your current script... If you have level before use:
* dirname(dirname(__FILE__))
*/
if (file_exists($filename)) {
echo '<img src="' . $filename . '"/>';
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
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");
-----------------------------------------------^
I am trying to change a file exenstion, but whenever I do the file seems to corrupt.
$oldFileName = $targetDir . DIRECTORY_SEPARATOR . $fileName;
$newString = preg_replace('"\.tmp$"', '.jpg', $oldFileName);
rename($oldFileName, $newString);
The code works and changes the extension, but yet the file when downloaded, comes up as being corrupt.
The exension is .tmp and I am trying to change it to .jpg.
If I download the .tmp and manually change it to a .jpg it works, but not when the PHP does it.
Anyone know why this may be happening?
Thanks!
try this
<?php
$file = 'example.txt';
$newfile = 'example.txt.bak'; //new file with extension
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>