當前位置:首頁 » 文件管理 » php圖片壓縮

php圖片壓縮

發布時間: 2022-02-11 21:07:25

php壓縮網路上的圖片嗎

PHP圖片上傳並壓縮的實現方法具體內容如下使用到三個文件
connect.php:連接資料庫
test_upload.php:執行sql語句
upload_img.php:上傳圖片並壓縮
三個文件代碼如下:
連接資料庫:connect.php
<?php
$db_host = '';
$db_user = '';
$db_psw = '';
$db_name = '';
$db_port = '';
$sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);$q="set names utf8;";
$result=$sqlconn->query($q);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", 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 = "insert into img (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");//獲取文件後綴名函數
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);}
//生成隨機文件名函數
function random($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'];
function ResizeImage($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等比例壓縮圖片

等比壓縮的知道比例吧?。所以需要根據比例設置$dst_w 和$dst_h
比如設置比例為2:1,代碼就應該這樣設置

$dst_w = $src_w/2
$dst_h = $src_h/2
這樣就行了

⑶ php 怎麼壓縮圖片後 在發給前端

<?php
/*
----------------------------------------------------------------------
函數:調整圖片尺寸或生成縮略圖
返回:True/False
參數:
$Image需要調整的圖片(含路徑)
$Dw=450調整時最大寬度;縮略圖時的絕對寬度
$Dh=450調整時最大高度;縮略圖時的絕對高度
$Type=11,調整尺寸;2,生成縮略圖
$path='img/';//路徑
$phtypes=array(
'img/gif',
'img/jpg',
'img/jpeg',
'img/bmp',
'img/pjpeg',
'img/x-png'
);
FunctionImg($Image,$Dw=450,$Dh=450,$Type=1){
IF(!File_Exists($Image)){
ReturnFalse;
}
//如果需要生成縮略圖,則將原圖拷貝一下重新給$Image賦值
IF($Type!=1){
Copy($Image,Str_Replace(".","_x.",$Image));
$Image=Str_Replace(".","_x.",$Image);
}
//取得文件的類型,根據不同的類型建立不同的對象
$ImgInfo=GetImageSize($Image);
Switch($ImgInfo[2]){
Case1:
$Img=@ImageCreateFromGIF($Image);
Break;
Case2:
$Img=@ImageCreateFromJPEG($Image);
Break;
Case3:
$Img=@ImageCreateFromPNG($Image);
Break;
}
//如果對象沒有創建成功,則說明非圖片文件
IF(Empty($Img)){
//如果是生成縮略圖的時候出錯,則需要刪掉已經復制的文件
IF($Type!=1){Unlink($Image);}
ReturnFalse;
}
//如果是執行調整尺寸操作則
IF($Type==1){
$w=ImagesX($Img);
$h=ImagesY($Img);
$width=$w;
$height=$h;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
IF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
}
}ElseIF($height>$Dh){
$Par=$Dh/$height;
$height=$Dh;
$width=$width*$Par;
IF($width>$Dw){
$Par=$Dw/$width;
$width=$Dw;
$height=$height*$Par;
}
}Else{
$width=$width;
$height=$height;
}
$nImg=ImageCreateTrueColor($width,$height);//新建一個真彩色畫布
ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);//重采樣拷貝部分圖像並調整大小
ImageJpeg($nImg,$Image);//以JPEG格式將圖像輸出到瀏覽器或文件
ReturnTrue;
//如果是執行生成縮略圖操作則
}Else{
$w=ImagesX($Img);
$h=ImagesY($Img);
$width=$w;
$height=$h;
$nImg=ImageCreateTrueColor($Dw,$Dh);
IF($h/$w>$Dh/$Dw){//高比較大
$width=$Dw;
$height=$h*$Dw/$w;
$IntNH=$height-$Dh;
ImageCopyReSampled($nImg,$Img,0,-$IntNH/1.8,0,0,$Dw,$height,$w,$h);
}Else{//寬比較大
$height=$Dh;
$width=$w*$Dh/$h;
$IntNW=$width-$Dw;
ImageCopyReSampled($nImg,$Img,-$IntNW/1.8,0,0,0,$width,$Dh,$w,$h);
}
ImageJpeg($nImg,$Image);
ReturnTrue;
}
}
?>
<html><body>
<formmethod="post"enctype="multipart/form-data"name="form1">
<table>
<tr><td>上傳圖片</td></tr>
<tr><td><inputtype="file"name="photo"size="20"/></td></tr>
<tr><td><inputtype="submit"value="上傳"/></td></tr>
</table>
允許上傳的文件類型為:<?=implode(',',$phtypes)?></form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!is_uploaded_file($_FILES["photo"][tmp_name])){
echo"圖片不存在";
exit();
}
if(!is_dir('img')){//路徑若不存在則創建
mkdir('img');
}
$upfile=$_FILES["photo"];
$pinfo=pathinfo($upfile["name"]);
$name=$pinfo['basename'];//文件名
$tmp_name=$upfile["tmp_name"];
$file_type=$pinfo['extension'];//獲得文件類型
$showphpath=$path.$name;

if(in_array($upfile["type"],$phtypes)){
echo"文件類型不符!";
exit();
}
if(move_uploaded_file($tmp_name,$path.$name)){
echo"成功!";
Img($showphpath,100,800,2);
}
echo"<imgsrc="".$showphpath.""/>";
}
?>
</body>
</html>

⑷ php 怎麼壓縮圖片的大小

好辦的,你把網站下載到本地,然後 用這個批量壓縮圖片的軟體就可以了

⑸ PHP不改變圖片尺寸,壓縮png圖片體積變大,怎麼辦

壓縮文件要加頭部文件,頭部文件比壓縮的部分大

⑹ 如何利用php把上傳的圖片壓縮

<?php
//Thefile
$filename='test.jpg';
$percent=0.5;

//Contenttype
header('Content-Type:image/jpeg');

//Getnewdimensions
list($width,$height)=getimagesize($filename);
$new_width=$width*$percent;
$new_height=$height*$percent;

//Resample
$image_p=imagecreatetruecolor($new_width,$new_height);
$image=imagecreatefromjpeg($filename);
imageresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);

//Output
imagejpeg($image_p,null,100);
?>

http://php.net/manual/en/function.imageresampled.php

⑺ php壓縮圖片大小到500k一下應該怎麼做啊

可以用光影啊,還有就是ps都可以了

⑻ php圖片上傳後被壓縮了,變得不清晰了,怎麼解決

如果清晰的上傳後,不清晰,,那就是設置的壓縮質量問題,,,如果是不清晰上傳了,這肯定不會清晰了。。。

找到上傳的設置里可以找到圖片壓縮比例。。。。

熱點內容
安卓的主播中心在哪裡 發布:2024-10-24 22:20:33 瀏覽:450
安卓軟體的文件夾在哪裡 發布:2024-10-24 22:19:38 瀏覽:55
txt去空格腳本 發布:2024-10-24 22:11:57 瀏覽:518
c語言怎麼調用文件 發布:2024-10-24 22:06:14 瀏覽:392
多雲平台演算法 發布:2024-10-24 22:04:57 瀏覽:719
台階圓編程 發布:2024-10-24 22:04:04 瀏覽:295
如何清理內置存儲 發布:2024-10-24 22:04:00 瀏覽:937
演算法規劃問題 發布:2024-10-24 21:57:56 瀏覽:728
數獨演算法java 發布:2024-10-24 21:55:37 瀏覽:797
培訓學校java排名 發布:2024-10-24 21:39:11 瀏覽:574