本文目錄一覽:
- 1、php等比縮放圖片,就是只按寬度縮小圖片,當圖片寬度大於750時就縮小到750 高度不用管 跟着寬度縮就行了
- 2、php實現圖片等比例縮放代碼
- 3、PHP等比例壓縮圖片的實例代碼
- 4、如何實現php圖片等比例縮放
php等比縮放圖片,就是只按寬度縮小圖片,當圖片寬度大於750時就縮小到750 高度不用管 跟着寬度縮就行了
首先說一下思路,首先你要判斷圖片的寬度,這需要用到一個函數,個人比較喜歡用getimagesize()
其次是等比例綻放,需要用到imagecopyresized(當然還有其他函數)
注意:我這裡用到的是gd庫
實現:
寫一個函數或者類都行,我這裡就以面向過程的方式來寫,你可以整理一下
$file = ‘pic.jpg’; //原圖片文件
$maxWidth = 750;
$info = getimagesize($file); //取得一個圖片信息的數組,索引 0 包含圖像寬度的像素值,索引 1 包含圖像高度的像素值。索引 2 是圖像類型的標記
if($info[0] $maxWidth )
{
exit(‘圖片小於’.$maxWidth.’,不需要縮放’);
}
$im = imagecreatefromjpeg($file); //根據圖片的格式對應的不同的函數,在此不多贅述。
$rate = $maxWidth/$info[0]; //計算綻放比例
$maxHeight = floor($info[1]*$rate); //計算出縮放後的高度
$des_im = imagecreatetruecolor($maxWidth,$maxHeight); //創建一個縮放的畫布
imagecopyresized($des_im,$im,0,0,0,0,$maxWidth,$maxHeight,$info[0],$info[1]); //縮放
imagejpeg($des_im,’thumb.jpg’); //輸出到thumb.jpg即為一個縮放後的文件
php實現圖片等比例縮放代碼
新建文件index.php,需要在統計目錄下有個圖片為q.jpg(可根據源碼進行更改圖片的名稱)
源代碼如下:
?php
$filename=”q.jpg”;
$per=0.3;
list($width,
$height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w,
$n_h);
$img=imagecreatefromjpeg($filename);
//拷貝部分圖像並調整
imagecopyresized($new,
$img,0,
0,0,
0,$n_w,
$n_h,
$width,
$height);
//圖像輸出新圖片、另存為
imagejpeg($new,
“q1.jpg”);
imagedestroy($new);
imagedestroy($img);
?
使用瀏覽器運行過後,在index.php同級的目錄下會有個q1.jpg,這個圖片就是等比例縮放後的圖片,路徑可以自己在源代碼裏面更改,放在自己的項目當中去或寫個方法也行
以上所述上就是本文的全部內容了,希望對大家學習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);
imagecopyresampled($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);
imagecopyresampled($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);
imagecopyresampled($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圖片等比例縮放
$imgsrc=$image_name;
$imgdst=$image_name;
list($width,$height,$type)=getimagesize($imgsrc);
$new_width = ($width600?600:$width)*0.9;
$new_height =($height600?600:$height)*0.9;
$giftype=check_gifcartoon($imgsrc);
$image_wp=imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromgif($imgsrc);
imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_wp, $imgdst,75);
imagedestroy($image_wp);
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/254815.html