php圖片比例縮放
A. 如何實現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);
B. 如何實現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);
}
}
?>
C. 關於php圖片縮放問題,比如一張400*300的圖片
1、html頁面不能對圖片有寬度和高度限制;
2、php進行縮放的話,你用的是GD?可以嘗試縮放的時候等比縮放:
前面getimagesize,imagesx,imagesy什麼的,我省略了,直接獲得當前的圖片的信息:
$size = array(
's' => array('width'=>$savewidth, 'height'=>$saveheight),
'o' => array('width'=>$width, 'height'=>$height),
);
//s代表當前圖片的寬高,
//o代表規則圖片的寬高,(就是你的200*200,超出就縮放的規則標准)
function parseimageresizerule($size = array()){
$extract = extract($size);
if($s['width'] >= $o['width'] || $s['height'] >= $o['height'
if($s['width'] >= $o['width']){
$radio['w'] = $o['width'] / $s['width'];
$state['w'] = true;
}
if($s['height'] >= $o['height']){
$radio['h'] = $o['height'] / $s['height'];
$state['h'] = true;
}
if($state['w'] && $state['h']){
if($radio['w'] < $radio['h']){
$radio['s'] = $radio['w'];
$radio['h'] = false;
}else{
$radio['s'] = $radio['h'];
$radio['w'] = false;
}
}elseif($state['w']){
$radio['s'] = $radio['w'];
}else{
$radio['s'] = $radio['h'];
}
$width = intval($s['width'] * $radio['s']);
$height = intval($s['height'] * $radio['s']);
$top = 0;
$left = 0;
}else{
$width = $s['width'];
$height = $s['height'];
$top = intval(($o['height'] - $height) / 2);
$left = intval(($o['width'] - $width) / 2);
}
return array(
'width' => $width,
'height' => $height,
'top' => $top,
'left' => $left,
);
}
最後返回的數組是實際圖片的長寬以及200,150這個圖片在200*200的圖片里的上左距離;
再用
imagefill($filesave, 0, 0, $white);
imageresampled($filesave, $filecache, $left, $top, 0, 0, $width, $height, $savewidth, $saveheight);
就得到1個等比縮放完成的圖片!明白?
代碼我手敲的,原理肯定可以的~ !
By ahonronline
D. 隨便下載一張圖片,使用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{
}
E. 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即為一個縮放後的文件
F. 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);
?>
G. php圖片可以等比例的縮放嗎
可以。
等比例縮放的方法是:
1、載入選區--自由變換。如下圖:
2、按住shift+alt鍵,使用滑鼠調整大小,這種情況下,選區會按照等比例的方法進行縮放的。
H. 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語言能夠有所幫助。
I. 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);
imageresampled($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);
imageresampled($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);
imageresampled($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模塊實現圖片縮放、裁剪、壓縮示例
J. 求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,調用效果: