Nginx配合php实现生成实时缩略图功能

2019-10-17 20:36:07王冬梅

        $ext = strtolower ( pathinfo ( $src, 4 ) );
        if ($imageinfos [2] == 1) {
            $im = imagecreatefromgif ( $src );
        } elseif ($imageinfos [2] == 2) {
            $im = imagecreatefromjpeg ( $src );
        } elseif ($imageinfos [2] == 3) {
            $im = imagecreatefrompng ( $src );
        }
        
        if (isset ( $im )) {
            $height = $imageinfos [1] * $width / $imageinfos [0];
            $dst_img = ImageCreateTrueColor ( $width, $height );
            
            imagesavealpha ( $dst_img, true );
            $trans_colour = imagecolorallocatealpha ( $dst_img, 0, 0, 0, 127 );
            imagefill ( $dst_img, 0, 0, $trans_colour );
            
            imagecopyresampled ( $dst_img, $im, 0, 0, 0, 0, $width, $height, $imageinfos [0], $imageinfos [1] );
            
            header ( 'content-type:image/jpg' );
            imagejpeg ( $dst_img, null, 90 );//输出文件流,90--压缩质量,100表示最高质量。
            
            @imagedestroy ( $im );
            @imagedestroy ( $dst_img );
        } else {
            echo @file_get_contents ( $src );
        }
        $content = ob_get_contents ();//获取输出流
        ob_end_flush ();//输出流到网页,保证第一次请求也有图片数据放回
        @file_put_contents ( $des, $content );//保存文件
    }
?>

效果图: