本文目錄一覽:
- 1、隨便下載一張圖片,使用PHP將圖片縮放到原來的1/2
- 2、php如何實時縮小圖片大小
- 3、php 怎麼壓縮圖片的大小
- 4、php等比例壓縮圖片
- 5、PHP等比例壓縮圖片的實例代碼
- 6、php等比縮放圖片,就是只按寬度縮小圖片,當圖片寬度大於750時就縮小到750 高度不用管 跟着寬度縮就行了
隨便下載一張圖片,使用PHP將圖片縮放到原來的1/2
//如果是JPG格式,則生成一個同比例的縮小圖
$file=”xxx.jpg”;
$extend_name=strtolower(substr(strrchr($file,”.”),1));
if($extend_name==’jpg’){
$image = imagecreatefromjpeg($full_name);//取原圖的數據
}
//如果是gif格式,則生成一個同比例的縮小圖
if($extend_name==’gif’){
$image = imagecreatefromgif($full_name);//取原圖的數據
}
//如果是png格式,則生成一個同比例的縮小圖
if($extend_name==’png’){
$image = imagecreatefrompng($full_name);//取原圖的數據
}
//echo $full_name.$full_name_small;
$size=GetImageSize($full_name);
$x=$size[0];
$y=$size[1];
//echo $x.” _ “.$y;
//假設首頁上的圖都是250象素左右,如果縮成150則圖像失真太嚴重,則把所有的圖都按這個大小進行等比縮放
//計算縮小比例
$rate=1/2;
$small_x=$size[0]*$rate;
$small_y=$size[1]*$rate;
$small_image = imagecreatetruecolor($small_x, $small_y);
imageCopyResampled($small_image,$image,0,0,0,0,$small_x,$small_y,$x,$y);
if(imagejpeg($small_image,$full_name_small)){
ImageDestroy($small_image);
}else{
}
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 typezhi值
header(‘Content-type: image/jpeg’);
// 獲取圖片的寬高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 創建一個圖片。接收參數分別為寬高,返回生成的資源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//獲取源文件資源句柄。接收參數為圖片路徑,返回句柄
$source = imagecreatefromjpeg($filename);
// 將源文件剪切全部域並縮小放到目標圖片上。前兩個為資源句柄
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// 輸出給瀏覽器
imagejpeg($thumb);
?
php 怎麼壓縮圖片的大小
php 壓縮圖片的大小:
?php
$im = imagecreatefromjpeg(‘D:phpplace.jpeg’);
resizeImage($im,,,’xinde’,’.jpg’);
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);
echo “start—————–” ;
if(($maxwidth $pic_width $maxwidth) ($maxheight $pic_height $maxheight))
{
if($maxwidth $pic_width$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}
if($maxheight $pic_height$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}
if($resizewidth_tag $resizeheight_tag)
{
if($widthratio$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if($resizewidth_tag !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if(function_exists(“imagecopyresampled”))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
php等比例壓縮圖片
等比壓縮的知道比例吧?。所以需要根據比例設置$dst_w 和$dst_h
比如設置比例為2:1,代碼就應該這樣設置
$dst_w = $src_w/2
$dst_h = $src_h/2
這樣就行了
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等比縮放圖片,就是只按寬度縮小圖片,當圖片寬度大於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即為一個縮放後的文件
原創文章,作者:TGAY,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/145440.html