??? ??
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>
??? ??? ????.
2. ???? ?? ??? ??? ?????. <?php
//設(shè)置最大寬度和高度
$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;
<?php
//繪制畫布
$thumb=imagecreatetruecolor($newwidth,$newheight);
//依據(jù)原圖創(chuàng)建一個與原圖一樣的新的圖像
$source=imagecreatefromjpeg($pic_info['tmp_name']);
//依據(jù)原圖創(chuàng)建縮略圖
imagecopyresized($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);
<?php
//設(shè)置縮略圖保存路徑
$new_file='./'.$info['id'].'.jpg';
//保存縮略圖到指定目錄
// header('Content-type:image/jpeg');
imagejpeg($thumb,$new_file,100);
?? ??
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]; //設(shè)置最大寬度和高度 $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); //依據(jù)原圖創(chuàng)建一個與原圖一樣的新的圖像 $source=imagecreatefromjpeg($pic_info['tmp_name']); //依據(jù)原圖創(chuàng)建縮略圖 imagecopyresized($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height); //設(shè)置縮略圖保存路徑 $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>
?? ??? ??? ????.