douba8048 2013-02-18 18:32
浏览 37

php imagecopyresized()制作全黑缩略图

So, I have this class that's half-working. Somehow I'm not being able to copy a re-sized sample of the uploaded image, only a black "square" with the "correct" dimensions (screw the dimensions, as long as the thumb comes up clear. one step at the time).

I'm sorry for the WOT but it's driving me cray-cray. Thanks in advance.

    <?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;
    }
}
    }
    ?>
  • 写回答

2条回答 默认 最新

  • dongzhi7641 2013-02-18 18:39
    关注

    I've ran into a similar problem like this. This has to do with png's with transparency.

    Give this a shot after you create $thumb using imagecreatetruecolor();

    imagealphablending($thumb, false);
    

    I'm not entirely certain this is the solution - but I think its along the right track. Your true color supports alpha blending - and it is blending in the background from the jpeg - and it might be confused by the lack of information.

    If this doesn't work, please describe the exact image format you are uploading so we can try it out and see what happens.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据