php调整图片大小

hao*_*han 0 php resize image

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

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

我试图替换代码

@imagejpeg($thumb, null, 100);
Run Code Online (Sandbox Code Playgroud)

@imagejpeg($image, null, 100);
Run Code Online (Sandbox Code Playgroud)

会工作〜

-

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何专家都知道发生了什么并帮助我解决它?

谢谢.

Ste*_*tes 8

你的$ actualHeight与$ actualHeigth的拼写不一致

如果你没有那么多@在任何地方,那么php会告诉你这个.