php利用GD庫生成縮略圖示例
更新時間:2014年01月16日 17:44:29 作者:
本文為大家介紹下php如何利用GD庫生成縮略圖的,下面有個不錯的示例,大家可以參考下
php利用GD庫生成縮略圖。
<form method="post" action="suo_do.php" enctype="multipart/form-data">
<input type="file" name="pic" />
<input type="submit" value="上傳1" />
</form>
<?php
header("content-type:text/html;charset=gbk");
ini_set("date.timezone","Asia/chong");
//判斷文件是否為空
if(empty($_FILES)){
echo"上傳文件過大";
exit;
}
//判斷文件上傳是否有錯誤
if($_FILES['pic']['error']){
echo "上傳文件";
exit;
}
//判斷文件類型是否非法獲取文件后綴
$allowtype=array("jpg","png","jpeg","gif");
$a=explode('.',$_FILES['pic']['name']);
$index=count($a)-1;
$ex=strtolower($a[$index]);
if(!in_array($ex,$allowtype)){
echo "上傳文件非法";
exit;
}
$file=date('YmdHis').rand().".".$ex;
$src=$_FILES['pic']['tmp_name'];
$des="upload/".$file;
$rs=move_uploaded_file($src,$des);
//縮略圖
//讀取已經(jīng)上傳圖片
$image=imagecreatefromjpeg($des);
$a=getimagesize($des);
$w=$a[0];
$h=$a[1];
if($w>$h){
$width=300;
$height=$width/$w*$h;
}else if($w<$h){
$height=300;
$width=$height/$h*$w;
}else{
$width=300;
$height=300;
}
//創(chuàng)建空白新圖片
$newimage=imagecreatetruecolor($width, $height);
//copy源圖片內(nèi)容 copy新圖片
imagecopyresized($newimage, $image, 0,0, 0,0, $width, $height, $w, $h);
$filename="upload/s_".$file;
imagejpeg($newimage,$filename);
復(fù)制代碼 代碼如下:
<form method="post" action="suo_do.php" enctype="multipart/form-data">
<input type="file" name="pic" />
<input type="submit" value="上傳1" />
</form>
<?php
header("content-type:text/html;charset=gbk");
ini_set("date.timezone","Asia/chong");
//判斷文件是否為空
if(empty($_FILES)){
echo"上傳文件過大";
exit;
}
//判斷文件上傳是否有錯誤
if($_FILES['pic']['error']){
echo "上傳文件";
exit;
}
//判斷文件類型是否非法獲取文件后綴
$allowtype=array("jpg","png","jpeg","gif");
$a=explode('.',$_FILES['pic']['name']);
$index=count($a)-1;
$ex=strtolower($a[$index]);
if(!in_array($ex,$allowtype)){
echo "上傳文件非法";
exit;
}
$file=date('YmdHis').rand().".".$ex;
$src=$_FILES['pic']['tmp_name'];
$des="upload/".$file;
$rs=move_uploaded_file($src,$des);
//縮略圖
//讀取已經(jīng)上傳圖片
$image=imagecreatefromjpeg($des);
$a=getimagesize($des);
$w=$a[0];
$h=$a[1];
if($w>$h){
$width=300;
$height=$width/$w*$h;
}else if($w<$h){
$height=300;
$width=$height/$h*$w;
}else{
$width=300;
$height=300;
}
//創(chuàng)建空白新圖片
$newimage=imagecreatetruecolor($width, $height);
//copy源圖片內(nèi)容 copy新圖片
imagecopyresized($newimage, $image, 0,0, 0,0, $width, $height, $w, $h);
$filename="upload/s_".$file;
imagejpeg($newimage,$filename);
您可能感興趣的文章:
- PHP GD庫生成圖像的幾個函數(shù)總結(jié)
- php實現(xiàn)的支持imagemagick及gd庫兩種處理的縮略圖生成類
- 使用gd庫實現(xiàn)php服務(wù)端圖片裁剪和生成縮略圖功能分享
- PHP用GD庫生成高質(zhì)量的縮略圖片
- php的GD庫imagettftext函數(shù)解決中文亂碼問題
- 可定制的PHP縮略圖生成程式(需要GD庫支持)
- PHP5中GD庫生成圖形驗證碼(有漢字)
- PHP基于GD庫的縮略圖生成代碼(支持jpg,gif,png格式)
- PHP中使用GD庫創(chuàng)建圓形餅圖的例子
- PHP中使用GD庫繪制折線圖 折線統(tǒng)計圖的繪制方法
- php 使用GD庫為頁面增加水印示例代碼
- php中使用gd庫實現(xiàn)下載網(wǎng)頁中所有圖片
- PHP基于GD庫的圖像處理方法小結(jié)
相關(guān)文章
使用Visual Studio Code連接MySql數(shù)據(jù)庫并進行查詢
這篇文章主要介紹了使用Visual Studio Code連接MySql數(shù)據(jù)庫并進行查詢,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02簡單解決Windows中MySQL的中文亂碼與服務(wù)啟動問題
這篇文章主要介紹了Windows中MySQL的中文亂碼與服務(wù)啟動問題,如果程序沒有特殊需要則建議MySQL盡量默認設(shè)為UTF-8格式編碼,需要的朋友可以參考下2016-03-03