S
S
Sergey Goryachev2017-07-28 01:57:08
PHP
Sergey Goryachev, 2017-07-28 01:57:08

How to make a thumbnail image from a PDF?

I have a simple code, it loads a PDF, shifts it to the right place and renames it.
But here comes another problem.
It is necessary to make a preview from the PDF (first page) during the download.
The preview should be loaded in the same place, with the same name and .jpg extension.
ImageMagick works successfully on the server.

$limit_size = 50*1024*1024; // 1 Mb
    $valid_format = array("pdf");
    $error_array = array();
    $path_file = "../oc-content/uploads/releases/";
    $rand_name = 'postroyka' . date('Ymd',date('w')!=6? strtotime("last Saturday"): time());
    if($_FILES){
        if($_FILES["upload_file"]["size"] > $limit_size){
            $error_array[] = "Размер файла превышает допустимый!";
        }
        $format = end(explode(".", $_FILES["upload_file"]["name"]));
        if(!in_array($format, $valid_format)){
            $error_array[] = "Формат файла не допустимый!";
        }
        if(empty($error_array)){
            if(is_uploaded_file($_FILES["upload_file"]["tmp_name"])){
                move_uploaded_file($_FILES["upload_file"]["tmp_name"], $path_file . $rand_name . ".$format");
            }else{
                $error_array[] = "Ошибка загрузки!";
            }
        }
    };

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Goryachev, 2017-07-28
@webirus

I'll just put it here, I came up with the solution myself, it turned out to be easier than I thought.

$im = new imagick( $path_file . $rand_name . '.pdf[0]' );
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(60);
$im->setImageFormat('jpeg');
$im->resizeImage(290, 375, imagick::FILTER_LANCZOS, 1);
$im->writeImage($path_file . $rand_name . '.jpg');
$im->clear();
$im->destroy();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question