php上傳圖片并壓縮的實(shí)現(xiàn)方法
本文實(shí)例講解了php上傳圖片并壓縮的實(shí)現(xiàn)方法,之前一篇《PHP實(shí)現(xiàn)圖片上傳并壓縮》已經(jīng)為大家進(jìn)行了簡(jiǎn)單介紹,此次實(shí)現(xiàn)上傳圖片然后按照比例縮略圖,指定縮略圖的最大高度或者最大寬度,具體內(nèi)容如下
實(shí)現(xiàn)代碼:
<?php function _UPLOADPIC($upfile, $maxsize, $updir, $newname = 'date') { if ($newname == 'date') $newname = date ( "Ymdhis" ); //使用日期做文件名 $name = $upfile ["name"]; $type = $upfile ["type"]; $size = $upfile ["size"]; $tmp_name = $upfile ["tmp_name"]; switch ($type) { case 'image/pjpeg' : case 'image/jpeg' : $extend = ".jpg"; break; case 'image/gif' : $extend = ".gif"; break; case 'image/png' : $extend = ".png"; break; } if (emptyempty ( $extend )) { echo ( "警告!只能上傳圖片類型:GIF JPG PNG" ); exit (); } if ($size > $maxsize) { $maxpr = $maxsize / 1000; echo ( "警告!上傳圖片大小不能超過" . $maxpr . "K!" ); exit (); } if (move_uploaded_file ( $tmp_name, $updir . $newname . $extend )) { return $updir . $newname . $extend; } } function show_pic_scal($width, $height, $picpath) { $imginfo = GetImageSize ( $picpath ); $imgw = $imginfo [0]; $imgh = $imginfo [1]; $ra = number_format ( ($imgw / $imgh), 1 ); //寬高比 $ra2 = number_format ( ($imgh / $imgw), 1 ); //高寬比 if ($imgw > $width or $imgh > $height) { if ($imgw > $imgh) { $newWidth = $width; $newHeight = round ( $newWidth / $ra ); } elseif ($imgw < $imgh) { $newHeight = $height; $newWidth = round ( $newHeight / $ra2 ); } else { $newWidth = $width; $newHeight = round ( $newWidth / $ra ); } } else { $newHeight = $imgh; $newWidth = $imgw; } $newsize [0] = $newWidth; $newsize [1] = $newHeight; return $newsize; } function getImageInfo($src) { return getimagesize($src); } /** * 創(chuàng)建圖片,返回資源類型 * @param string $src 圖片路徑 * @return resource $im 返回資源類型 * **/ function create($src) { $info=getImageInfo($src); switch ($info[2]) { case 1: $im=imagecreatefromgif($src); break; case 2: $im=imagecreatefromjpeg($src); break; case 3: $im=imagecreatefrompng($src); break; } return $im; } /** * 縮略圖主函數(shù) * @param string $src 圖片路徑 * @param int $w 縮略圖寬度 * @param int $h 縮略圖高度 * @return mixed 返回縮略圖路徑 * **/ function resize($src,$w,$h) { $temp=pathinfo($src); $name=$temp["basename"];//文件名 $dir=$temp["dirname"];//文件所在的文件夾 $extension=$temp["extension"];//文件擴(kuò)展名 $savepath="{$dir}/{$name}";//縮略圖保存路徑,新的文件名為*.thumb.jpg //獲取圖片的基本信息 $info=getImageInfo($src); $width=$info[0];//獲取圖片寬度 $height=$info[1];//獲取圖片高度 $per1=round($width/$height,2);//計(jì)算原圖長寬比 $per2=round($w/$h,2);//計(jì)算縮略圖長寬比 //計(jì)算縮放比例 if($per1>$per2||$per1==$per2) { //原圖長寬比大于或者等于縮略圖長寬比,則按照寬度優(yōu)先 $per=$w/$width; } if($per1<$per2) { //原圖長寬比小于縮略圖長寬比,則按照高度優(yōu)先 $per=$h/$height; } $temp_w=intval($width*$per);//計(jì)算原圖縮放后的寬度 $temp_h=intval($height*$per);//計(jì)算原圖縮放后的高度 $temp_img=imagecreatetruecolor($temp_w,$temp_h);//創(chuàng)建畫布 $im=create($src); imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height); if($per1>$per2) { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return addBg($savepath,$w,$h,"w"); //寬度優(yōu)先,在縮放之后高度不足的情況下補(bǔ)上背景 } if($per1==$per2) { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return $savepath; //等比縮放 } if($per1<$per2) { imagejpeg($temp_img,$savepath, 100); imagedestroy($im); return addBg($savepath,$w,$h,"h"); //高度優(yōu)先,在縮放之后寬度不足的情況下補(bǔ)上背景 } } /** * 添加背景 * @param string $src 圖片路徑 * @param int $w 背景圖像寬度 * @param int $h 背景圖像高度 * @param String $first 決定圖像最終位置的,w 寬度優(yōu)先 h 高度優(yōu)先 wh:等比 * @return 返回加上背景的圖片 * **/ function addBg($src,$w,$h,$fisrt="w") { $bg=imagecreatetruecolor($w,$h); $white = imagecolorallocate($bg,255,255,255); imagefill($bg,0,0,$white);//填充背景 //獲取目標(biāo)圖片信息 $info=getImageInfo($src); $width=$info[0];//目標(biāo)圖片寬度 $height=$info[1];//目標(biāo)圖片高度 $img=create($src); if($fisrt=="wh") { //等比縮放 return $src; } else { if($fisrt=="w") { $x=0; $y=($h-$height)/2;//垂直居中 } if($fisrt=="h") { $x=($w-$width)/2;//水平居中 $y=0; } imagecopymerge($bg,$img,$x,$y,0,0,$width,$height,100); imagejpeg($bg,$src,100); imagedestroy($bg); imagedestroy($img); return $src; } } ?>
使用方法:
$filename=(_UPLOADPIC($_FILES["upload"],$maxsize,$updir,$newname='date')); $show_pic_scal=show_pic_scal(230, 230, $filename); resize($filename,$show_pic_scal[0],$show_pic_scal[1]);
希望本文所述對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。
相關(guān)文章
Thinkphp結(jié)合AJAX長輪詢實(shí)現(xiàn)PC與APP推送詳解
這篇文章主要給大家介紹了關(guān)于Thinkphp結(jié)合AJAX長輪詢實(shí)現(xiàn)PC與APP推送的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07thinkphp實(shí)現(xiàn)163、QQ郵箱收發(fā)郵件的方法
這篇文章主要介紹了thinkphp實(shí)現(xiàn)163等郵箱收發(fā)郵件的方法,在163網(wǎng)易郵箱上已測(cè)試,特分享給大家2015-12-12php實(shí)現(xiàn)解析xml并生成sql語句的方法
這篇文章主要介紹了php實(shí)現(xiàn)解析xml并生成sql語句的方法,涉及php針對(duì)xml格式文件的讀取、解析及sql字符串拼接相關(guān)操作技巧,需要的朋友可以參考下2018-02-02將一維或多維的數(shù)組連接成一個(gè)字符串的php代碼
自定義一個(gè)函數(shù) ,把一個(gè)數(shù)組變成用,(逗號(hào))連接起來的字符串 (注意:應(yīng)考慮到多維數(shù)組的情況,并以返回值的形式返回)2010-08-08PHP的命令行擴(kuò)展Readline相關(guān)函數(shù)的使用
PHP 作為一個(gè) Web 開發(fā)語言,相對(duì)來說,命令行程序并不是它的主戰(zhàn)場(chǎng)。所以很多年輕的 PHP 開發(fā)者可能連命令行腳本都沒有寫過,更別提交互式的命令操作了。而今天,我們帶來的這個(gè)擴(kuò)展就是針對(duì) PHP 的交互式命令行操作的2021-05-05mac系統(tǒng)下為 php 添加 pcntl 擴(kuò)展
pcntl中php實(shí)現(xiàn)多進(jìn)程必須要安裝的擴(kuò)展,本文給大家簡(jiǎn)單介紹下如何在mac系統(tǒng)中為 php 添加 pcntl 擴(kuò)展2016-08-08