【问题标题】:Downscaling with php imagick使用 php imagick 进行缩减
【发布时间】:2017-05-04 07:58:09
【问题描述】:

我在使用 imagick 限制 pdf 到 jpg 转换器的输出时遇到问题,但我似乎无法找到仅调整大于 16 兆像素的图像大小的正确解决方案。

https://stackoverflow.com/a/6387086/3405380 有我正在寻找的解决方案,但我找不到 convert -resize '180000@>' screen.jpg 的 php 等效项(在我的情况下是 '16000000@>')。我试过$imagick->resizeImage(Imagick::RESOURCETYPE_AREA, 4000 * 4000);,但这只是切断了转换而不是调整大小。

public static function storeFilePdfToJpg($file) {
    $destinationPath = public_path() . Config::get('my.image_upload_path');
    $filename = str_random(10) . '_' . str_replace('.pdf', '', $file->getClientOriginalName()) . '.jpg';

    $imagick = new Imagick();
    $imagick->setResolution(350,350);
    $imagick->readImage($file->getPathName());
    $imagick->setImageCompression(imagick::COMPRESSION_JPEG);
    $imagick->setImageCompressionQuality(100);
    $imagick->resizeImage(Imagick::RESOURCETYPE_AREA, 4000 * 4000);
    $imagick->writeImages($destinationPath . '/' . $filename, false);
    return Config::get('my.full_image_upload_path') . $filename;
}

【问题讨论】:

    标签: php image-resizing imagick


    【解决方案1】:

    Image Magick 的 C api 没有 (afaik) 公开此功能,因此 PHP Imagick 扩展无法实现此功能。

    这在 PHP 中很容易实现:

    function openImageWithArea(int $desiredArea, string $filename) : Imagick
    {
        $image = new Imagick();
        $dataRead = $image->pingImage($filename);
        if (!$dataRead) {
            throw new \Exception("Failed to ping image of filename $filename");
        }
        $width = $image->getImageWidth();
        $height = $image->getImageHeight();
    
        $area = $width * $height;
        $ratio = $desiredArea / $area;
    
        $desiredWidth = (int)($ratio * $width);
        $desiredHeight = (int)($ratio * $height);
    
        $imagick = new Imagick();
        $imagick->setSize($desiredWidth, $desiredHeight);
        $imagick->readImage($file);
    
        return $imagick;
    }
    

    应该可以。

    【讨论】:

      猜你喜欢
      • 2011-11-19
      • 2010-12-29
      • 2012-01-17
      • 1970-01-01
      • 2016-09-14
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      相关资源
      最近更新 更多