Create Thumbnail of ONLINE PDF for first page only using Imagick - php

I tried to make a thumbnail of a pdf file which is hosted on another server. My current code is:
<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The problem is that code is only generating thumbnail for LAST PAGE of the pdf file. How can I make a thumbnail for first page only? I tried to add [0] at the imagick line.
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf[0]");
but it didn't work. It only work for local pdf file, i.e:
$im = new imagick("my-pdf-file.pdf[0]");
Please help me solve this problem.. Thanks..

You'll need to reset the active image to the first page. This can be done with Imagick::setIteratorIndex.
<?php
$im = new imagick("http://www.d3publisher.us/Walkthroughs/Naruto_NC_3_DS.pdf");
$im->setIteratorIndex(0);
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>

Try...
$im->setImageIndex(0); //this will return 1th page of the pdf file
$im->setImageFormat('jpg');

"This can be done with Imagick::setIteratorIndex. .."
..or not. Simply has no effect . Setting it to one crashes something, setting it to 0 gets the last page..
function make_thumbnail($filename)
{
try
{
$imagick= new Imagick($filename);
}
catch(ImagickException $e)
{
// failed to make a thimbynail. what now?
// load up our trusty truetype font png instead?
$imagick->destroy();
return "0"; // shove any rubbish in the db - it will just say no image available when asked.
}
$imagick->setIteratorIndex(0);// rewind to first page or image of a multi series
$imagick->setImageFormat("png"); // turn it into a png
$imagick = $imagick->flattenImages(); // remove any transparency
$imagick->scaleImage(300,0); //resize...to less than 300px wide
$d = $imagick->getImageGeometry();
$h = $d['height'];
if($h > 300)
$imagick->scaleImage(0,300);
$imagick->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$imagick->setImageCompressionQuality(0);
$imagick->setIteratorIndex(0);
$a = $imagick->getImageBlob(); // output as bytestream
$imagick->destroy();
return $a;
}

Related

PHP PDF to image crops some files when generating a thumbnail. setIteratorIndex fails as well

I have written a function in PHP 7.4 that generates a thumbnail of the first page of a PDF document. The function works as intended for most PDF files.
However, some PDFs get cropped when I generate the thumbnail. Additionally, sometimes, I don't get the first page.
This PDF triggers both problems:
http://www.teavigoinfo.com/pdf/study-27.pdf
function PDF2Thumbnail($url) {
$image = #file_get_contents($url);
if($image) {
$im = new Imagick();
$im->readImageBlob($image);
$im->setIteratorIndex(0);
$im->thumbnailImage(300, 0);
$im->setImageFormat('png');
$im = $im->flattenImages();
header('Content-Type: image/png');
echo $im;
}
}
Thank you.

using Image Manipulation Class in CodeIgniter 4 without save in a file

this is my code
$file = new \CodeIgniter\Files\File($imgUrl);
$fileMime = $file->getMimeType();
if(file_exists("img/us/".$_SESSION["sess_order"]."/".$_SESSION["sess_userimg"])){
if($fileMime == 'image/jpeg'){
$image = \Config\Services::image()
-> withFile($file)
-> fit(100, 100, 'center');
header("Content-type: image/JPEG");
echo $image;
}
}else{
return "img/usr.png";
}
I'll get user's Image and return thumbnail of that image.
with Image Manipulation Class in #CodeIgniter 4 can do it well, but I don't need to save thumbnail file in storage, I just need image as an Object. How can I do it.
I want something like using file_get_contents() function and return it
thanks

php imagick resize image code not working properly

I am using the following Imagick function to resize an image. The code runs and resizes the image, but with an error from the server saying "An error occurred while requesting the document from the testing server." And I cannot detect the problem. In the browser it, however, outputs non-human readable characters. In case I try to output the resized/modified image to the browser, there is no problem. I face this problem as I try to save the image to the disk.
Here is my code:
<?php
imagick_resize('running.jpg');
function imagick_resize($image, $width = 460, $height = 300)
{
// define widescreen dimensions
// $width = 460;
// $height = 300;
// load an image
$i = new Imagick($_SERVER['DOCUMENT_ROOT'] . '/alchemyapi/' . $image);
// get the current image dimensions
$geo = $i->getImageGeometry();
// crop the image
if(($geo['width']/$width) < ($geo['height']/$height))
{
$i->cropImage($geo['width'], floor($height*$geo['width']/$width), 0, (($geo['height']-($height*$geo['width']/$width))/2));
}
else
{
$i->cropImage(ceil($width*$geo['height']/$height), $geo['height'], (($geo['width']-($width*$geo['height']/$height))/2), 0);
}
// thumbnail the image
$i->ThumbnailImage($width,$height,true);
// save or show or whatever the image
# $i->setImageFormat('png');
# header("Content-Type: image/png");
# unlink('small_square_img.png');
$i->writeImage($_SERVER['DOCUMENT_ROOT'] . '/alchemyapi/tmp/small_square_img.png');
# file_put_contents('small_square_img.png', $i);
exit($i);
}
?>
The data you are sending to the browser is not a valid image. It is quite likely that you have a Byte-Order-Marker in some of your files before the
You should check for that and fix it if it is occuring, or provide an example of the "non-human readable characters" for people to have a clue of what the problem is.

PHP - Script using imagepng won't save file and then display it

I'm trying to load an image, edit it, save it and then display it from a script that is called within IMG tags. The script works if I want to just display the image and it does save the image. But it won't save it and then display it. Does anyone know what I'm doing wrong? Any help would be greatly appreciated.
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = #imagecreatefrompng($file_location);
// This section of code removed as doesn't affect result
imagepng($img_display, $file_location);
chmod($file_location, 0777);
imagepng($img_display);
imagedestroy($img_display);
}
?>
Try this and checks if your folder has permission to save the image. chmod 777 on it for sure.
<?php
header('Content-Type: image/png');
$file_location = "test.png";
if (file_exists($file_location)) {
$img_display = imagecreatefrompng($file_location); // Create PNG image
$filename = $file_location + time(); // Change the original name
imagepng($img_display, $filename); // Saves it with another name
imagepng($filename); // Sends it to the browser
imagedestroy($img_display); // Destroy the first image
imagedestroy($filename); // Destroy the second image
}
Try this:
ob_start();
imagepng($img_display);
$contents = ob_get_clean();
file_put_contents($file_location, $contents);
echo $contents;
imagedestroy($img_display);

PHP ImageMagick invert color

I'm working on a PHP app that converts a self portrait into a pencil drawing and part of the app requires a desaturated/inverted image similar to the example below:
before and after invert:
It's easy creating a desaturated image, but how would I achieve this invert using imagemagick? I tried using negate, but failed miserably.
Here's what I have so far:
<?php
$img = new Imagick();
$img->readImage($image);
$img->modulateImage(100, 0, 100);
$img->writeImage('image.png');
ob_clean();
header('Content-Type: image/png');
echo $img;
$img->destroy();
?>
According this page, you can call imagemagick filter by PHP functions :
function negateImage($imagePath, $grayOnly, $channel) {
$imagick = new Imagick(realpath($imagePath));
$imagick->negateImage($grayOnly, $channel);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
}

Categories