Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php调整脚本大小不需要';不适用于透明gif';s_Php_Transparency_Gd_Gif - Fatal编程技术网

php调整脚本大小不需要';不适用于透明gif';s

php调整脚本大小不需要';不适用于透明gif';s,php,transparency,gd,gif,Php,Transparency,Gd,Gif,我怎样才能使它适用于透明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=@ima

我怎样才能使它适用于透明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("<b>Error: </b>Unable to create image $newImage. Check directory permissions.");
            }   

          imagedestroy($image_org);
            imagedestroy($img_copy);
        }   
    }   
函数resizeImage($image,$newImage,$target\u width,$target\u height,$type=”“){
如果(是_文件($image)){
如果($type==“.gif”){
$image\u org=@imagecreatefromformgif($image);
}否则{
$image_org=@imagecreatefromjpeg($image);
}
如果($image\u org){
列表($w,$h,$type,$attr)=getimagesize($image);
$factor=C_Image_Custom::calcRescaleFactor($w,$h,$target_width,$target_height);
如果($factor>1){
$image_w=$w/$factor;
$image_h=$h/$factor;
}否则{
$image_w=$w;
$image_h=$h;
}       
//注意:ImageCreateTureColor需要带有GD2.0的PHP
$img\u copy=imagecreatetruecolor($image\u w,$image\u h);
imagecopyresampled($img\u copy,$image\u org,0,0,0,$image\u w,$image\u h,$w,$h);
if(@imagejpeg($img_copy,$newImage,80)){
chmod($newImage,0777);
}否则{
echo(“错误:无法创建映像$newImage。请检查目录权限。”);
}   
图像销毁($image\u org);
图像销毁(img_副本);
}   
}   

看起来您只输出到jpeg,而jpeg没有透明度。如果要输出透明度,需要输出gif或png

如果您想用颜色替换透明度,我想您需要php函数imagecolorallocatealpha

为什么只使用jpeg

这也适用于gif:

if($type == ".gif"){
                    $image_org=@imagecreatefromgif($image);
            }else{
                    $image_org=@imagecreatefromjpeg($image);
            }

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

function resizeImage($originalImage、$toWidth、$toHeight、$isJPG)
{
//获取原始几何体并计算比例
列表($width,$height)=getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;
//使用默认比率重新计算新大小
如果($yscale>$xscale){
$new_width=圆形($width*(1/$yscale));
$new_height=圆形($height*(1/$yscale));
}否则{
$new_width=圆形($width*(1/$xscale));
$new_height=圆形($height*(1/$xscale));
}
//调整原始图像的大小
如果($isJPG){
$imageResized=ImageCreateTureColor($new\u width,$new\u height);
$imageTmp=imagecreatefromjpeg($originalImage);
imagecopyresampled($imageResized,$imageTmp,0,0,0,$new_width,$new_height,$width,$height);
}否则{
//$imageResized=ImageCreateTureColor($new\u width,$new\u height);
//$imageTmp=imagecreatefromgif($originalImage);
//imagecopyresampled($imageResized,$imageTmp,0,0,0,$new_width,$new_height,$width,$height);
#下面是调整gif大小的步骤,透明与否
# http://ru2.php.net/imagecopyresampled
#加载/创建图像
$imageTmp=imagecreatefromgif($originalImage);
$imageResized=ImageCreateTureColor($new\u width,$new\u height);
imagealphablending($imageResized,false);
#获取并重新分配透明度颜色
$transindex=imagecolortransparent($imageTmp);
如果($transindex>=0){
$transcol=imagecolorsforindex($imageTmp,$transindex);
$transindex=imagecolorallocatealpha(
$imageResized,
$transcol[“红色”],
$transcol[“绿色”],
$transcol[“蓝色”],
127
);
imagefill($imageResized,0,0,$transindex);
}
#重采样
imagecopyresampled($imageResized,$imageTmp,0,0,0,$new_width,$new_height,$width,$height);
#恢复透明度
如果($transindex>=0){
imagecolortransparent($imageResized,$transindex);
对于($y=0;$y<$new_height;++$y){
对于($x=0;$x<$new\u width;++$x){
如果((imagecolorat($imageResized,$x,$y)>>24和0x7F)>=100){
图像设置像素(
$imageResized,
$x,
$y,
$transindex
);
}
}
}
}
#保存GIF
imagetruecolortopalette($imageResized,true,255);
imagesavealpha($imageResized,false);
}
返回$imageResized;
}

原始函数来自PhpToys 1.0,与transparent.gifs一起工作的部分来自于。

我也有同样的问题,透明度不适用于PNG,当它与PNG一起工作时,它不适用于GIF,我整天都在寻找解决方案,直到找到此函数为止“”

一些变量上只有一些小错误,因此您必须按如下方式分配它们以删除$trnprt_indx和$trnprt_color的错误

$trnprt_color['red'] = 255;
$trnprt_color['green'] = 255;
$trnprt_color['blue'] = 255;
$trnprt_indx = 127;

希望这能对你有所帮助

这真的很老了,但万一其他人也像我一样难以理解,下面是对我有用的解释:

<?php
$sourceImage = imagecreatefromgif('./enjoy.gif');
        
$newWidth = 220;
$newHeight = 120;

//Blank canvas
$destImage = imagecreatetruecolor($newWidth, $newHeight);

$transColorIndex = imagecolortransparent($sourceImage); 
// Returns the index of the transparent color: 119

$transRGBColor = imagecolorsforindex($sourceImage, $transColorIndex); 
//Returns the RGB of the index color: [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 
//In this case it's white but can be anaything

$transGdColor = imagecolorallocate($destImage, $transRGBColor['red'], $transRGBColor['green'], $transRGBColor['blue']); 
// Returns: 16777215   //A GD color identifier created with imagecolorallocate().

imagefill($destImage, 0, 0, $transGdColor);
//Fills the blank image with that color

imagecolortransparent($destImage, $transGdColor);
//Sets that color as transparent

//Resample
imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($sourceImage), imagesy($sourceImage));

