Example #1
0
<?php

include './vendor/autoload.php';
$advPng = new \PHPImageOptim\Tools\Png\AdvPng();
$advPng->setBinaryPath('/usr/local/bin/advpng');
$optiPng = new \PHPImageOptim\Tools\Png\OptiPng();
$optiPng->setBinaryPath('/usr/local/bin/optipng');
$pngOut = new \PHPImageOptim\Tools\Png\PngOut();
$pngOut->setBinaryPath('/usr/local/bin/pngout');
$pngCrush = new \PHPImageOptim\Tools\Png\PngCrush();
$pngCrush->setBinaryPath('/usr/local/bin/pngcrush');
$pngQuant = new \PHPImageOptim\Tools\Png\PngQuant();
$pngQuant->setBinaryPath('/usr/local/bin/pngquant');
$optim = new \PHPImageOptim\PHPImageOptim();
$optim->setImage('./tests/image/lenna-original.png');
$optim->chainCommand($pngQuant)->chainCommand($advPng)->chainCommand($optiPng)->chainCommand($pngCrush)->chainCommand($pngOut);
$optim->optimise();
 public function multipleUpload($fs, $k, $fsTmpName)
 {
     $this->addInsert('mediaName', $fs['file']['name'][$k]);
     $this->setMediaName($fs['file']['name'][$k]);
     $this->addInsert('mediaType', $fs['file']['type'][$k]);
     $this->setMediaType($fs['file']['type'][$k]);
     $this->addInsert('mediaSizes', $fs['file']['size'][$k]);
     if (false === $this->moveUploadFile($fsTmpName, $this->buildTargetFile($this->buildTargetName($fs['file']['name'][$k])))) {
         throw new ErrorLogicModelException('Error upload file');
     } else {
         if (in_array($this->ext, $this->imageExtensions)) {
             $imageFile = Clean::get($this->getTargetPathFileName());
             $imgSize = new Size($imageFile);
             $imgSize->imgSize();
             if ($this->resizeImage > 0) {
                 $resize = new \ContentinumComponents\Images\CalculateResize($this->getResizeImage(), $imgSize->getWidth(), $imgSize->getHeight());
                 $nSize = $resize->getNewSize();
                 $nWidth = $nSize['width'];
                 $nHeight = $nSize['height'];
             } else {
                 $nWidth = $imgSize->getWidth();
                 $nHeight = $imgSize->getHeight();
             }
             $newImage = @imagecreatetruecolor($nWidth, $nHeight);
             switch ($this->ext) {
                 case 'JPG':
                 case 'JPEG':
                 case 'jpeg':
                 case 'jpg':
                     $jpegOptim = new \PHPImageOptim\Tools\Jpeg\JpegOptim();
                     $jpegOptim->setBinaryPath('/usr/bin/jpegoptim');
                     $jpegOptim->setOptimisationLevel(3);
                     imagecopyresampled($newImage, imagecreatefromjpeg($imageFile), 0, 0, 0, 0, $nWidth, $nHeight, $imgSize->getWidth(), $imgSize->getHeight());
                     imagejpeg($newImage, $imageFile, $this->getImageQuality());
                     $optim = new \PHPImageOptim\PHPImageOptim();
                     $optim->setImage($imageFile);
                     $optim->chainCommand($jpegOptim);
                     $optim->optimise();
                     break;
                 case "PNG":
                 case "png":
                     if ($this->resizeImage > 0) {
                         imageAlphaBlending($newImage, false);
                         imageSaveAlpha($newImage, true);
                         imagecopyresampled($newImage, imageCreateFromPng($imageFile), 0, 0, 0, 0, $nWidth, $nHeight, $imgSize->getWidth(), $imgSize->getHeight());
                         imagepng($newImage, $imageFile);
                     }
                     $pngQuant = new \PHPImageOptim\Tools\Png\PngQuant();
                     $pngQuant->setBinaryPath('/usr/bin/pngquant');
                     $optim = new \PHPImageOptim\PHPImageOptim();
                     $optim->setImage($imageFile);
                     $optim->chainCommand($pngQuant);
                     $optim->optimise();
                     break;
                 case "GIF":
                 case "gif":
                     if ($this->resizeImage > 0) {
                         imageAlphaBlending($newImage, false);
                         imageSaveAlpha($newImage, true);
                         imagecopyresampled($newImage, imagecreatefromgif($imageFile), 0, 0, 0, 0, $nWidth, $nHeight, $imgSize->getWidth(), $imgSize->getHeight());
                         imagegif($newImage, $imageFile);
                     }
                     $gifsicle = new \PHPImageOptim\Tools\Gif\Gifsicle();
                     $gifsicle->setBinaryPath('/usr/bin/gifsicle');
                     $optim = new \PHPImageOptim\PHPImageOptim();
                     $optim->setImage($imageFile);
                     $optim->chainCommand($gifsicle);
                     $optim->optimise();
                     break;
                 default:
                     break;
             }
             if ($this->resizeImage > 0) {
                 $this->addInsert('mediaDimensions', $nWidth . 'x' . $nHeight, true);
             }
             $this->addInsert('mediaSizes', filesize($imageFile), true);
         }
         if (in_array($this->ext, $this->imageExtensions)) {
             return 1;
         }
     }
 }