Click here to Skip to main content
15,885,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a pdf file that I want to convert its first page to jpg. Here is the code:
PHP
<?php
$file = $_SERVER['DOCUMENT_ROOT'].'/pdf/7a41272307d844006ce1d07db8ee951e_PRO 1.pdf';

$im = new \Imagick($file);
$im->setIteratorIndex(0); // just use first page
$im->setImageAlphaChannel(\Imagick::VIRTUALPIXELMETHOD_WHITE); // Other alpha options are available, see Imagick PHP documentation
$im->setImageColorspace(\Imagick::COLORSPACE_SRGB); // Other colorspaces are available, see Imagick PHP documentation
$im->setImageBackgroundColor('white'); // Set transparent background elements to this color
$im->setFormat('JPG'); // Format, a wide variety is supported
$im->scaleImage(350, 350, true); // WxH + force aspect (true/false)

// To just output the raw image data to browser:
echo $im;

$im->destroy();
?>

But I am getting:
����JFIFHH�� ICC_PROFILE mntrRGB XYZ acspAPPL���- desc�|cprtx(wtpt�bkpt�rXYZ�gXYZ�bXYZ�rTRC gTRC bTRC desc"Artifex Software sRGB ICC Profile"Artifex Software sRGB ICC ProfiletextCopyright Artifex Software 2011XYZ �Q�XYZ XYZ o�8��XYZ b����XYZ $����curv #(-27;@EJOTY^chmrw|������������������������� %+28>ELRY`gnu|���������������� &/8AKT]gqz������������ !-8COZfr~���������� -;HUcq~��������� +:IXgw��������'7HYj{�������+=Oat������� 2FZn�������  % : O d y � � � � � �  ' = T j � � � � � � " 9 Q i � � � � � �  * C \ u � � � � � & @ Z t � � � � �.Id���� %A^z���� &Ca~����1Om����&Ed����#Cc����'Ij����4Vx���&Il����Ae

What I have tried:

How can I fix this issue? Why I am getting these weird icons?
Posted
Updated 4-Jun-23 22:07pm
Comments
Richard MacCutchan 3-Jun-23 4:04am    
You are trying to display an image (i.e. binary) file as text. What do you expect?
FRS4002 3-Jun-23 4:08am    
No I have a PDF file, and I want to convert the first page only to a JPG thumbnail... I also tried using
<img src="<?=$im;?>">
but I am getting the same result...
Richard MacCutchan 3-Jun-23 4:36am    
Of course you are getting the same result; read my previous comment again.
FRS4002 3-Jun-23 4:40am    
Sorry, but I don't understand what should I do...
Richard MacCutchan 3-Jun-23 4:44am    
Check that imagemagick code you are using can successfully do what you want. Have you checked that all the commands are correct according to the documentation? As far as I can see the data you are displaying has not been converted.

1 solution

You're trying to display the raw bytes of an image inline within an HTML file. That won't work.

You'll either need a separate endpoint to generate the thumbnail, and reference that as your image source:
HTML
<img src="getPdfThumbnail.php">
Or convert the data to Base64, and use a data: URI:
PHP
echo '<img src="data:image/jpeg;base64,' . base64_encode($im) . '">';
Data URLs - HTTP | MDN[^]
 
Share this answer
 
v2
Comments
FRS4002 5-Jun-23 4:25am    
Both methods didn't work... They both displayed an empty image... But the second method gave me this long thing:

<img src="image/jpeg;base64,/9j/4AAQSkZJRgABAQAASABIAAD/4gogSUNDX1BST0ZJTEUAAQEAAAoQAAAAA">


The code of course is larger than that... How to fix this issue?
Richard Deeming 5-Jun-23 4:28am    
The "big long thing" is a Base64-encoded version of the image bytes. There's no way to "fix" that short of using a separate PHP file to generate the thumbnail, as per my first suggestion.

NB: I missed off the data: prefix for the data URI; try adding that in to see if you get an image.
FRS4002 5-Jun-23 4:31am    
It worked! Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900