使用PHP调整大小并替换图像

时间:2017-04-02 15:33:30

标签: php image

我正在尝试调整大小并替换用户上传的图像,但我能做的最多就是调整大小并输出为另一个文件。我使用了库图像魔术师来调整大小。

如果有人能够解释我如何在不使用图书馆的情况下做到这一点,那就更好了。

public function add() {
     $f=Base::instance()->get('FILES');
     $fext=pathinfo ($f['usrimg']['name'],PATHINFO_EXTENSION);
     $this->copyFrom('POST');
     $this->save();
     $newName=str_pad($this->_id,5,"0").'.'.$fext;
     move_uploaded_file($f['usrimg']['tmp_name'], $newName);
     $this->load('id='.$this->_id);
     $this->set('photo',$newName);
     $this->update();  

     require_once('php_image_magician.php');
     $magicianObj = new imageLib($newName);
     $magicianObj->resizeImage(100, 200);
     $magicianObj->saveImage('q.jpg', 100);
}

1 个答案:

答案 0 :(得分:0)

如果您想使用gd库,例如您可以使用此代码调整jpeg图像的大小:

  $image=file_get_contents('image.jpg');
  $src=imagecreatefromstring($image);
  $x=imagesx($src);
  $y=imagesy($src);
  $y1=100; //new width
  $x1=intval($y1*$x/$y); //new height proprtional to new width
  $dst=imagecreatetruecolor($x1,$y1);
  imagecopyresized($dst,$src,0,0,0,0,$x1,$y1,$x,$y);
  header("Content-Type: image/jpeg");
  imagejpeg($dst);