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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题