2017-04-02 79 views
0

我正在尝试调整大小并替换用户上传的图像,但我可以做的最多的是调整大小并将其作为另一个文件输出。我已经使用图书馆图像魔术师来调整大小。调整大小并使用PHP替换图像

如果有人能解释我如何做到这一点,而不使用库它会更好。

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); 
} 
+0

我建议你使用'gd'或'gd2'库。它非常完整且易于使用。 –

+0

你可以给我一个例子,如果它的大小超过一定的大小,请确定图片的大小@AmirZojaji – thenoob

+1

我在一个新的答案中找出它。 –

回答

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); 
+0

变量$ doc_row的用途是什么?当我尝试实现这一点时,我收到一条消息,表明它是未定义的 – thenoob

+0

对不起。这是错误的。纠正。 –

+0

当我将此代码添加到函数添加中时,似乎没有任何事情发生。图像大小保持不变 – thenoob