本文目錄一覽:
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將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 圖片壓縮顯示
(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.進行縮放
imagecopyresampled($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等比例壓縮圖片的實例代碼
具體代碼如下所示:
/**
*
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模塊實現圖片縮放、裁剪、壓縮示例
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/127744.html