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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog