帮助修复图片库

发布于 2024-09-27 14:16:51 字数 1908 浏览 3 评论 0原文

我的图像库有问题。我想我知道问题所在,但我对图像知之甚少,希望有人能告诉我到底出了什么问题。

我想做的是调整 .png 的大小并保留透明度。当我调整大小并保存 .png 图像时,它会失去透明度并变成黑色。

我认为问题出在 resize 函数中的 imagecreatetruecolor 函数。文档表明这会返回黑色图像。我不认为这就是我所追求的。

有人可以多管闲事并告诉我问题是否确实在于调整大小功能以及应该如何解决这个问题。

谢谢。

class ResizeImage {

    // Load Image
    function load($filename) {
        $image_info = getimagesize($filename);
        $this->image_type = $image_info[2];

        if( $this->image_type == IMAGETYPE_JPEG ) {
            $this->image = imagecreatefromjpeg($filename);
        } elseif( $this->image_type == IMAGETYPE_GIF ) {
            $this->image = imagecreatefromgif($filename);
        } elseif( $this->image_type == IMAGETYPE_PNG ) {
            $this->image = imagecreatefrompng($filename);
            imagealphablending($this->image, true);
            imagesavealpha($this->image, true);
        }
    }

        // Resize the image
        function resize($width,$height) {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }

        // Save the image
    function save($filename, $image_type='', $compression=100, $permissions=null) {
        if ($image_type != '') {
            $this->image_type = $image_type;
        }

        if( $this->image_type == IMAGETYPE_JPEG ) {
            imagejpeg($this->image,$filename,$compression);
        } elseif( $this->image_type == IMAGETYPE_GIF ) {
            imagegif($this->image,$filename);
        } elseif( $this->image_type == IMAGETYPE_PNG ) {
            imagepng($this->image,$filename);
        }
        if( $permissions != null) {
            chmod($filename,$permissions);
        }
    }

I have a problem with an image library. I think I know the problem but I know very little about images and was hoping that someone could tell me what precisely is going wrong.

What I am trying to do is resize a .png and preserve the transparency. When I resize and save a .png image it looses its transparency and turns black.

I believe that the problem is with the imagecreatetruecolor function in the resize function. The documentation suggests this returns a black image. I don't think this is what I am after.

Could someone have a nosey and tell me if the problem does in fact lie with the resize function and how this should be fixed.

Thanks.

class ResizeImage {

    // Load Image
    function load($filename) {
        $image_info = getimagesize($filename);
        $this->image_type = $image_info[2];

        if( $this->image_type == IMAGETYPE_JPEG ) {
            $this->image = imagecreatefromjpeg($filename);
        } elseif( $this->image_type == IMAGETYPE_GIF ) {
            $this->image = imagecreatefromgif($filename);
        } elseif( $this->image_type == IMAGETYPE_PNG ) {
            $this->image = imagecreatefrompng($filename);
            imagealphablending($this->image, true);
            imagesavealpha($this->image, true);
        }
    }

        // Resize the image
        function resize($width,$height) {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }

        // Save the image
    function save($filename, $image_type='', $compression=100, $permissions=null) {
        if ($image_type != '') {
            $this->image_type = $image_type;
        }

        if( $this->image_type == IMAGETYPE_JPEG ) {
            imagejpeg($this->image,$filename,$compression);
        } elseif( $this->image_type == IMAGETYPE_GIF ) {
            imagegif($this->image,$filename);
        } elseif( $this->image_type == IMAGETYPE_PNG ) {
            imagepng($this->image,$filename);
        }
        if( $permissions != null) {
            chmod($filename,$permissions);
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谷夏 2024-10-04 14:16:51

尝试使用 Priimage 类。查看示例

Try to use Primage class. Check example.

冰雪梦之恋 2024-10-04 14:16:51

尝试使用 imagesavealpha,例如:

function resize($width,$height) {
        $new_image = imagecreatetruecolor($width, $height);
        imagesavealpha($new_image, true);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }

try using imagesavealpha, for example:

function resize($width,$height) {
        $new_image = imagecreatetruecolor($width, $height);
        imagesavealpha($new_image, true);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文