php imagecopyresized()制作全黑缩略图


php imagecopyresized() making full black thumbnail

所以,我有一个工作了一半的类。不知怎的,我无法复制上传图像的重新调整大小的样本,只有一个具有"正确"尺寸的黑色"正方形"(拧紧尺寸,只要拇指清晰。一步一步)。

我为WOT感到抱歉,但它把我逼疯了。提前谢谢。

    <?php 
    class Upload {
#function from http://stackoverflow.com/a/10666106/587811
public function resize_values($origWidth,$origHeight,$maxWidth = 200,$maxHeight = 200){
    #check for longest side, we'll be seeing that to the max value above
    if($origHeight > $origWidth){ #if height is more than width
        $newWidth = ($maxHeight * $origWidth) / $origHeight;
        $retval = array(width => $newWidth, height => $maxHeight);
    }
    else{
        $newHeight= ($maxWidth * $origHeight) / $origWidth;
        $retval = array(width => $origWidth, height => $newHeight);
}
    return $retval;
}
public function image($picurl, $file, $path="images/uploaded/") {
    echo "function chamada!";
    if ($picurl) {
        $picFileName=rand(1,9999).$_SESSION['id'];
        $picExt=substr(strstr($picurl,'.'),1,3);
        $picExt=strtolower($picExt);
        $allowed = array("jpg","png","gif","bmp");
        if (in_array($picExt,$allowed)) {
            if (getimagesize($file)) {
                $picNewName=str_replace(" ","_",$picFileName.'.'.$picExt);
                $picWhereTo=$path.$picNewName;
                $copy=move_uploaded_file($file, $picWhereTo);       
                if ($copy) {
                    list($width, $height) = getimagesize($picWhereTo);
                    $size = $this->resize_values($width,$height,250,250);
                    $thumb = imagecreatetruecolor($size['width'],$size['height']);
    imagealphablending($thumb, false);
                    $source = imagecreatefromjpeg($picWhereTo);
                    imagecopyresized($thumb,$source,0,0,0,0,$size['width'],$size['height'],$width,$height);
                    $picNewName=$picFileName.'_thumb.'.$picExt;
                    imagejpeg($thumb,$path.$picNewName);
                    $picinfo['where']=$picWhereTo;
                    $picinfo['name']=$picNewName;
                    return $picinfo;
                }
                else return false;
            }
            else return false;
        }
        else return false;
    }
}
    }
    ?>

我遇到过类似的问题。这与png的透明度有关。

在您使用imagecreatetruecolor()创建$thumb之后,给它一个镜头;

imagealphablending($thumb, false);

我不完全确定这是解决方案,但我认为这是正确的。你的真实颜色支持alpha混合——它是在jpeg的背景中混合——它可能会因为缺乏信息而感到困惑。

如果这不起作用,请描述你上传的确切图像格式,这样我们就可以尝试一下,看看会发生什么。

更改

$source = imagecreatefromjpeg($file);

$source = imagecreatefromjpeg($picWhereTo);

这就是你如何调用函数

$objoflclass->image($_FILES['img']['name'],$_FILES['img']['tmp_name'],'');

其中$_FILES['img']是图像上传字段的名称,我相信从中你可以理解

的问题所在