這里會使用到三個文件:
connect.php:連接資料庫
test_upload.php:執行SQL語句
upload_img.php:上傳圖片並壓縮
三個文件代碼如下:
連接資料庫:connect.php
<?php
$db_host='';
$db_user='';
$db_psw='';
$db_name='';
$db_port='';
$sqlconn=newmysqli($db_host,$db_user,$db_psw,$db_name);
$q="setnamesutf8;";
$result=$sqlconn->query($q);
if(mysqli_connect_errno()){
printf("Connectfailed:%s ",mysqli_connect_error());
exit();
}
?>
當然使用一些封裝的資料庫類也是可以的。
執行SQL語句:test_upload.php
<?php
require("connect.php");
require("upload_img.php");
$real_img=$uploadfile;
$small_img=$uploadfile_resize;
$insert_sql="insertintoimg(real_img,small_img)values(?,?)";
$result=$sqlconn->prepare($insert_sql);
$result->bind_param("ss",$real_img,$small_img);
$result->execute();
?>
上傳圖片並壓縮:upload_img.php
<?php
//設置文件保存目錄
$uploaddir="upfiles/";
//設置允許上傳文件的類型
$type=array("jpg","gif","bmp","jpeg","png");
//獲取文件後綴名函數
functionfileext($filename)
{
returnsubstr(strrchr($filename,'.'),1);
}
//生成隨機文件名函數
functionrandom($length)
{
$hash='CR-';
$chars='';
$max=strlen($chars)-1;
mt_srand((double)microtime()*1000000);
for($i=0;$i<$length;$i++)
{
$hash.=$chars[mt_rand(0,$max)];
}
return$hash;
}
$a=strtolower(fileext($_FILES['filename']['name']));
//判斷文件類型
if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type))
{
$text=implode(",",$type);
$ret_code=3;//文件類型錯誤
$page_result=$text;
$retArray=array('ret_code'=>$ret_code,'page_result'=>$page_result);
$retJson=json_encode($retArray);
echo$retJson;
return;
}
//生成目標文件的文件名
else
{
$filename=explode(".",$_FILES['filename']['name']);
do
{
$filename[0]=random(10);//設置隨機數長度
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if(move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['filename']['tmp_name']))
{
$ret_code=1;//上傳失敗
}
else
{//上傳成功
$ret_code=0;
}
}
$retArray=array('ret_code'=>$ret_code);
$retJson=json_encode($retArray);
echo$retJson;
}
//壓縮圖片
$uploaddir_resize="upfiles_resize/";
$uploadfile_resize=$uploaddir_resize.$name;
//$pic_width_max=120;
//$pic_height_max=90;
//以上與下面段注釋可以聯合使用,可以使圖片根據計算出來的比例壓縮
$file_type=$_FILES["filename"]['type'];
functionResizeImage($uploadfile,$maxwidth,$maxheight,$name)
{
//取得當前圖片大小
$width=imagesx($uploadfile);
$height=imagesy($uploadfile);
$i=0.5;
//生成縮略圖的大小
if(($width>$maxwidth)||($height>$maxheight))
{
/*
$widthratio=$maxwidth/$width;
$heightratio=$maxheight/$height;
if($widthratio<$heightratio)
{
$ratio=$widthratio;
}
else
{
$ratio=$heightratio;
}
$newwidth=$width*$ratio;
$newheight=$height*$ratio;
*/
$newwidth=$width*$i;
$newheight=$height*$i;
if(function_exists("imageresampled"))
{
$uploaddir_resize=imagecreatetruecolor($newwidth,$newheight);
imageresampled($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}
else
{
$uploaddir_resize=imagecreate($newwidth,$newheight);
imageresized($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}
ImageJpeg($uploaddir_resize,$name);
ImageDestroy($uploaddir_resize);
}
else
{
ImageJpeg($uploadfile,$name);
}
}if($_FILES["filename"]['size'])
{
if($file_type=="image/pjpeg"||$file_type=="image/jpg"|$file_type=="image/jpeg")
{
//$im=imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/x-png")
{
//$im=imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/gif")
{
//$im=imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
else//默認jpg
{
$im=imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);
ImageDestroy($im);
}
}
?>
請按照現實情況更改connect.php,test_upload.php中對應的信息。
望採納,謝謝。
❷ php如何實時縮小圖片大小
PHP中縮放圖像:
有兩種改變圖像大小的方法.
(1):ImageCopyResized() 函數在所有GD版本中有效,但其縮放圖像的演算法比較粗糙.
(2):ImageCopyResampled(),其像素插值演算法得到的圖像邊緣比較平滑.質量較好(但該函數的速度比
ImageCopyResized() 慢).
兩個函數的參數是一樣的.如下:
ImageCopyResampled(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
ImageCopyResized(dest,src,dx,dy,sx,sy,dw,dh,sw,sh);
它們兩個都是從原圖像(source)中抓取特定位置(sx,sy)復制圖像qu區域到目標t
圖像(destination)的特定位置(dx,dy)。另外dw,dh指定復制的圖像區域在目標圖像上的大小,sw,sh指定從原圖像復制的圖像區域
的大小。如果有ps經驗的話,就相當於在原圖像選擇一塊區域,剪切移動到目的圖像上,同時有拉伸或縮小的操作。
例一:
(本例子是將圖片按原大小的4/1的大小顯示)
<?php
// 指定文件路徑和縮放比例
$filename = 'test.jpg';
$percent = 0.5;
// 指定頭文件Content type值
header('Content-type: image/jpeg');
// 獲取圖片的寬高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 創建一個圖片。接收參數分別為寬高,返回生成的資源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//獲取源文件資源句柄。接收參數為圖片路徑,返回句柄
$source = imagecreatefromjpeg($filename);
// 將源文件剪切全部域並縮小放到目標圖片上。前兩個為資源句柄
imageresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// 輸出給瀏覽器
imagejpeg($thumb);
?>
❸ php圖片上傳能用代碼壓縮圖片文件的大小嗎
圖片的格式是多變的,但是壓縮圖片的方式不變,壓縮軟體壓縮圖片一致都是那樣,我將操作步驟寫下來了,樓主可以看看
1、安裝相對應的輔助工具(迅捷圖片壓縮軟體)運行工具;
2、打開工具,看到頁面上的圖片壓縮選項,點擊這個藍色的按鈕進入將要實行操作的頁面。
3、在頁面上點擊選擇文件按鈕,或是選擇文件夾按鈕,都可以將存放圖片文件的文件夾打開,然後對圖片進行選擇。
4、選擇文件時我們按住多選鍵Ctrl,選擇我們需要壓縮的圖片添加到頁面中間的位置。
5、做到這一步了,下面我們可以對壓縮圖片的壓縮選項做一個選擇,可以轉換圖片的格式,轉化為png或者是jpg,將圖片壓縮可以選擇的壓縮選項如下。
6、將所有的參數設置完成之後我們點擊頁面上的「開始壓縮按鈕就可以進行壓縮了。
日常使用的壓縮圖片的辦法是將圖片壓縮為壓縮包,在使用是還要對其解壓才能使用,這種壓縮方法壓縮圖片不同點在於不會將圖片文件壓縮為壓縮包,能將圖片最大限度的縮小,圖片的狀態不會改變。
❹ PHP 怎麼樣把一張圖片縮小到指定大小
如果是改變顯示的大小,直接img標簽屬性里,width和height設置啊。
如果想真正改變,你看看這個代碼(沒試驗過):
function makeThumb($srcFile,$dstFile,$dstW,$dstH) {
$data=GetImageSize($srcFile,&$info);
switch (CoreUtil::getFileExtension($dstFile)){
case'gif':
$im= @ImageCreateFromGIF($srcFile); break;
case'jpg':
case'jpeg':
$im= @imagecreatefromjpeg($srcFile); break;
case'png':
$im= @ImageCreateFromPNG($srcFile); break;
default:returnFalse;
}
if(!$im) returnFalse;
$srcW=ImageSX($im);
$srcH=ImageSY($im);
$dstX=0;
$dstY=0;
if ($srcW*$dstH>$srcH*$dstW){
$fdstH=round($srcH*$dstW/$srcW);
$dstY=floor(($dstH-$fdstH)/2); $fdstW=$dstW;
} else {
$fdstW=round($srcW*$dstH/$srcH); $dstX=floor(($dstW-$fdstW)/2);
$fdstH=$dstH;
}
$ni=ImageCreate($dstW,$dstH);
$dstX=($dstX<0)?0:$dstX;
$dstY=($dstX<0)?0:$dstY;
$dstX=($dstX>($dstW/2))?floor($dstW/2):$dstX;
$dstY=($dstY>($dstH/2))?floor($dstH/s):$dstY;
$black= ImageColorAllocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstW,$dstH,$black);
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
ImageJpeg($ni,$dstFile);
imagedestroy($im);
imagedestroy($ni);
returnTrue;
}
大概就是用到imagecreatefromjpeg、imagecreatetruecolor、imageresampled 、 imagepng這幾個函數
❺ php圖片可以等比例的縮放嗎
可以。
等比例縮放的方法是:
1、載入選區--自由變換。如下圖:
2、按住shift+alt鍵,使用滑鼠調整大小,這種情況下,選區會按照等比例的方法進行縮放的。
❻ PHP不改變圖片尺寸,壓縮png圖片體積變大,怎麼辦
壓縮文件要加頭部文件,頭部文件比壓縮的部分大
❼ 求php圖片縮放處理函數
<?php
/**
*圖片縮放
*@paramstring$url
*@paramint$maxWidth
*@paramint$maxHeight
*@returnstring
*/
functionthumb($url,$maxWidth,$maxHeight,&$info){
$info=$imgInfo=getimagesize($url);
$width=$imgInfo[0];//獲取圖片寬度
$height=$imgInfo[1];//獲取圖片高度
$r=min($maxHeight/$height,$maxWidth/$width);
if($r>=1){//不用縮放
$maxHeight=$height;
$maxWidth=$width;
}elseif($r<1){//縮放
$maxHeight=$height*$r;
$maxWidth=$width*$r;
}
$temp_img=imagecreatetruecolor($maxWidth,$maxHeight);//創建畫布
$fun=str_replace('/','createfrom',$imgInfo['mime']);
$im=$fun($url);
imageresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
ob_start();
$fun=str_replace('/','',$imgInfo['mime']);
$fun($temp_img);
$imgstr=ob_get_contents();
ob_end_clean();
imagedestroy($im);
return$imgstr;
}
$imgUrl=$_GET['url'];
$info=array();
$string=thumb($imgUrl,500,500,$info);
$mimeArray=explode("/",$info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo$string;
以上代碼存為thumb.php,調用效果:
❽ php 圖片壓縮顯示
(1)網頁結構里用:<img src="image.php?name=p01.png">,來調用處理後的圖片信息。
(2)在後台腳本 image.php 里對傳過來的圖片名進行處理返回:
<?php
$pic = $_REQUEST['name'];
// 1.打開圖片源文件資源
$im = @imagecreatefrompng($pic);
if ($im) {
// 2.源文件的寬高,也可寫為定值
$fx = imagesx($im); // 取寬
$fy = imagesy($im); // 取高
// 3.使用新的寬高
$sx = 150;
$sy = 100;
// 4.生成小圖資源
$sm = imagecreatetruecolor($sx,$sy);
// 5.進行縮放
imageresampled($sm,$im,0,0,0,0,$sx,$sy,$fx,$fy);
// 6.輸出圖像
header('Content-Type: image/png');
imagepng($sm);
// 7.釋放資源
imagedestroy($sm);
imagedestroy($im);
}
(3)代碼里假設是對 png 圖片處理,相關字都是 png,如果想對 jpg 類型處理的可都換成 jpeg
❾ PHP等比例壓縮圖片的實例代碼
具體代碼如下所示:
/**
*
desription
壓縮圖片
*
@param
sting
$imgsrc
圖片路徑
*
@param
string
$imgdst
壓縮後保存路徑
*/
public
function
compressedImage($imgsrc,
$imgdst)
{
list($width,
$height,
$type)
=
getimagesize($imgsrc);
$new_width
=
$width;//壓縮後的圖片寬
$new_height
=
$height;//壓縮後的圖片高
if($width
>=
600){
$per
=
600
/
$width;//計算比例
$new_width
=
$width
*
$per;
$new_height
=
$height
*
$per;
}
switch
($type)
{
case
1:
$giftype
=
check_gifcartoon($imgsrc);
if
($giftype)
{
header('Content-Type:image/gif');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromgif($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
}
break;
case
2:
header('Content-Type:image/jpeg');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromjpeg($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
case
3:
header('Content-Type:image/png');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefrompng($imgsrc);
imageresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}
總結
以上所述是小編給大家介紹的PHP等比例壓縮圖片的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
您可能感興趣的文章:php中10個不同等級壓縮優化圖片操作示例PHP
實現等比壓縮圖片尺寸和大小實例代碼php
gd等比例縮放壓縮圖片函數基於PHP實現等比壓縮圖片大小php上傳圖片並壓縮的實現方法PHP實現圖片上傳並壓縮PHP實現圖片壓縮的兩則實例php使用imagick模塊實現圖片縮放、裁剪、壓縮示例
❿ PHP 如何縮小上傳的PNG和GIF圖片
PNG圖片用的是imagecreatefrompng。可以處理前先判斷圖片的格式。部分代碼如下:
$ground_info=getimagesize($groundImage);
switch($ground_info[2]){ //取得背景圖片的格式
case 1:$ground_im=imagecreatefromgif($groundImage);break;
case 2:$ground_im=imagecreatefromjpeg($groundImage);break;
case 3:$ground_im=imagecreatefrompng($groundImage);break;
default:return false; //未知的文件格式
}