本文目錄一覽:
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實現圖片等比例縮放代碼
新建文件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將圖片縮放到原來的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 壓縮圖片的大小:
?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);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/254022.html