當前位置:首頁 » 編程語言 » php圖片縮放比例縮放

php圖片縮放比例縮放

發布時間: 2022-06-11 16:50:05

⑴ 如何縮小圖片解析度 本人有若干張圖片要傳到網上,想把圖片解析度調小一些有什麼軟體支持批量轉換

批量修改圖片解析度的方法:

步驟1,安裝工具軟體後打開使用,點擊軟體左邊五個功能中的【更改尺寸】功能,然後再點擊【添加文件】按鈕將需要修改解析度的圖片添加到軟體中。

php 如何將上傳的圖片按比例縮放並存在伺服器里

//接收上傳的文件
foreach($_FILESas$file)
{
$tempFileName=$file['tmp_name'];//上傳文件的臨時路徑
}
/把圖片移動到伺服器制定路徑
$img='/var/www/html/picture/test.jpg';
move_uploaded_file($tempFileName,$img);

//縮放比例
$ratio=0.5;

//修改尺寸至於各個函數是幹嘛的,google一下吧
$imagedata=getimagesize($img);
$olgWidth=$imagedata[0];
$oldHeight=$imagedata[1];
$newWidth=$olgWidth*$ratio;
$newHeight=$oldHeight*$ratio;

$image=imagecreatefromjpeg($img);
$thumb=imagecreatetruecolor($newWidth,$newHeight);
imageresized($thumb,$image,0,0,0,0,$newWidth,$newHeight,$olgWidth,$oldHeight);
imagejpeg($thumb,$img);

imagedestroy($thumb);
imagedestroy($image);

⑶ php等比例縮放圖片原理

這里的$width主要是在$width沒有設置的時候直接用$height進行處理(else),在這里意義不大。
if判斷除了0/false/null(可能點漏)幾個,其他數值、包括負數,都為真。
你的if/else中的核心是誰大按誰的比例進行縮放。

⑷ 如何實現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);
}

}

?>

⑸ 如何實現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);

⑹ 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語言能夠有所幫助。

⑺ 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');

寬高自己設定 就可以自動等比例縮放了

⑻ 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模塊實現圖片縮放、裁剪、壓縮示例

⑼ 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);
?>

⑽ 跪求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);
}

這個是代碼和所引用的

熱點內容
瀏覽器打不開伺服器通信怎麼辦 發布:2024-05-18 21:32:22 瀏覽:961
創建存儲空間 發布:2024-05-18 21:20:57 瀏覽:122
sql日期和時間 發布:2024-05-18 21:16:19 瀏覽:143
安卓網頁怎麼截取 發布:2024-05-18 20:53:56 瀏覽:972
在配置更新的時候沒電關機怎麼辦 發布:2024-05-18 20:36:10 瀏覽:928
win7訪問win2000 發布:2024-05-18 20:27:41 瀏覽:389
青島人社局密碼多少 發布:2024-05-18 20:19:10 瀏覽:735
無法存儲呼叫轉移 發布:2024-05-18 20:18:30 瀏覽:128
資料庫的調優 發布:2024-05-18 20:18:29 瀏覽:347
sqlserver注冊表清理 發布:2024-05-18 20:13:14 瀏覽:993