【问题标题】:generating transparent GIF images in php在 php 中生成透明的 GIF 图像
【发布时间】:2013-09-16 11:25:38
【问题描述】:

我有一个 php 脚本,它可以动态生成较大图像的缩略图而不将其存储在 Web 服务器上。问题是当图像是透明的 GIF 图像时,生成的缩略图不是透明的。它为透明PNG格式图像正确生成透明缩略图。唯一的问题是 GIF 图像的透明度。 这是脚本:

<?php

//ex . <img src="thumb.php?p=pp.png&q=4&w=60"/>
if(!isset($_GET['p'])){exit;}

if(!isset($_GET['h']) && !isset($_GET['w'])){
    list($w,$h,$type,$attr) = getimagesize($_GET['p']);
    $_GET['w'] = $w;
    $_GET['h'] = $h;
}else if(!isset($_GET['w'])){
    $_GET['w'] = 0;
}else if(!isset($_GET['h'])){
    $_GET['h'] = 0;
}


if(!isset($_GET['q'])){$_GET['q'] = 100;}

$info = pathinfo($_GET['p']);
switch($info['extension']){
    case 'gif' : $t = 1; break;
    case 'jpg' : $t = 2; break;
    case 'png' : $t = 3; break;
}

if($t==1){
    header("Content-Type: image/gif");
    $img = imagecreatefromgif($_GET['p']);
}else if($t==2){
    header("Content-Type: image/jpg");
    $img = imagecreatefromjpeg($_GET['p']);
}else if($t==3){
    header("Content-Type: image/png");
    $img = imagecreatefrompng($_GET['p']);

}else{
    exit;
}

$orSize = orSize($img);
$th = thSize($orSize,$_GET['w'],$_GET['h']);
$tmp_img = imagecreatetruecolor( $th['w'],$th['h'] );

//if image is GIF or PNG format, produce transparent images
if ($t==1 || $t==3) {
    imagealphablending($tmp_img, false);
    imagesavealpha($tmp_img, true); 
    if ($t==3)
    {
        $source = imagecreatefrompng($_GET['p']);
    }
    else {
        $source = imagecreatefromgif($_GET['p']);
    }
    imagealphablending($source, true);
    imagecopyresampled($tmp_img, $source, 0, 0, 0, 0, $th['w'], $th['h'], $orSize['w'], $orSize['h'] );
}else{
    imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $th['w'], $th['h'], $orSize['w'], $orSize['h'] );
}

if(isset($_GET['s'])){
    //S VAR OUTPUTS IT AS DIFFERENT FORMAT
    $t = $_GET['s'];
}

if(!isset($_GET['save'])){
    //SAVE VAR NEEDS TO BE THE PATH TO SAVE TO AND FILENAME
    $_GET['save'] = null;
}

if($t==1){
    imagegif($tmp_img,$_GET['save'],$_GET['q']);
}else if($t==2){
    imagejpeg($tmp_img,$_GET['save'],$_GET['q']);
}else if($t ==3){
    $_GET['q'] = 10 - ($_GET['q'] /= 10);
    imagepng($tmp_img,$_GET['save'],$_GET['q']);
}

imagedestroy($tmp_img);

function orSize($img){
    $orSize['w'] = imagesx($img);
    $orSize['h'] = imagesy($img);
    return $orSize;
}

function thSize($orSize,$w,$h){
    $th = Array();
    if($w!=0 && $h == 0){
        $th['w'] = $w;
        $th['h'] = floor( $orSize['h'] * ( $w / $orSize['w'] ) );
    }else if($w==0 && $h != 0){
        $th['h'] = $h;
        $th['w'] = floor( $orSize['w'] * ( $h / $orSize['h'] ) );
    }else{
        $th['w'] = $w; $th['h'] = $h;
    }
    return $th;
}
?>

脚本是这样工作的,生成缩略图只需传递图像名称如下:

 <img src="thumb.php?p=image.png&q=4&w=60"/>

如何使这个脚本生成 GIF 透明图像的透明缩略图? 生成透明图像的行如下:

    // if $t is gif or png image
if ($t==1 || $t==3) {
    imagealphablending($tmp_img, false);
    imagesavealpha($tmp_img, true); 
    if ($t==3)
    {
        $source = imagecreatefrompng($_GET['p']);
    }
    else {
        $source = imagecreatefromgif($_GET['p']);
    }
    imagealphablending($source, true);
    imagecopyresampled($tmp_img, $source, 0, 0, 0, 0, $th['w'], $th['h'], $orSize['w'], $orSize['h'] );
}else{
    imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $th['w'], $th['h'], $orSize['w'], $orSize['h'] );
}

【问题讨论】:

  • 有什么理由让你更喜欢 GIF 而不是 PNG?
  • 图片是用户上传的,所以我想要的是,如果用户上传了透明的 GIF 图片,我想显示 GIF 图片的透明度。我这边没有偏好。只是为了确保保留 png 和 gif 的透明度。

标签: php php-gd


【解决方案1】:

请务必在写入图像之前使用imagesavealpha

参考http://www.php.net/manual/en/function.imagesavealpha.php

【讨论】:

    猜你喜欢
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多