亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

生成縮略圖

1,獲取上傳頭像的寬和高

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28 0028
 * Time: 下午 1:18
 */
header('Content-type:text/html;charset=utf-8');
$info=array('id'=>1,'name'=>'張三');
//接收并處理上傳圖像
if(!empty($_FILES['pic'])){
    $pic_info=$_FILES['pic'];
    //獲取圖片大小
    $image_info=getimagesize($pic_info['tmp_name']);
    echo '<pre>';
    print_r($image_info);
    echo '</pre>';
    $width=$image_info[0];
    $height=$image_info[1];
}
?>
<form action="" method="post" enctype="multipart/form-data">
    <h2>編輯用戶頭像</h2>
    <p>用戶姓名:<?php echo $info['name'];?></p>
    <p>現(xiàn)有頭像:</p><img src="<?php echo './'.$info['id'].'.jpg?rand='.rand() ;?>" onerror="this.src='./default.jpg'" /><br>
    上傳頭像:<input name="pic" type="file"><br>
    <input type="submit" value="保存頭像">
</form>

運行展示如下:

微信圖片_20180228153615.png

2,計算縮略圖的最大寬度和高度

<?php
//設置最大寬度和高度
$maxwidth=$maxheight=90;
//自動計算縮略圖的寬和高
if($width>$height){
    //第一種方式
    $newwidth=$maxwidth;
    $newheight=round($newwidth*$height/$width);
}else{
    $newheight=$maxheight;
    $newwidth=round($newheight*$width/$height);
}
  //第二種方式
//    $percent=0.2;   //定義縮略圖的縮放比例
//    $newwidth=$width*$percent;
//    $newheight=$height*$percent;

3,依據原圖制作縮略圖

<?php
//繪制畫布
$thumb=imagecreatetruecolor($newwidth,$newheight);
//依據原圖創(chuàng)建一個與原圖一樣的新的圖像
$source=imagecreatefromjpeg($pic_info['tmp_name']);
//依據原圖創(chuàng)建縮略圖
imagecopyresized($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);

4,將縮略圖保存到指定目錄

<?php
//設置縮略圖保存路徑
    $new_file='./'.$info['id'].'.jpg';
    //保存縮略圖到指定目錄
//    header('Content-type:image/jpeg');
    imagejpeg($thumb,$new_file,100);

5,查看運行結果

完整代碼如下:

imagesize.php:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28 0028
 * Time: 下午 1:18
 */
header('Content-type:text/html;charset=utf-8');
$info=array('id'=>1,'name'=>'張三');
//接收并處理上傳圖像
if(!empty($_FILES['pic'])){
    $pic_info=$_FILES['pic'];
    //獲取圖片大小
    $image_info=getimagesize($pic_info['tmp_name']);
    echo '<pre>';
    print_r($image_info);
    echo '</pre>';
    $width=$image_info[0];
    $height=$image_info[1];
    //設置最大寬度和高度
    $maxwidth=$maxheight=90;
    //自動計算縮略圖的寬和高
    //第一種方式
    if($width>$height){
        $newwidth=$maxwidth;
        $newheight=round($newwidth*$height/$width);
    }else{
        $newheight=$maxheight;
        $newwidth=round($newheight*$width/$height);
    }
    //第二種方式
//    $percent=0.2;   //定義縮略圖的縮放比例
//    $newwidth=$width*$percent;
//    $newheight=$height*$percent;
    //繪制畫布
    $thumb=imagecreatetruecolor($newwidth,$newheight);
    //依據原圖創(chuàng)建一個與原圖一樣的新的圖像
    $source=imagecreatefromjpeg($pic_info['tmp_name']);
    //依據原圖創(chuàng)建縮略圖
    imagecopyresized($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);
    //設置縮略圖保存路徑
    $new_file='./'.$info['id'].'.jpg';
    //保存縮略圖到指定目錄
//    header('Content-type:image/jpeg');
    imagejpeg($thumb,$new_file,100);
}
?>
<form action="" method="post" enctype="multipart/form-data">
    <h2>編輯用戶頭像</h2>
    <p>用戶姓名:<?php echo $info['name'];?></p>
    <p>現(xiàn)有頭像:</p><img src="<?php echo './'.$info['id'].'.jpg?rand='.rand() ;?>" onerror="this.src='./default.jpg'" /><br>
    上傳頭像:<input name="pic" type="file"><br>
    <input type="submit" value="保存頭像">
</form>

運行結果如下:

微信圖片_20180228161426.png

繼續(xù)學習
||
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/2/28 0028 * Time: 下午 1:18 */ header('Content-type:text/html;charset=utf-8'); $info=array('id'=>1,'name'=>'張三'); //接收并處理上傳圖像 if(!empty($_FILES['pic'])){ $pic_info=$_FILES['pic']; //獲取圖片大小 $image_info=getimagesize($pic_info['tmp_name']); echo '<pre>'; print_r($image_info); echo '</pre>'; $width=$image_info[0]; $height=$image_info[1]; //設置最大寬度和高度 $maxwidth=$maxheight=90; //自動計算縮略圖的寬和高 //第一種方式 if($width>$height){ $newwidth=$maxwidth; $newheight=round($newwidth*$height/$width); }else{ $newheight=$maxheight; $newwidth=round($newheight*$width/$height); } //第二種方式 // $percent=0.2; //定義縮略圖的縮放比例 // $newwidth=$width*$percent; // $newheight=$height*$percent; //繪制畫布 $thumb=imagecreatetruecolor($newwidth,$newheight); //依據原圖創(chuàng)建一個與原圖一樣的新的圖像 $source=imagecreatefromjpeg($pic_info['tmp_name']); //依據原圖創(chuàng)建縮略圖 imagecopyresized($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height); //設置縮略圖保存路徑 $new_file='./'.$info['id'].'.jpg'; //保存縮略圖到指定目錄 // header('Content-type:image/jpeg'); imagejpeg($thumb,$new_file,100); } ?> <form action="" method="post" enctype="multipart/form-data"> <h2>編輯用戶頭像</h2> <p>用戶姓名:<?php echo $info['name'];?></p> <p>現(xiàn)有頭像:</p><img src="<?php echo './'.$info['id'].'.jpg?rand='.rand() ;?>" onerror="this.src='./default.jpg'" /><br> 上傳頭像:<input name="pic" type="file"><br> <input type="submit" value="保存頭像"> </form>
提交重置代碼