dongyuqie4322 2010-09-23 20:26
浏览 31
已采纳

上传文件后创建php缩略图

Hi friends what is the best way to create thumbnail on the fly after uploading an image ?

move_uploaded_file($source, $dest)
//uploaded successfully
//what will be code to generate thumbnail ?
  • 写回答

2条回答 默认 最新

  • doudang2817 2010-09-23 20:36
    关注

    TRY THIS...

    function create_img($file_name,$dir,$dest_dir,$maxwidth,$maxheight){
        /* adjust server memory size, file upload size, and post size
        *  htaccess file:
        *  php_value post_max_size 16M
        *  php_value upload_max_filesize 6M
        *  script:
        *  ini_set('memory_limit', '100M'); //handle large images
        */
        ini_set('memory_limit', '100M');
        if(file_exists($dir)){
                    $file = "$dir/$file_name";
                    if(!file_exists($dest_dir.$file_name)){
                        list($orig_width, $orig_height, $type) = getimagesize($file);
                        //calculate dimensions
                        if ($orig_width > $maxwidth){
                            $height = $maxwidth * ($orig_height / $orig_width);
                            $width = $maxwidth;
                            if($height>$maxheight){
                                $height = $maxheight;
                                $width = $maxheight * ( $orig_width / $orig_height);
                            }
                        }
                        elseif($orig_height > $maxheight){
                            $height = $maxheight;
                            $width = $maxheight * ($orig_width / $orig_height);
                            if($width>$maxwidth){
                                $height = $maxwidth * ($orig_height / $orig_width);
                                $width = $maxwidth;
                            }
                        }
                        else{
                            $width = $orig_width;
                            $height = $orig_height;    
                        }                    
    
                        $obr_p = @imagecreatetruecolor($width, $height);
                        if ($type == 1) {
                            $obr = @imagecreatefromgif($file);
                            imagecopyresampled($obr_p, $obr, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
                            imagegif($obr_p, "$dest_dir/$file_name");
                        }
    
                        else if($type == 2) {
                            $obr = imagecreatefromjpeg($file);
                            imagecopyresampled($obr_p, $obr, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
                            imagejpeg($obr_p, "$dest_dir/$file_name",100);
                        } 
                        else if($type == 3){
                            $obr = @imagecreatefrompng($file);
                            imagecopyresampled($obr_p, $obr, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
                            imagepng($obr_p, "$dest_dir/$file_name");
                        }             
                        else{
                            return FALSE;
                        }
                    }// if exist
                    else{
                        return FALSE;
                    }
        }//if exist dir
        else{
            return FALSE;
        }
    }//end create_img
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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