src/Service/ImageHelper/ImageResizer.php line 30

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: anthony
  5.  * Date: 04/12/18
  6.  * Time: 11:56
  7.  */
  8. namespace App\Service\ImageHelper;
  9. use Symfony\Component\Filesystem\Filesystem;
  10. class ImageResizer implements ImageResizerInterface
  11. {
  12.     /**
  13.      * Resize an image
  14.      *
  15.      * @param string $image (The full image path with filename and extension)
  16.      * @param string $newPath (The new path to where the image needs to be stored)
  17.      * @param int $height (The new height to resize the image to)
  18.      * @param int $width (The new width to resize the image to)
  19.      * @return string (The new path to the reized image)
  20.      */
  21.     public function resizeImage($image$newPath$height 0$width 100)
  22.     {
  23.         // Get current dimensions
  24.         $ImageDetails $this->getImageDetails($image);
  25.         $name $ImageDetails->name;
  26.         $height_orig $ImageDetails->height;
  27.         $width_orig $ImageDetails->width;
  28.         $fileExtention $ImageDetails->extension;
  29.         $ratio $ImageDetails->ratio;
  30.         $jpegQuality 100;
  31.         if($width $width_orig){
  32.             $height $height_orig * ($width $width_orig);
  33.         } else if ($height $height_orig){
  34.             $width $width_orig * ($height $height_orig);
  35.         } else {
  36.             if($height 0){
  37.                 $width $height $ratio;
  38.             } else if($width 0){
  39.                 $height $width $ratio;
  40.             }
  41.         }
  42.         $width round($width);
  43.         $height round($height);
  44.         $gd_image_dest imagecreatetruecolor($width$height);
  45.         $gd_image_src null;
  46.         switch( strtolower($fileExtention) ){
  47.             case 'png' :
  48.                 $gd_image_src imagecreatefrompng($image);
  49.                 imagealphablending$gd_image_destfalse );
  50.                 imagesavealpha$gd_image_desttrue );
  51.                 break;
  52.             case 'jpeg': case 'jpg'$gd_image_src imagecreatefromjpeg($image);
  53.                 break;
  54.             case 'gif' $gd_image_src imagecreatefromgif($image);
  55.                 break;
  56.             default: break;
  57.         }
  58.         imagecopyresampled($gd_image_dest$gd_image_src0000$width$height$width_orig$height_orig);
  59.         $filesystem = new Filesystem();
  60.         $filesystem->mkdir($newPath0777);
  61.         $newFileName $newPath $name "." $fileExtention;
  62.         switch( $fileExtention ){
  63.             case 'png' imagepng($gd_image_dest$newFileName); break;
  64.             case 'jpeg' : case 'jpg' imagejpeg($gd_image_dest$newFileName$jpegQuality); break;
  65.             case 'gif' imagegif($gd_image_dest$newFileName); break;
  66.             default: break;
  67.         }
  68.         return $newPath;
  69.     }
  70.         /**
  71.          *
  72.          * Gets image details such as the extension, sizes and filename and returns them as a standard object.
  73.          *
  74.          * @param $imageWithPath
  75.          * @return \stdClass
  76.          */
  77.         private function getImageDetails($imageWithPath){
  78.         $size getimagesize($imageWithPath);
  79.         $imgParts explode("/",$imageWithPath);
  80.         $lastPart $imgParts[count($imgParts)-1];
  81.         if(stristr("?",$lastPart)){
  82.             $lastPart substr($lastPart,0,stripos("?",$lastPart));
  83.         }
  84.         if(stristr("#",$lastPart)){
  85.             $lastPart substr($lastPart,0,stripos("#",$lastPart));
  86.         }
  87.         $dotPos     stripos($lastPart,".");
  88.         $name         substr($lastPart,0,$dotPos);
  89.         $extension     substr($lastPart,$dotPos+1);
  90.         $Details = new \stdClass();
  91.         $Details->height    $size[1];
  92.         $Details->width        $size[0];
  93.         $Details->ratio        $size[0] / $size[1];
  94.         $Details->extension $extension;
  95.         $Details->name         $name;
  96.         return $Details;
  97.     }
  98. }