Hello community good afternoon someone around here has used the imagemagick library with php to compress images since I am using a function but I do not veil the changes when I check the weight of the images, that code is what is in an example of the documentation.
namespace Compacted_Imagick;
class Compacted_Imagick {
/*
* Configuracion inicial
*/
public $setting = [
'degrees' => 360,
'length' => 16,
'startlength' => 97,
'lastlength' => 122,
'patronspaces' => '/\W/'
];
public function compresed($file,$output,$quality) {
$img = new \Imagick($file);
$imgmagick = new \Imagick();
$imgmagick->setcompressionquality($quality);
$imgmagick->newpseudoimage(
$img->getimagewidth(),
$img->getimageheight(), 'canvas:white');
$imgmagick->setformat('jpg');
return $output = $img->getimageblob();
}
}
Here is an example of how I use this function
public function processImage() {
require_once (ROOT.DS.'vendor'.DS.'imagickcompacted'.DS.'Compacted_Imagick.php');
if($this->request->is('post')){
$imgtmp = $this->request->data['name_imagen'];
if($imgtmp['name'] != NULL){
//esta es mi clase que contiene la funcion
$img = new \Compacted_Imagick\Compacted_Imagick();
$dir = new Folder();
$dir->create(ROOT.DS.'tmpupload'.DS.$this->Auth->user('person_id'),TRUE, 0755);
$mv = new File($imgtmp['tmp_name']);
$mv->copy(ROOT.DS.'tmpupload'.DS.$this->Auth->user('person_id').DS.$imgtmp['name']);
$path = ROOT.DS.'tmpupload'.DS.$this->Auth->user('person_id').DS.$imgtmp['name'];
//Aqui obtengo la extensión para crear el nuevo archivo
$ext = pathinfo($imgtmp['name'],PATHINFO_EXTENSION);
//aqui el nuevo nombre de la imagen
$name_image_2 = $img->uniquename();
//luego la ruta donde se copiara la imagen
$path_copy = ROOT.DS.'tmpupload'.DS.$this->Auth->user('person_id').DS.$name_image_2.'.'.$ext;
//por ultimo mi función que procesa la imagen
$img->compresed($path, $path_copy, 25);
echo json_encode($imgtmp['name']);
die();
}
}
$this->autoRender = false;
}
The problem is that I do not see that the file is created try to write the same image but check the weight in megabytes and weighs the same as when the image was uploaded