学习中,php可以生成缩略图吗,

解决方案 »

  1.   

    可以生成function createThumbImage($original,$thumb_path,$new_width=50,$new_height=50,$ext=null)
    {
    if ($ext==null)
    {
    $ext=end(explode(".",$original));
    }
    $ext=strtolower($ext);
    $filename = $original;
    list($width, $height) = getimagesize($filename);
    $image_p = imagecreatetruecolor($new_width, $new_height); switch ($ext)
    {
    case 'jpg':
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_p, $thumb_path, 100);
    return true;
    case 'gif':
    $image = imagecreatefromgif($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagegif($image_p, $thumb_path, 100);
    return true;
    case 'png':
    $image = imagecreatefrompng($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagepng($image_p, $thumb_path, 100);
    return true;
    }
    return false;
    }
    这段代码我参考ice_berg的代码的,忘记了是否经过修改。。
      

  2.   

    摘自:木目子技术交流站
    /**
        * 生成一个缩略图
        * @params $Image:String;
        * @return String;
        */
        function make2small($Image)
        {
            $info=$this->getInfo($Image);
            if(!$info)
            {
                return 0;
            }        $file=$this->DOC_ROOT.$this->Path."/".$Image;        /*随即生成一个文件名*/
            $small=$this->make_rand(16).$this->mimePhoto[$info["mime"]];        if(@file_exists($this->DOC_ROOT.$this->Path."/".$small))
            {
                return $this->make2small($Image);
            }        /*如果大图小于小缩略图,则直接拷贝一份*/
            if($this->width>=$info["width"] && $this->heigh>=$info["heigh"])
            {
                if(@copy($file,$this->DOC_ROOT.$this->smallPath."/".$small))
                {
                    return $small;
                }else{
                    return 0;
                }
            }
            /*按照比例得到缩略图的高、宽*/
            if(($info["width"] / $info["heigh"]) > ($this->width / $this->heigh))
            {
                $w=$this->width;
                $h=floor($info["heigh"]*$w / $info["width"]);
            }else{
                $h=$this->heigh;
                $w=floor($info["width"]*$h / $info["heigh"]);
            }
            /*创建缩略图*/
            switch($info["mime"])
            {
                case "image/gif":
                    $im=@imagecreatefromgif($file);
                    break;
                case "image/pjpeg":
                case "image/jpeg":
                    $im=@imagecreatefromjpeg($file);
                    break;
                case "image/png":
                    $im=@imagecreatefrompng($file);
                    break;
                default:
                    return -1;
            }
            if($im)
            {
                $ni=@imagecreatetruecolor($w,$h);
                @imagecopyresampled($ni,$im,0,0,0,0,$w,$h,$info["width"],$info["heigh"]);
                $result=@imagejpeg($ni,$this->DOC_ROOT.$this->smallPath."/".$small);
                @imagedestroy($ni);
            }            @imagedestroy($im);
            if($result)
            {
                return $small;
            }
            return "NoSmall.gif";
        }