开发者

php resize image

开发者 https://www.devze.com 2022-12-25 00:33 出处:网络
I have a class to read and output the image content, if $width is set, it will resize the image, and then output it.

I have a class to read and output the image content, if $width is set, it will resize the image, and then output it.

If I call the function like this $image->readImage('123.jpg'); , it can output the image file correctly, but when I call $image->readImage('123.jpg', 300); to resize it, it just display a black image with resized width & height.

And I tried to replace the code from

@imagejpeg($thumb, null, 100);

to

@imagejpeg($image, null, 100);

will works~

-

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 ($actualWi开发者_StackOverflowdth <= $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;
    }
}

Any experts know what happened and help me to solve it ?

Thanks.


you've been inconsistant in your spelling of $actualHeight vs $actualHeigth

if you didn't have so many @ everywhere, then php would have told you this.

0

精彩评论

暂无评论...
验证码 换一张
取 消