php圖片縮放比例
1. php圖片可以等比例的縮放嗎
可以。
等比例縮放的方法是:
1、載入選區--自由變換。如下圖:
2、按住shift+alt鍵,使用滑鼠調整大小,這種情況下,選區會按照等比例的方法進行縮放的。
2. 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 type值
header('Content-type: image/jpeg');
// 獲取圖片的寬高
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// 創建一個圖片。接收參數分別為寬高,返回生成的資源句柄
$thumb = imagecreatetruecolor($newwidth, $newheight);
//獲取源文件資源句柄。接收參數為圖片路徑,返回句柄
$source = imagecreatefromjpeg($filename);
// 將源文件剪切全部域並縮小放到目標圖片上。前兩個為資源句柄
imageresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
$newheight, $width, $height);
// 輸出給瀏覽器
imagejpeg($thumb);
?>
3. 如何實現PHP圖片裁剪與縮放
給你段代碼吧。上邊的是具體的事務處理。下面的是類文件。
////////////////////////////////////////////////////下面開始處理圖片壓縮問題
$src = "$fileurl";
echo $src;
$image = new Image($src);
$width= $image->getimgwidth();
echo $width."寬度是這些";
if($width>1024){
$coefficient=number_format(1024/$width, 4, '.', '');
echo $coefficient;
$image->percent = $coefficient;
$image->openImage();
$image->thumpImage();
//************************************重新給圖片命名
$randname=date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$hz;
echo "<br/>重新命名是這個".$randname;
$fileurl=$fileimgweb.$randname;//重新給資料庫里存的圖片地址命名
// $image->showImage();
$image->saveImage($fileurl);
}
////////////////////////////////////////////////////圖片壓縮問題處理結束
--------------------------------------------------------------------------------------
<?php
/**
圖片壓縮操作類
v1.0
*/
class Image{
private $src;
private $imageinfo;
private $image;
public $percent = 0.5;
public function __construct($src){
$this->src = $src;
}
/**
獲取圖片的寬度並傳輸給前台
*/
public function getimgwidth(){
$imgwidth= getimagesize($this->src)[0];
// echo $imgwidth;
return $imgwidth;
}
/**
打開圖片
*/
public function openImage(){
list($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(
'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
);
$fun = "imagecreatefrom".$this->imageinfo['type'];
$this->image = $fun($this->src);
}
/**
操作圖片
*/
public function thumpImage(){
$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width,$new_height);
//將原圖復制帶圖片載體上面,並且按照一定比例壓縮,極大的保持了清晰度
imageresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
}
/**
輸出圖片
*/
public function showImage(){
header('Content-Type: image/'.$this->imageinfo['type']);
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image);
}
/**
保存圖片到硬碟
*/
public function saveImage($fileurl){
imagejpeg($this->image, $fileurl,75);
// $funcs = "image".$this->imageinfo['type'];
// $funcs($this->image,$name.'.'.$this->imageinfo['type']);
}
/**
銷毀圖片
*/
public function destruct(){
imagedestroy($this->image);
}
}
?>
4. 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);
//拷貝部分圖像並調整
imageresized($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語言能夠有所幫助。
5. HTML PHP 網頁如何設定圖片寬度超過700px 則自動縮小
第一個方法:
1、用css來實現IE7以上瀏覽器的圖片縮略效果。
.divimg{ max-width:50px; max-height:50px; } *html.divimg{width:expression(this.width>50&&this.width>this.height?50:auto); height:expresion(this.height>50?50:auto); }
說明: 這段代碼是把圖片等比例縮小為50px * 50px,可以根據網頁的寬度來自由修改尺寸。
2、萬惡的IE6不識別上面的代碼怎麼辦?
如果你的網頁寬度為580像素,我們來看
.ltop3 { line-height: 25px; font-size: 12px; overflow: hidden; width: 580px;}
這樣的話,IE7會識別第一步的代碼來自動縮略,而IE6會根據第二步的代碼,如果圖片內容超過網頁寬度就自動隱藏,這就完美的解決了圖片的縮略問題。
另外還有一個方法也告訴大家,希望有用。
第2個方法:
JS代碼加到網頁<BODY>前面
<SCRIPT language=javaScript type=text/JavaScript>
//改變圖片大小
function resizepic(thispic)
{
if(thispic.width>400) thispic.width=400;
}
//無級縮放圖片大小
function bbimg(o)
{
var zoom=parseInt(o.style.zoom, 10)||100;
zoom+=event.wheelDelta/12;
if (zoom>0) o.style.zoom=zoom+'%';
return false;
}
</SCRIPT>
在圖片屬性加上onmousewheel="return bbimg(this)" onload="javascript:resizepic(this)" 代碼即可讓超過400像素的圖片自動縮略。
6. php等比縮放圖片,就是只按寬度縮小圖片,當圖片寬度大於750時就縮小到750 高度不用管 跟著寬度縮就行了
首先說一下思路,首先你要判斷圖片的寬度,這需要用到一個函數,個人比較喜歡用getimagesize()
其次是等比例綻放,需要用到imageresized(當然還有其他函數)
注意:我這里用到的是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); //創建一個縮放的畫布
imageresized($des_im,$im,0,0,0,0,$maxWidth,$maxHeight,$info[0],$info[1]); //縮放
imagejpeg($des_im,'thumb.jpg'); //輸出到thumb.jpg即為一個縮放後的文件
7. 跪求PHP上傳圖片並按比例縮放到一定尺寸的程序,非常感謝
publicfunctionupimges(){
$filename=time();
header("Content-Type:text/html;charset=utf-8");
$upload=newUpimage('','','','',$filename);
//設置上傳文件大小
$upload->maxSize=100000000000000;
//$upload->saveRule=microtime().'_'.mt_rand(1000,9999);
//上傳文件命名規則timeuniqidcom_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;
}
echojson_encode($data);
}
這個是代碼和所引用的
8. 如何實現php圖片等比例縮放
$imgsrc=$image_name;
$imgdst=$image_name;
list($width,$height,$type)=getimagesize($imgsrc);
$new_width=($width>600?600:$width)*0.9;
$new_height=($height>600?600:$height)*0.9;
$giftype=check_gifcartoon($imgsrc);
$image_wp=imagecreatetruecolor($new_width,$new_height);
$image=imagecreatefromgif($imgsrc);
imageresampled($image_wp,$image,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($image_wp,$imgdst,75);
imagedestroy($image_wp);
9. 求php圖片縮放處理函數
<?php
/**
*圖片縮放
*@paramstring$url
*@paramint$maxWidth
*@paramint$maxHeight
*@returnstring
*/
functionthumb($url,$maxWidth,$maxHeight,&$info){
$info=$imgInfo=getimagesize($url);
$width=$imgInfo[0];//獲取圖片寬度
$height=$imgInfo[1];//獲取圖片高度
$r=min($maxHeight/$height,$maxWidth/$width);
if($r>=1){//不用縮放
$maxHeight=$height;
$maxWidth=$width;
}elseif($r<1){//縮放
$maxHeight=$height*$r;
$maxWidth=$width*$r;
}
$temp_img=imagecreatetruecolor($maxWidth,$maxHeight);//創建畫布
$fun=str_replace('/','createfrom',$imgInfo['mime']);
$im=$fun($url);
imageresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
ob_start();
$fun=str_replace('/','',$imgInfo['mime']);
$fun($temp_img);
$imgstr=ob_get_contents();
ob_end_clean();
imagedestroy($im);
return$imgstr;
}
$imgUrl=$_GET['url'];
$info=array();
$string=thumb($imgUrl,500,500,$info);
$mimeArray=explode("/",$info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo$string;
以上代碼存為thumb.php,調用效果:
10. php 怎麼實現不等比例縮放圖片
functiontep_image($src,$alt='',$width='',$height='',$parameters=''){
$image='<imgsrc="'.tep_output_string($src).'"alt="'.$alt.'"';
if(tep_not_null($alt)){
$image.='title="'.$alt.'"';
}
if(((empty($width)||empty($height))){
if($image_size=@getimagesize($src)){
if(empty($width)&&tep_not_null($height)){
$ratio=$height/$image_size[1];
$width=intval($image_size[0]*$ratio);
}elseif(tep_not_null($width)&&empty($height)){
$ratio=$width/$image_size[0];
$height=intval($image_size[1]*$ratio);
}elseif(empty($width)&&empty($height)){
$width=$image_size[0];
$height=$image_size[1];
}
}
}
if(tep_not_null($width)&&tep_not_null($height)){
$image.='width="'.$width.'"height="'.$height.'"';
}
if(tep_not_null($parameters))$image.=''.$parameters;
$image.='/>';
return$image;
}
functiontep_not_null($value){
if(is_array($value)){
if(sizeof($value)>0){
returntrue;
}else{
returnfalse;
}
}else{
if(($value!='')&&(strtolower($value)!='null')&&(strlen(trim($value))>0)){
returntrue;
}else{
returnfalse;
}
}
}
用tep_image('圖片路徑','圖片alt','width','height');
寬高自己設定 就可以自動等比例縮放了