html - Creating images from .tif file using Imagick in PHP
Get the solution ↓↓↓I'm unable to use Imagick in PHP to create images. I have created a log file to debug the function in use, but I don't get much from it. I have check the $sourcePath, $destPath, $fileExtension and all are fine.
convert -version:
Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Copyright: В© 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib
// Function used to create Thumnail
function CreateThumnail($sourcePath, $destPath, $fileExtension)
{
$maxCoreNum = 4; // set maximum number of cores
$tempPath = "";
if (strtoupper($fileExtension) == "TIF" || strtoupper($fileExtension) == "TIFF")
{
$tempPath = str_replace("thumb.jpg","temp.tif",$destPath);
//flatten image
$command = 'gdal_translate -co "TILED=YES" -co "COMPRESS=LZW" -co "BIGTIFF=YES" -ot Byte -scale "'.$sourcePath.'" "'.$tempPath.'" 2>&1';
exec($command, $output);
$image = new Imagick($tempPath);
$image->setImageColorspace(255);
$image->setCompression(Imagick::COMPRESSION_JPEG);
$image->setCompressionQuality(60);
$image->setResourceLimit (6, $maxCoreNum); //Set maximum number of cores to use with ImageMagick
$image->setImageBackgroundColor('white');
$image = $image->flattenImages(); // Use this instead.
$image->setImageFormat('jpeg');
$image->resizeImage(300, 0, imagick::FILTER_UNDEFINED, 1);
$image->writeImage($destPath);
$command = 'rm "'.$tempPath.'"';
exec($command);
}
}
Answer
Solution:
Seven years ago, I pointed out a bug to the PHP.net team, showing thatwriteImage()
is broken, and in fact, you need to use andwriteImageFile()
. I suspect this problem must exist also forreadImage()
vs.readImageFile()
.
Swap your code,$image->writeImage($destPath);
, and try this in its place...
$image = new Imagick();
$image_filehandle = fopen($sourcePath, 'a+');
$image->readImageFile($image_filehandle);
$image_filehandle = fopen($destPath, 'w+');
$image->writeImageFile($image_filehandle);
fclose($image_filehandle);
This should tell you iffopen()
fails, in case it's not a problem with Imagick at all, and one with permissions and user/file rights.
I have a working project where I use imagick functions, take a look at the source code for icon-resizing and you'll see that I follow pretty much the procedures I describe above: https://github.com/HoldOffHunger/GreenGluonCMS/blob/master/traits/scripts/SimpleImages.php
I've always been a fan of Imagick and have always felt it to be underappreciated and underused. Let me know if this answer helps!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: npm err! code 1
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.