這篇文章主要介紹“php怎么實(shí)現(xiàn)上傳圖片生成縮略圖功能”,在日常操作中,相信很多人在php怎么實(shí)現(xiàn)上傳圖片生成縮略圖功能問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”php怎么實(shí)現(xiàn)上傳圖片生成縮略圖功能”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

<?php
function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth, $quality){
$details = getimagesize("$imageDirectory/$imageName") or die('Please only upload images.');
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$srcImg = imagecreatefrom'.$type.'("$imageDirectory/$imageName");');
$thumbHeight = $details[1] * ($thumbWidth / $details[0]);
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight,
$details[0], $details[1]);
eval('image'.$type.'($thumbImg, "$thumbDirectory/$imageName"'.
(($type=='jpeg')?', $quality':'').');');
imagedestroy($srcImg);
imagedestroy($thumbImg);
}
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "data/$name");
createThumbnail("/location/of/main/image", $name, "/location/to/store/thumb", 120, 80);
//120 = thumb width :: 80 = thumb quality (1-100)
}
}
?>接下來再為大家推薦一個(gè)實(shí)例php使用GD庫上傳圖片以及創(chuàng)建縮略圖,直接看代碼:
GD庫是PHP進(jìn)行圖象操作一個(gè)很強(qiáng)大的庫。
先在php.ini里增加一行引用:extension=php_gd2.dll
重啟apache,做一個(gè)測試頁var_dump(gd_info());輸出數(shù)據(jù)表明GD庫引用成功。
圖片上傳頁面 upload.html
<html> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> <title>圖片上傳</title> </head> <body> <h2>文件上傳(只允許上傳jpg類型圖片)</h2> <form enctype="multipart/form-data" action="upload_img.php" method="post"> <input name="upfile" type="file"><br><br> <input type="submit" value="提交"> </form> </body> </html>
處理頁面upload_img.php
<?php
//上傳圖片保存地址
$uploadfile = "upfiles/".$_FILES['upfile']['name'];
//縮略圖保存地址
$smallfile = "upfiles/small_".$_FILES['upfile']['name'];
if($_FILES['upfile']['type'] != "image/jpeg")
{
echo '文件類型錯(cuò)誤';
}
else
{
move_uploaded_file($_FILES['upfile']['tmp_name'],$uploadfile); //上傳文件
$dstW=200;//縮略圖寬
$dstH=200;//縮略圖高
$src_image=ImageCreateFromJPEG($uploadfile);
$srcW=ImageSX($src_image); //獲得圖片寬
$srcH=ImageSY($src_image); //獲得圖片高
$dst_image=ImageCreateTrueColor($dstW,$dstH);
ImageCopyResized($dst_image,$src_image,0,0,0,0,$dstW,$dstH,$srcW,$srcH);
ImageJpeg($dst_image,$smallfile);
echo '文件上傳成功<br>';
echo "<img src='$smallfile' />";
}
?>到此,關(guān)于“php怎么實(shí)現(xiàn)上傳圖片生成縮略圖功能”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站標(biāo)題:php怎么實(shí)現(xiàn)上傳圖片生成縮略圖功能-創(chuàng)新互聯(lián)
分享路徑:http://chinadenli.net/article28/hcecp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)、搜索引擎優(yōu)化、微信公眾號(hào)、云服務(wù)器、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容