聘我网

新概念招聘3.0

用PHP进行图片resize操作

vote up0vote downstar

刚好手头没有PS,于是先用PHP解决燃眉急:

$filename = '3.jpg';
list($width_orig, $height_orig) = getimagesize($filename);
$width = 90;
$height = 110;
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
header('Content-type: image/jpg');
imagejpeg($image_p);

这里主要是

int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)

处理前: alt text

处理后: alt text

封装一下:

function resizeTo($filename,$to)
{
    $size = getimagesize($filename);
    $width = 90;
    $height = 110;
    $image_p = imagecreatetruecolor($width, $height);
    switch($size[2])
    {
        case 1:
            $func = 'imagecreatefromgif';
            $func2 = 'imagegif';
            break;
        case 2:
            $func = 'imagecreatefromjpeg';
            $func2 = 'imagejpeg';
            break;
        case 3:
            $func = 'imagecreatefrompng';
            $func2 = 'imagepng';
            break;
    }
    $image = $func($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
    $func2($image_p,$to);
}
 

1 个答复

vote up0vote down

gd还可以圆角处理:

<?php
class ImageRoundCorner
{
  private $im;
  private $imageInfo;
  private $radius;
  private $rColor;
  private $bColor;
  private $gColor;

  private $imageTypes = array(
          1 => 'GIF',2 => 'JPG',3 => 'PNG',4 => 'SWF',
               5 => 'PSD',6 => 'BMP',7 => 'TIFF(intel byte order)',
              8 => 'TIFF(motorola byte order)',9 => 'JPC',10 => 'JP2',
               11 => 'JPX',12 => 'JB2',13 => 'SWC',14 => 'IFF',15 => 'WBMP',
             16 => 'XBM'
           );
  /**
   * 初始化
   * 
   * @param sring $image
   * @param int $radius
   * @param int $rColor
   * @param int $bColor
   * @param int $gColor
   * 
   */
  public function __construct($image, $radius, $rColor=255, $bColor=0, $gColor=0)
  {
    if(file_exists($image))
    {
      $imageInfo = getimagesize($image);
      $this->imageInfo = array
      (
        'file' => str_replace('\\', '/', realpath($image)),
        'width' => $imageInfo[0],
        'height' => $imageInfo[1],
        'type' => $this->imageTypes[$imageInfo[2]],
        'mime' => $imageInfo['mime']
      );
      //获取原图
      $this->im = $this->getLoadImage($image, $this->imageInfo['type']);
      $this->radius = $radius;
      $this->rColor = $rColor;
      $this->bColor = $bColor;
      $this->gColor = $bColor;
    }
    else
    {
      exit();
    }
  }

  /**
   * 加载图片
   * 
   * @param string $image
   * @param string $type
   * @return gd
   */
  private function getLoadImage($image, $type)
  {
    if (empty($type)) 
    {
      return false;
    }
    switch(strtolower($type)) 
    {
      case 'jpg':
        $img = @imagecreatefromjpeg($image);
        break;
      case 'gif':
        $img = @imagecreatefromgif($image);
        break;
      case 'png':
        $img = @imagecreatefrompng($image);
        break;
      default:
      return false;
    }
    return $img;
  }

  /**
   * 获取圆角图片
   */
  private function getCornerImage()
  {
    //画布
    $cornerImage = imagecreatetruecolor($this->radius, $this->radius);
    //背景
    $bgColor = imagecolorallocate($cornerImage, $this->rColor, $this->gColor, $this->bColor);
    //前景
    $fgColor = imagecolorallocate($cornerImage, 0, 0, 0);
    imagefill($cornerImage, 0,0 ,$bgColor);
    //四分之一圆
    imagefilledarc($cornerImage, $this->radius, $this->radius, $this->radius*2, $this->radius*2, 180, 270, $fgColor, IMG_ARC_PIE);
    imagecolortransparent($cornerImage, $fgColor);
    return $cornerImage;
  }

  /**
   * 圆角
   */
  public function rounded()
  {
    $cornerImage = $this->getCornerImage();
    $rounderImage = imagecreatetruecolor($this->imageInfo['width'], $this->imageInfo['height']);
    $fgColor = imagecolorallocate($rounderImage, $this->rColor, $this->gColor, $this->bColor);
    imagefill($rounderImage, 0, 0, $fgColor);
    imagecopymerge($rounderImage, $this->im, 0, 0,0,0,$this->imageInfo['width'], $this->imageInfo['height'], 100 );
    imagecopymerge($rounderImage, $cornerImage,0,0,0,0, $this->radius,$this->radius,100);
    //左底
    $lbRounderImage = imagerotate($cornerImage, 90, $fgColor);
    imagecopymerge($rounderImage, $lbRounderImage, 0, $this->imageInfo['height']-$this->radius, 0, 0,$this->radius,$this->radius, 100);
    //右底
    $rbCornerImage = imagerotate($cornerImage, 180, $fgColor);
    imagecopymerge($rounderImage, $rbCornerImage, $this->imageInfo['width']-$this->radius, $this->imageInfo['height']-$this->radius, 0, 0, $this->radius, $this->radius, 100);
    //左上
    $ltCornerImage = imagerotate($cornerImage, 270, $fgColor);
    imagecopymerge($rounderImage, $ltCornerImage, $this->imageInfo['width']-$this->radius, 0, 0, 0, $this->radius, $this->radius,100);
    imagecolortransparent($rounderImage, $fgColor);
    //新图片
    $ext = substr($this->imageInfo['file'], strrpos($this->imageInfo['file'], '.'));
    $rounderName = str_replace($ext, '', $this->imageInfo['file']).'_round.png';
    imagepng($rounderImage, $rounderName);
    imagedestroy($this->im);
    imagedestroy($cornerImage);
    imagedestroy($rounderImage);
  }
}

$inst = new ImageRoundCorner("test.png", 5, 255, 255);
$inst->rounded();
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.