0

我们有一些相当老的代码可以上传、调整大小然后保存图像,以便可以在我们的 CMS 中使用。

除非我们上传 24 位 PNG,否则一切正常,因为出于某种原因,它会将其转换为 8 位 PNG,无论如何要解决这个问题?

附件是有问题的功能:

function resize_png( $origfile,$newfile,$endh,$endw,$imagequal,$crop,$watermrkimg=false) {

    $imagequal = floor($imagequal/10);          //needed to give a quality of between 0-9 as required by php5 (php4 allowed 0-99)

$return_val = 1;
if (!$return_val = ( ($img = ImageCreateFromPNG ( $origfile )) && $return_val == 1 ) ? "1" : "0" ) {
    quit_on_error ("Your picture is corrupt, please try resaving it or uploading another picture","Image Error",'1');
}

// 1. get transparent colour
$colorTransparent = imagecolortransparent($img);

$origw = imagesx ($img);                                                            // Original image width
$origh = imagesy ($img);                                                            // Original image height

$ratiow = $origw / $endw;                                                   //get ratios of current dimension against min dimension
$ratioh = $origh / $endh;

if ($ratiow == $ratioh) {                                                       //if image is already correctly proportioned
    $neww = round($origw / $ratiow);
    $newh = round($origh / $ratioh);
    $offsetw = '0';
    $offseth = '0';
}
elseif ($ratioh < $ratiow) {                                                    // if image is wide

    if ($crop == '1')
    {
        $neww = round($origw / $ratioh);                                        // this will be too wide
        $newh = round($origh / $ratioh);                                        // this will be perfect
    }
    else {
        $neww = round($origw / $ratiow);                                        // this will be perfect
        $newh = round($origh / $ratiow);                                        // this will be to short
    }

    $offseth = '0';                                                             // as height perfect
    $offsetw = round(($neww - $endw) / 2);                                      // horizontally centred
}
else  {                                                                         // if image is tall
    if ($crop == '1')
    {
        $neww = round($origw / $ratiow);                                            // this will be perfect
        $newh = round($origh / $ratiow);                                            // this will be too tall
    }
    else
    {
        $neww = round($origw / $ratioh);                                            // this will be too thin
        $newh = round($origh / $ratioh);                                            // this will be perfect

    }
    $offsetw = '0';                                                             // as width perfect
    $offseth = round(($newh - $endh) / 2);                                      // vertically centred
}

if($crop != '1')            // If original is smaller then don't resize at all, then quality will be better
{                           // ... unless of course we are cropping for a thumbnail, then we want to be resized up ...
    if (($origw < $neww) && ($origh < $newh))
    {
         $neww = $origw;
        $newh = $origh;
    }
}

$resized_id = ImageCreate( $neww , $newh );                                     // create an image to resize the image proportionally

// 2. Set transparent colour
imagepalettecopy($resized_id, $img);
imagefill($resized_id,0,0,$colorTransparent);
imagecolortransparent($resized_id,$colorTransparent);

ImageCopyResampled( $resized_id, $img,                                          // resize image - no cropping, so may be too big in one dimension
                0,0,                                                            // dst x,y
                0,0,                                                            // src LR,UD
                $neww, $newh,
                $origw, $origh );

if ($crop == '1')
{
    $resized_cropped_id = ImageCreate( $endw , $endh );                             // create an image to crop the oversized dimension

    // 2. Set transparent colour for cropped image
    imagepalettecopy($resized_cropped_id, $img);
    imagefill($resized_cropped_id,0,0,$colorTransparent);
    imagecolortransparent($resized_cropped_id,$colorTransparent);

    ImageCopyResampled( $resized_cropped_id, $resized_id,                               // crop image - so right size
                    0,0,                                                            // dst x,y
                    $offsetw,$offseth,                                              // src LR,UD
                    $endw, $endh,
                    $endw, $endh);


    $return_val = ( $full = ImagePNG( $resized_cropped_id, $newfile, $imagequal )   // save jpeg to destination
                 && $return_val == 1 ) ? "1" : "0";
     ImageDestroy( $resized_cropped_id );
}
else
{
    $return_val = ( $full = ImagePNG( $resized_id, $newfile, $imagequal )   // save jpeg to destination
                 && $return_val == 1 ) ? "1" : "0";

}

ImageDestroy( $resized_id );                                                    // wipe memory for temp images
ImageDestroy( $img );

return ($return_val) ? TRUE : FALSE ;

}

谢谢大家,我现在已经使用 get_png_imageinfo 和 imagecreatetruecolor 修复了它!

4

1 回答 1

2

imagecreate使用最大尺寸为 256 色的颜色表创建基于调色板的图像。

imagecreatetruecolor没有这样的限制,但当然会带来文件大小大很多倍的缺点。

于 2012-07-09T10:59:43.523 回答