2010-03-27 263 views
0

我有一个类来读取和输出图像内容,如果$宽度设置,它将调整图像大小,然后输出它。php调整大小图像

如果我调用这样的函数$ image-> readImage('123.jpg'); ,它可以正确输出图像文件,但是当我调用$ image-> readImage('123.jpg',300);调整它的大小,它只是显示一个黑色的图像,调整后的宽度为&高度。

我试图从

@imagejpeg($thumb, null, 100); 

更换代码

@imagejpeg($image, null, 100); 

意志的作品〜

-

protected function readImage($fileName, $width = 0) 
{ 
    if ($width <= 0) { 
     return @file_get_contents($this->destination . '/' . $fileName); 
    } else { 
     $imageSize = @getimagesize($this->destination . '/' . $fileName); 
     $actualWidth = $imageSize[0]; 
     $actualHeigth = $imageSize[1]; 

     if ($actualWidth <= $width) { 
      return @file_get_contents($this->destination . '/' . $fileName); 
     } 
     $height = (100/($actualWidth/$width)) * .01; 
     $height = @round($actualHeigth * $height); 

     $image = @imagecreatefromjpeg($this->destination . '/' . $fileName); 
     $thumb = @imagecreatetruecolor($width, $height); 
     @imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $actualWidth, $actualHeight); 

     ob_start(); 
     @imagejpeg($thumb, null, 100); 
     $bits = ob_get_contents(); 
     ob_end_clean(); 

     return $bits; 
    } 
} 

哪位高手知道发生了什么,并帮助我解决它?

谢谢。

+1

如果你从函数调用前删除了所有的'@'符号,你可能会得到一个体面的错误信息,可能会给你一个线索。使用'@'是一个坏习惯,除非你确实需要它。 – zombat 2010-03-27 00:08:33

+0

你的第一段有点令人困惑,因为你声称相同的确切代码会做两件不同的事情......你可以澄清你说你称呼'$ image-> readImage('123.jpg')的意思吗? ? – 2010-03-27 00:29:09

+0

对不起,我编辑过它。 – haohan 2010-03-27 00:35:51

回答

8

您已在$的ActualHeight的拼写VS $ actualHeigth

被不一致的,如果你没有那么多@无处不在,那么PHP会告诉你这一点。

+0

谢谢,我将在未来编码@。 – haohan 2010-03-27 00:55:35

+0

如果你不需要他们为什么他们在那里hahan? – 2010-03-27 03:11:09