PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 19, 2022

[FIXED] What is difference between setImageCompressionQuality vs setCompressionQuality - Imagick

 September 19, 2022     image, image-processing, imagemagick, imagick, php     No comments   

Issue

I found two methods in Imagick for set image compression quality

A ) setImageCompressionQuality

B ) setCompressionQuality

so I want to know which one is best and why in below condition

I read that setCompressionQuality method only works for new images (?)

I am trying to compress a file jpeg/png

$im = new Imagick();

$im->readImage($file); // path/to/file
$im->setImageCompressionQuality($quality); // 90,80,70 e.g.

$im->writeImage($file);

Solution

The method setImageCompressionQuality sets compression quality for your current image. This method is a wrapper for MagickWand's MagickSetImageCompressionQuality function. Source code is:

WandExport MagickBooleanType MagickSetImageCompressionQuality(MagickWand *wand,
  const size_t quality)
{
  assert(wand != (MagickWand *) NULL);
  assert(wand->signature == MagickWandSignature);
  if (wand->debug != MagickFalse)
    (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name);
  if (wand->images == (Image *) NULL)
    ThrowWandException(WandError,"ContainsNoImages",wand->name);

  //This line sets the quality for the instance 'images'  
  wand->images->quality=quality;
  return(MagickTrue);
}

The method setCompressionQuality sets compression quality for the whole object. This method is a wrapper for MagickWand's MagickSetCompressionQuality function. Source code is:

WandExport MagickBooleanType MagickSetCompressionQuality(MagickWand *wand,
  const size_t quality)
{
  assert(wand != (MagickWand *) NULL);
  assert(wand->signature == MagickWandSignature);
  if (wand->debug != MagickFalse)
    (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",wand->name);

  //This line sets quality for the image_info struct instance.
  wand->image_info->quality=quality;
  return(MagickTrue);
}

The MagickWand struct holds instances of Image and ImageInfo structs, source:

struct _MagickWand
{
  ...

  Image
    *images;          /* The images in this wand - also the current image */

  ImageInfo
    *image_info;      /* Global settings used for images in Wand */
  ...
};

Both Image and ImageInfo structs hold a size_t quality; data member. So for your example setImageCompressionQuality is perfectly fine.



Answered By - zindarod
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing