本文目錄一覽:
php按寬度縮放圖片?
我不明白你的意思,如果你是想比如用戶上傳了一個圖片,然後你用php自動等比縮小圖片,然後將縮小後的圖片再保存到服務器上,我不曉得怎麼做,因為我不會用php。
不過如果僅僅是調用img 然後讓img等比縮小在頁面中顯示,為什麼不能用CSS+js
css{
max-height:100px;
max-width:100px; /*圖片等比例縮小,最大寬度和高度都是100px,可以自己改變,原圖沒有超過設置的大小,輸出就是原圖大小*/
}
IE6不支持MAX屬性
然後再加一段js去實現在IE6下的img等比縮放大小,網上有源代碼。
如何實現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);
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上傳圖片並按比例縮放到一定尺寸的程序,非常感謝
public function upimges(){
$filename = time();
header(“Content-Type:text/html;charset=utf-8”);
$upload = new Upimage(”,”,”,”,$filename);
//設置上傳文件大小
$upload-maxSize = 100000000000000 ;
// $upload-saveRule =microtime().’_’.mt_rand(1000,9999);
//上傳文件命名規則time uniqid com_create_guid 等
// $upload-$saveRule = time() ;
//設置上傳文件類型
$upload-allowExts = explode(‘,’,’jpg,gif,png,jpeg’);
//設置附件上傳目錄
$upload-savePath = ‘./data/attached/skp/’.$_SESSION[‘formtoken’].’/’;
$upload-thumb = true;
// 設置引用圖片類庫包路徑
$upload-imageClassPath = ‘./Image.php’;
//設置需要生成縮略圖的文件後綴
$upload-thumbPrefix = ‘thumb_,thumbm_,thumbl_,thumbs_’;//生產4張縮略圖
//設置縮略圖最大寬度
$upload-thumbMaxWidth = ‘960,480,120,64’;
//設置縮略圖最大高度
$upload-thumbMaxHeight = ‘960,480,120,64’;
//刪除原圖
$upload-thumbRemoveOrigin = false;
if(!$upload-upload()) {
$data[‘tip’] = $upload-getErrorMsg();
$data[‘status’] = ‘0’;
}else{
$info = $upload-getUploadFileInfo();
$data[‘counts’] = count($info);
$data[‘status’] = ‘1’;
$data[‘tip’] = ‘上傳成功’;
$data[‘info’] = $info;
}
echo json_encode($data);
}
這個是代碼和所引用的
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/282576.html