Imagick 使用不同的目錄和數據庫 PDF 來縮略圖 JPG (Imagick using different directory and database PDF to thumbnail JPG)


問題描述

Imagick 使用不同的目錄和數據庫 PDF 來縮略圖 JPG (Imagick using different directory and database PDF to thumbnail JPG)

When I use a php script using imagick basics in the same folder where the image is and using the filename the output is perfect, it outputs the image of the PDF's 1st page:

// Read page 1 
$im = new imagick( 'Miami_Guide.pdf[0]' );
// Convert to png 
$im‑>setImageFormat( "png" );
// Send out
header( "Content‑Type: image/png" );
echo $im;

Here is the challenge: how do I move this script to a different directory AND using a fieldname called $uploaded from a database record instead? I tried this but it didn't work:

$path: '../../otherplace/';
// $uploaded is the field with the filename value
$fileloc: $path . $uploaded;
// Read page 1 
$im = new imagick( '$fileloc[0]' );
// Convert to png 
$im‑>setImageFormat( "png" );
// Send out 
header( "Content‑Type: image/png" );
echo $im;

I get a "Fatal error: Uncaught exception 'ImagickException' with message 'Unable to read the file: $fileloc' in /home/user/public_html/dir/folder/script.php:334 Stack trace: #0 /home/user/public_html/dir/folder/script.php(334): Imagick‑>__construct('$fileloc') #1 /home/user/public_html/dir/folder/script.php(226): show_record(Array) #2 {main} thrown in /home/user/public_html/dir/folder/script.php on line 334

Also tried pathinfo() and fatal error again

I made a few tweaks but now Imagick is returning gibberish instead of image:

$sourcePath = 'dir/folder/'; // Path of original image
$sourceUrl = 'http://www.site.com/';
$thumbPath = $sourcePath; // Writeable thumb path
$thumbUrl = $sourceUrl . $thumbPath . $uploaded ; 
$im = new Imagick($thumbUrl);
$im‑>setImageFormat( "jpg" );
$im‑>thumbnailImage(100, 100);
header("Content‑type: image/jpg");
echo "$im";

"No error" but outputs gibberish instead of image:

ÿÛCÿÀdd"ÿÄÿÄ9 !1"#A2QVh‘•¦Ôä$Waq±ÓÿÄÿÄ)!1Qq"A‘Òar’¡áÿÚ?Íkßµ+¾;é=oÕÜŽW#ü–ÙænÝ»ë‘çOo´OÒÝr2_^¬ÇiÓFãaR›5$ò~\’0#Ïn¢—·©ÕuµðGMXÍ«ùÛæÿÃ'g§lfÍ,Ø”Sú*¥Áw“h‡/qk,=Ýržzï–>~ÂÒút ƒÔ«á3žŽÃ‹¶s8yd“4–ݤ®§‚ÇqW¼ºÎ£‹Áß~.—´¤ŸÅKn@ «DE ±gÄ©"hm&㥰öÔ«b¬..........

Any help?

‑‑‑‑‑

參考解法

方法 1:

The Reason your first example doesn't work is because you have your variable inside single quotes, not double quotes.

This:

$im = new imagick( '$fileloc[0]' );

Tells imagemagick to look for a file named $fileloc, which of course does not exist.

do this instead:

$im = new imagick( "$fileloc[0]" );

As for your second example, I belive you are having problems because for some reason you've put $im inside quotes when you echo it. You should echo it the same way you were in your working test:

echo $im;

not

echo "$im";

方法 2:

Here is the solution:

instead of:

echo $img;

you should echo:

echo "<img src='data:image/jpg;base64,".base64_encode($img)."' />";

This solved my Imagick problem, I hope it will help others. 

One thing that I made sure was to test the basics: test.pdf > test.jpg, also tested: test.pdf > test.png, test.pdf > test.tiff ‑ once I was sure that all the extensions were in the delegates and working fine, I moved to different dir and pulling the file from someplace else.

Brief of all the steps:

1‑ installed imagick (php) ‑ I believe it was devel

2‑ set the path to the file thru http://

3‑ the rest was pretty much Imagick basics:

 $pdfUrl = "http://www.site.com/dir/$file";
 $img = new Imagick($pdfUrl);
 $img‑>setImageFormat("jpeg");
 echo "<img src='data:image/jpg;base64,".base64_encode($img)."' />";

(by cchapGordon Baileycchap)

參考文件

  1. Imagick using different directory and database PDF to thumbnail JPG (CC BY‑SA 3.0/4.0)

#imagick #pdf #Database #jpeg #file-location






相關問題

Imagick 安裝錯誤 - 類未定義 (Imagick Installation Errors - Class Undefined)

GD/Imagick 不保存我的圖像 (GD/Imagick doesn't save my image)

Imagick 使用不同的目錄和數據庫 PDF 來縮略圖 JPG (Imagick using different directory and database PDF to thumbnail JPG)

如何在不損失分辨率和質量的情況下減小圖像尺寸(高度、寬度) (How to decrease image size(height, width) without losing its resolution and quality)

Đang cố gắng để tưởng tượng chạy trên PHP 5.4.3 ở Windows x64 (Trying to get imagick running on PHP 5.4.3 at Windows x64)

WampServer 和 Imagemagick,無法識別 imagick php 模塊 (WampServer and Imagemagick, imagick php module not recogized)

PHP Imagick:如何將自定義圖像屬性保存到文件 (PHP Imagick: How to save custom image property to file)

PHP Imagick - 顯示多圖像 TIFF 的第一片 (PHP Imagick - display first slice of mutli image TIFF)

可以加快 php/imagick 中的圖像加載速度 (Possible to speed up image loading in php/imagick)

PHP imagick 相當於 -define png:color-type=6 (PHP imagick equivalent of -define png:color-type=6)

php Imagick::levelImage 使用 (php Imagick::levelImage usage)

imagick:無法查詢字體度量 (Imagick: Failed to query the font metrics)







留言討論