/**
  * Put image to square
  *
  * @param  string   $path               image file
  * @param  int      $width              square width
  * @param  int      $height             square height
  * @param  int	    $align              reserved
  * @param  int 	    $valign             reserved
  * @param  string   $bgcolor            square background color in #rrggbb format
  * @param  string   $destformat         image destination format
  * @return string|false
  * @author Dmitry (dio) Levashov
  * @author Alexey Sukhotin
  **/
 protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null)
 {
     if (($s = @getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     /* Coordinates for image over square aligning */
     $y = ceil(abs($height - $s[1]) / 2);
     $x = ceil(abs($width - $s[0]) / 2);
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $img1 = new Imagick();
             $img1->newImage($width, $height, new ImagickPixel($bgcolor));
             $img1->setImageColorspace($img->getImageColorspace());
             $img1->setImageFormat($destformat != null ? $destformat : $img->getFormat());
             $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
             $result = $img1->writeImage($path);
             return $result ? $path : false;
             break;
         case 'gd':
             $img = self::gdImageCreate($path, $s['mime']);
             if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
                 self::gdImageBackground($tmp, $bgcolor);
                 if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
                     return false;
                 }
                 $result = self::gdImage($tmp, $path, $destformat, $s['mime']);
                 imagedestroy($img);
                 imagedestroy($tmp);
                 return $result ? $path : false;
             }
             break;
     }
     return false;
 }
 /**
  * Put image to square
  *
  * @param  string   $path               image file
  * @param  int      $width              square width
  * @param  int      $height             square height
  * @param  int	    $align              reserved
  * @param  int 	    $valign             reserved
  * @param  string   $bgcolor            square background color in #rrggbb format
  * @param  string   $destformat         image destination format
  * @return string|false
  * @author Dmitry (dio) Levashov
  * @author Alexey Sukhotin
  **/
 protected function imgSquareFit($path, $width, $height, $align = 'center', $valign = 'middle', $bgcolor = '#0000ff', $destformat = null)
 {
     if (($s = @getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     /* Coordinates for image over square aligning */
     $y = ceil(abs($height - $s[1]) / 2);
     $x = ceil(abs($width - $s[0]) / 2);
     switch ($this->imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $img1 = new Imagick();
             $img1->newImage($width, $height, new ImagickPixel($bgcolor));
             $img1->setImageColorspace($img->getImageColorspace());
             $img1->setImageFormat($destformat != null ? $destformat : $img->getFormat());
             $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
             $result = $img1->writeImage($path);
             return $result ? $path : false;
             break;
         case 'gd':
             if ($s['mime'] == 'image/jpeg') {
                 $img = imagecreatefromjpeg($path);
             } elseif ($s['mime'] == 'image/png') {
                 $img = imagecreatefrompng($path);
             } elseif ($s['mime'] == 'image/gif') {
                 $img = imagecreatefromgif($path);
             } elseif ($s['mime'] == 'image/xbm') {
                 $img = imagecreatefromxbm($path);
             }
             if ($img && false != ($tmp = imagecreatetruecolor($width, $height))) {
                 if ($bgcolor == 'transparent') {
                     list($r, $g, $b) = array(0, 0, 255);
                 } else {
                     list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
                 }
                 $bgcolor1 = imagecolorallocate($tmp, $r, $g, $b);
                 if ($bgcolor == 'transparent') {
                     $bgcolor1 = imagecolortransparent($tmp, $bgcolor1);
                 }
                 imagefill($tmp, 0, 0, $bgcolor1);
                 if (!imagecopy($tmp, $img, $x, $y, 0, 0, $s[0], $s[1])) {
                     return false;
                 }
                 if ($destformat == 'jpg' || $destformat == null && $s['mime'] == 'image/jpeg') {
                     $result = imagejpeg($tmp, $path, 100);
                 } else {
                     if ($destformat == 'gif' || $destformat == null && $s['mime'] == 'image/gif') {
                         $result = imagegif($tmp, $path, 7);
                     } else {
                         $result = imagepng($tmp, $path, 7);
                     }
                 }
                 imagedestroy($img);
                 imagedestroy($tmp);
                 return $result ? $path : false;
             }
             break;
     }
     return false;
 }
예제 #3
0
 /**
  * Resize image
  *
  * @param  string   $path       image file
  * @param  int      $width      new width
  * @param  int      $height     new height
  * @param  bool	    $crop       crop image
  * @param  bool	    $exactfit   fit into given dimensions exactly
  * @param  string   $imgLib     image library
  * @param  string   $bgcolor    image background color
  * @param  string   $destformat image destination format
  * @return string|false
  * @author Dmitry (dio) Levashov, Alexey Sukhotin
  **/
 protected function resizeImg($path, $width, $height, $crop = false, $exactfit = false, $imgLib = 'imagick', $bgcolor = '#0000ff', $destformat = null)
 {
     if (($s = @getimagesize($path)) == false) {
         return false;
     }
     $result = false;
     list($x, $y, $size_w, $size_h) = $this->getResizeCropDimensions($s[0], $s[1], $width, $height, $crop, $exactfit);
     switch ($imgLib) {
         case 'imagick':
             try {
                 $img = new imagick($path);
             } catch (Exception $e) {
                 return false;
             }
             $img->contrastImage(1);
             if ($crop == false) {
                 $img->resizeImage($size_w, $size_h, NULL, true);
                 if ($exactfit == true) {
                     $img1 = new Imagick();
                     $img1->newImage($width, $height, new ImagickPixel($bgcolor));
                     $img1->setImageFormat($destformat != null ? $destformat : $img->getFormat());
                     $img->resizeImage($size_w, $size_h, NULL, true);
                     $img1->compositeImage($img, imagick::COMPOSITE_OVER, $x, $y);
                     $result = $img1->writeImage($path);
                     return $result ? $path : false;
                 }
             } else {
                 $img->cropImage($width, $height, $x, $y);
             }
             $result = $img->writeImage($path);
             return $result ? $path : false;
             break;
         case 'gd':
             if ($s['mime'] == 'image/jpeg') {
                 $img = imagecreatefromjpeg($path);
             } elseif ($s['mime'] == 'image/png') {
                 $img = imagecreatefrompng($path);
             } elseif ($s['mime'] == 'image/gif') {
                 $img = imagecreatefromgif($path);
             } elseif ($s['mime'] == 'image/xbm') {
                 $img = imagecreatefromxbm($path);
             }
             $init_w = $size_w;
             $init_h = $size_h;
             if ($exactfit == true) {
                 $init_w = $width;
                 $init_h = $height;
             }
             if ($img && false != ($tmp = imagecreatetruecolor($init_w, $init_h))) {
                 if ($crop == false) {
                     if ($bgcolor == 'transparent') {
                         list($r, $g, $b) = array(0, 0, 255);
                     } else {
                         list($r, $g, $b) = sscanf($bgcolor, "#%02x%02x%02x");
                     }
                     $bgcolor1 = imagecolorallocate($tmp, $r, $g, $b);
                     if ($bgcolor == 'transparent') {
                         $bgcolor1 = imagecolortransparent($tmp, $bgcolor1);
                     }
                     imagefill($tmp, 0, 0, $bgcolor1);
                     if (!imagecopyresampled($tmp, $img, $x, $y, 0, 0, $size_w, $size_h, $s[0], $s[1])) {
                         return false;
                     }
                 } else {
                     if (!imagecopy($tmp, $img, 0, 0, $x, $y, $width, $height)) {
                         return false;
                     }
                 }
                 if ($destformat == 'jpg' || $destformat == null && $s['mime'] == 'image/jpeg') {
                     $result = imagejpeg($tmp, $path, 100);
                 } else {
                     if ($destformat == 'gif' || $destformat == null && $s['mime'] == 'image/gif') {
                         $result = imagegif($tmp, $path, 7);
                     } else {
                         $result = imagepng($tmp, $path, 7);
                     }
                 }
                 imagedestroy($img);
                 imagedestroy($tmp);
                 return $result ? $path : false;
             }
             break;
     }
     return false;
 }