header ( 'Content-type:image/gif' );
imagegif($destImage);

你能不能提供更多关于正在发生的事情的信息,而不是制作透明的GIF?这是输入图像,输出仅为JPEG,请看这行:“如果(@imagejpeg($img_copy,$newImage,80)){”。这正是gif和png所需要的。非常好的解决方案,感谢您的分享。
$trnprt_indx
$trnprt_color
未定义或为空,如果它是错误工作的。
$trnprt_color['red'] = 255;
$trnprt_color['green'] = 255;
$trnprt_color['blue'] = 255;
$trnprt_indx = 127;
<?php
$sourceImage = imagecreatefromgif('./enjoy.gif');
        
$newWidth = 220;
$newHeight = 120;

//Blank canvas
$destImage = imagecreatetruecolor($newWidth, $newHeight);

$transColorIndex = imagecolortransparent($sourceImage); 
// Returns the index of the transparent color: 119

$transRGBColor = imagecolorsforindex($sourceImage, $transColorIndex); 
//Returns the RGB of the index color: [red] => 255 [green] => 255 [blue] => 255 [alpha] => 127 
//In this case it's white but can be anaything

$transGdColor = imagecolorallocate($destImage, $transRGBColor['red'], $transRGBColor['green'], $transRGBColor['blue']); 
// Returns: 16777215   //A GD color identifier created with imagecolorallocate().

imagefill($destImage, 0, 0, $transGdColor);
//Fills the blank image with that color

imagecolortransparent($destImage, $transGdColor);
//Sets that color as transparent

//Resample
imagecopyresampled($destImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($sourceImage), imagesy($sourceImage));

header ( 'Content-type:image/gif' );
imagegif($destImage);