PHP调整大小图像 [英] php resize image

查看:62
本文介绍了PHP调整大小图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

我试图替换其中的代码

@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;
    }
}

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

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

谢谢.

推荐答案

您在$ actualHeight与$ actualHeigth的拼写方面一直不一致

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

如果您到处都没有那么多@,那么php会告诉您这一点.

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

这篇关于PHP调整大小图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