0

Hey guys) Need your help. I use ext yii2-imagine. I try to save file in quality => 50, but it's not working. Image is always saved in quality => 100. Why can it happen ?

   $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]);
    }

2 Answers2

0

You should use jpeg_quality option:

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

See examples in documentation.

rob006
  • 21,383
  • 5
  • 53
  • 74
0

According to documentation of imagine, you can set 'jpeg_quality' setting for jpeg and 'png_compression_level' for png. In your case you can try something like that:

    $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);
Andrii Filenko
  • 954
  • 7
  • 17