0

嘿伙计们)需要你的帮助。我使用 ext yii2-imagine。我尝试以质量 => 50 保存文件,但它不起作用。图像始终以质量 => 100 保存。为什么会发生这种情况?

   $imagine = new Image();
    $photo = $imagine::getImagine()->open($this->uploadedFile->tempName);

    $width = $photo->getSize()->getWidth();
    $height = $photo->getSize()->getHeight();

    if (($width >= $this->width) || ($height >= $this->height)) {
        $photo->thumbnail(new Box($this->width, $this->height))->save($this->uploadedFile->tempName, ['quality' => 50]);
    }
4

2 回答 2

0

您应该使用jpeg_quality选项:

$photo->thumbnail(new Box($this->width, $this->height))
     ->save($this->uploadedFile->tempName, ['jpeg_quality' => 50]);

请参阅文档中的示例。

于 2019-02-28T08:24:12.097 回答
0

根据想象的文档,您可以为 jpeg 设置“jpeg_quality”设置,为 png 设置“png_compression_level”。在你的情况下,你可以尝试这样的事情:

    $options = [];
    switch (exif_imagetype($this->uploadedFile->tempName)) {
        case IMAGETYPE_PNG:
            $options = ['png_compression_level' => 9];
            break;
        case IMAGETYPE_JPEG:
            $options = ['jpeg_quality' => 50];
            break;
        default:
            throw new \Exception('Unsupported format');
    }
    $photo->thumbnail(new Box($this->width, $this->height))->save($this->uploadedFile->tempName, $options);
于 2019-02-28T08:25:57.360 回答