比如(什麼?)上面有不同大小的圖片--------語文表達缺主語,含糊導致無法理解。
伺服器上面?客戶機上面?具體什麼軟體環境上面?
「有不同大小的圖片」,已經存在的圖片通常已經壓縮過的,像JPEG更是有損壓縮。再次壓縮必定再次會降低畫質。PHP可以再次處理圖片,但畫質和存儲大小不能兼得,畫質好就存儲大,要存儲小就畫質差,根據自己的畫質需求處理。
② php將pdf文件格式轉換成圖片,並壓縮
有一份pdf文件,需要將其轉換成圖片, 如果圖片過大,同時還需要將其壓縮。
1、安裝插件
因為不同版本的用法略有區別,我這里用的是2.1版, 最近文檔還需要看官方文檔。
2、簡單使用
3、常用方法
4、其他
1、說明
2、安裝
不同版本的使用略有區別,我這用的是2.5版本的
3、簡單使用
其中resize,可以指定壓縮的寬度和高度,如
如果是指定寬度,智適應高度就是這樣
save的三個參數是,
4、更多
更多使用,看 說明文檔
壓縮圖片的時候,報不能讀取問題
這個可能是遇到最多的問題。可能原因如下:
1、文件讀取許可權
查看文件的許可權,看是否有讀取的許可權(r), 沒有的話直接把文件改為 777
2、插件不支持該格式文件
輸入 php --ri imagick , 在支持列表看是否支持該文件的格式。沒有的話,自己網路啦。
3、內存或緩存不夠
進入插件的 /vendor/intervention/image/src/Intervention/Image/Imagick/Decoder.php , 在24行斷點
可能會得到消息:
然後,在網路下,原來是壓縮的文件過大,插件使用的緩存不夠,這里直接將配置改大即可
將配置文件改成如下
③ 如何實現PHP圖片裁剪與縮放
給你段代碼吧。上邊的是具體的事務處理。下面的是類文件。
////////////////////////////////////////////////////下面開始處理圖片壓縮問題
$src = "$fileurl";
echo $src;
$image = new Image($src);
$width= $image->getimgwidth();
echo $width."寬度是這些";
if($width>1024){
$coefficient=number_format(1024/$width, 4, '.', '');
echo $coefficient;
$image->percent = $coefficient;
$image->openImage();
$image->thumpImage();
//************************************重新給圖片命名
$randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;
echo "<br/>重新命名是這個".$randname;
$fileurl=$fileimgweb.$randname;//重新給資料庫里存的圖片地址命名
// $image->showImage();
$image->saveImage($fileurl);
}
////////////////////////////////////////////////////圖片壓縮問題處理結束
--------------------------------------------------------------------------------------
<?php
/**
圖片壓縮操作類
v1.0
*/
class Image{
private $src;
private $imageinfo;
private $image;
public $percent = 0.5;
public function __construct($src){
$this->src = $src;
}
/**
獲取圖片的寬度並傳輸給前台
*/
public function getimgwidth(){
$imgwidth= getimagesize($this->src)[0];
// echo $imgwidth;
return $imgwidth;
}
/**
打開圖片
*/
public function openImage(){
list($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(
'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
);
$fun = "imagecreatefrom".$this->imageinfo['type'];
$this->image = $fun($this->src);
}
/**
操作圖片
*/
public function thumpImage(){
$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width,$new_height);
//將原圖復制帶圖片載體上面,並且按照一定比例壓縮,極大的保持了清晰度
imageresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
}
/**
輸出圖片
*/
public function showImage(){
header('Content-Type: image/'.$this->imageinfo['type']);
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image);
}
/**
保存圖片到硬碟
*/
public function saveImage($fileurl){
imagejpeg($this->image, $fileurl,75);
// $funcs = "image".$this->imageinfo['type'];
// $funcs($this->image,$name.'.'.$this->imageinfo['type']);
}
/**
銷毀圖片
*/
public function destruct(){
imagedestroy($this->image);
}
}
?>
④ 如何利用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能壓縮網路上的圖片嗎
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等比例壓縮圖片的實例代碼
具體代碼如下所示:
/**
*
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 怎麼壓縮圖片後 在發給前端
<?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
/**
*圖片縮放
*@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上傳圖片壓縮
html里加一個表單,然後提交到處理上傳文件的upload.php
類似下面這樣
<fromaction="upload.php"enctype="multipart/from-data"method="post">
<inputtype="file"name="file3"/>
</from>
然後upload.php里先處理圖片上傳,最終保存一份原始圖片文件在伺服器上,接著就把文件路徑什麼的傳到你的這個函數里處理圖片就OK了