当前位置:  开发笔记 > 编程语言 > 正文

PHP的大小调整脚本不适用于透明的GIF

如何解决《PHP的大小调整脚本不适用于透明的GIF》经验,为你挑选了1个好方法。

我怎样才能使它适用于透明的gif和png?

function resizeImage($image,$newImage,$target_width,$target_height, $type="") {
    if (is_file($image)) {
        if($type == ".gif"){
            $image_org=@imagecreatefromgif($image);
        }else{
            $image_org=@imagecreatefromjpeg($image);
        }
        if ($image_org) {
            list($w,$h,$type,$attr) = getimagesize($image);
            $factor=C_Image_Custom::calcRescaleFactor($w,$h,$target_width,$target_height);

            if ($factor>1) {
                $image_w = $w / $factor;
                $image_h = $h / $factor;
            } else {
                $image_w = $w;
                $image_h = $h;
            }       

        //Note: PHP with GD2.0 required for imagecreatetruecolor
        $img_copy = imagecreatetruecolor($image_w, $image_h);
        imagecopyresampled($img_copy, $image_org, 0, 0, 0, 0, $image_w, $image_h, $w, $h);

            if (@imagejpeg($img_copy, $newImage, 80)) {
                chmod($newImage,0777);
            }   else {
                echo("Error: Unable to create image $newImage. Check directory permissions.");
            }   

          imagedestroy($image_org);
            imagedestroy($img_copy);
        }   
    }   

Andrew Swift.. 5

此功能非常适合我调整jpg和透明(或非透明)gif的大小:

function resizeImage($originalImage, $toWidth, $toHeight, $isJPG)
{
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale = $width / $toWidth;
    $yscale = $height / $toHeight;

    // Recalculate new size with default ratio
    if ($yscale > $xscale) {
        $new_width = round($width * (1 / $yscale));
        $new_height = round($height * (1 / $yscale));
    } else {
        $new_width = round($width * (1 / $xscale));
        $new_height = round($height * (1 / $xscale));
    }

    // Resize the original image
    if ($isJPG) {
        $imageResized = imagecreatetruecolor($new_width, $new_height);
        $imageTmp = imagecreatefromjpeg($originalImage);
        imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    } else {
        //$imageResized = imagecreatetruecolor($new_width, $new_height);
        //$imageTmp = imagecreatefromgif ($originalImage);
        //imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        # what follows is for resizing a gif, transparent or not
        # http://ru2.php.net/imagecopyresampled
        # load/create images
        $imageTmp = imagecreatefromgif($originalImage);
        $imageResized = imagecreatetruecolor($new_width, $new_height);
        imagealphablending($imageResized, false);

        # get and reallocate transparency-color
        $transindex = imagecolortransparent($imageTmp);
        if ($transindex >= 0) {
            $transcol = imagecolorsforindex($imageTmp, $transindex);
            $transindex = imagecolorallocatealpha(
                $imageResized,
                $transcol['red'],
                $transcol['green'],
                $transcol['blue'],
                127
            );
            imagefill($imageResized, 0, 0, $transindex);
        }

        # resample
        imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        # restore transparency
        if ($transindex >= 0) {
            imagecolortransparent($imageResized, $transindex);
            for ($y = 0; $y < $new_height; ++$y) {
                for ($x = 0; $x < $new_width; ++$x) {
                    if (((imagecolorat($imageResized, $x, $y) >> 24) & 0x7F) >= 100) {
                        imagesetpixel(
                            $imageResized,
                            $x,
                            $y,
                            $transindex
                        );
                    }
                }
            }

        }
        # save GIF
        imagetruecolortopalette($imageResized, true, 255);
        imagesavealpha($imageResized, false);
    }
    return $imageResized;
}

原始功能来自PhpToys 1.0,适用于透明.gifs的部分来自此PHP文档注释。



1> Andrew Swift..:

此功能非常适合我调整jpg和透明(或非透明)gif的大小:

function resizeImage($originalImage, $toWidth, $toHeight, $isJPG)
{
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale = $width / $toWidth;
    $yscale = $height / $toHeight;

    // Recalculate new size with default ratio
    if ($yscale > $xscale) {
        $new_width = round($width * (1 / $yscale));
        $new_height = round($height * (1 / $yscale));
    } else {
        $new_width = round($width * (1 / $xscale));
        $new_height = round($height * (1 / $xscale));
    }

    // Resize the original image
    if ($isJPG) {
        $imageResized = imagecreatetruecolor($new_width, $new_height);
        $imageTmp = imagecreatefromjpeg($originalImage);
        imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    } else {
        //$imageResized = imagecreatetruecolor($new_width, $new_height);
        //$imageTmp = imagecreatefromgif ($originalImage);
        //imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        # what follows is for resizing a gif, transparent or not
        # http://ru2.php.net/imagecopyresampled
        # load/create images
        $imageTmp = imagecreatefromgif($originalImage);
        $imageResized = imagecreatetruecolor($new_width, $new_height);
        imagealphablending($imageResized, false);

        # get and reallocate transparency-color
        $transindex = imagecolortransparent($imageTmp);
        if ($transindex >= 0) {
            $transcol = imagecolorsforindex($imageTmp, $transindex);
            $transindex = imagecolorallocatealpha(
                $imageResized,
                $transcol['red'],
                $transcol['green'],
                $transcol['blue'],
                127
            );
            imagefill($imageResized, 0, 0, $transindex);
        }

        # resample
        imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

        # restore transparency
        if ($transindex >= 0) {
            imagecolortransparent($imageResized, $transindex);
            for ($y = 0; $y < $new_height; ++$y) {
                for ($x = 0; $x < $new_width; ++$x) {
                    if (((imagecolorat($imageResized, $x, $y) >> 24) & 0x7F) >= 100) {
                        imagesetpixel(
                            $imageResized,
                            $x,
                            $y,
                            $transindex
                        );
                    }
                }
            }

        }
        # save GIF
        imagetruecolortopalette($imageResized, true, 255);
        imagesavealpha($imageResized, false);
    }
    return $imageResized;
}

原始功能来自PhpToys 1.0,适用于透明.gifs的部分来自此PHP文档注释。

推荐阅读
乐韵答题
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有