導航:首頁 > 編程語言 > php圖片裁剪縮放

php圖片裁剪縮放

發布時間:2022-12-16 07:14:12

『壹』 php圖像處理函數有哪些

php圖像處理函數大全

php圖片處理代碼分享,包括縮放、剪裁、縮放、翻轉、旋轉、透明、銳化等。需要的朋友可以參考下

一、創建圖片資源
imagecreatetruecolor(width,height);
imagecreatefromgif(圖片名稱);
imagecreatefrompng(圖片名稱);
imagecreatefromjpeg(圖片名稱);畫出各種圖像
imagegif(圖片資源,保存路徑);
imagepng()
imagejpeg();

二、獲取圖片屬性
imagesx(res//寬度
imagesy(res//高度
getimagesize(文件路徑)
返回一個具有四個單元的數組。索引
0 包含圖像寬度的像素值,索引 1 包含圖像高度的像素值。索引 2 是圖像類型的標記:1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 =
PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10
= JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM。這些標記與 PHP 4.3.0 新加的
IMAGETYPE 常量對應。索引 3 是文本字元串,內容為「height="yyy" width="xxx"」,可直接用於 IMG
標記。
銷毀圖像資源
imagedestroy(圖片資源);

三、透明處理
PNG、jpeg透明色都正常,只有gif不正常
imagecolortransparent(resource
image [,int
color])//將某個顏色設置成透明色
imagecolorstotal()
imagecolorforindex();

四、圖片的裁剪
imageresized()
imageresampled();

五、加水印(文字、圖片)
字元串編碼轉換string iconv ( string $in_charset ,
string $out_charset , string $str )

六、圖片旋轉
imagerotate();//制定角度的圖片翻轉

七、圖片的翻轉
沿X軸 沿Y軸翻轉

八、銳化
imagecolorsforindex()
imagecolorat()

『貳』 隨便下載一張圖片,使用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{
}

『叄』 php圖片可以等比例的縮放嗎

可以。

等比例縮放的方法是:

1、載入選區--自由變換。如下圖:

2、按住shift+alt鍵,使用滑鼠調整大小,這種情況下,選區會按照等比例的方法進行縮放的。

『肆』 php如何實現圖片的裁剪

php中裁剪圖片主要使用gd庫的imageresampled方法

$src_path='1.jpg';
//創建源圖的實例
$src=imagecreatefromstring(file_get_contents($src_path));

//裁剪開區域左上角的點的坐標
$x=100;
$y=12;
//裁剪區域的寬和高
$width=200;
$height=200;
//最終保存成圖片的寬和高,和源要等比例,否則會變形
$final_width=100;
$final_height=round($final_width*$height/$width);
//將裁剪區域復制到新圖片上,並根據源和目標的寬高進行縮放或者拉升
$new_image=imagecreatetruecolor($final_width,$final_height);
imageresampled($new_image,$src,0,0,$x,$y,$final_width,$final_height,$width,$height);
//輸出圖片
header('Content-Type:image/jpeg');
imagejpeg($new_image);
imagedestroy($src);
imagedestroy($new_image);

『伍』 php手機端圖片裁剪怎麼做

340改成 100%或是 你想適應的屏幕的比例

『陸』 php 裁切圖片問題 求助

PHP做圖片裁切

PHP做圖片裁剪的原理是通過前台定位出要裁剪圖片的左上角位置,及右下角位置的做標,如果有放大和縮小的話還要給出放大和縮小的百分比,以及要對哪一個圖片進行操作,這幾個值傳到後台,在用PHP去處理!

前端圖片坐標點的得到可以自己用JS去實現,如果你的JS寫的不是很熟也沒有關系,可以下載一個jsCropperUI這一個包裡面有很多的DEMO,可以實現前台的拖曳效果了!

前台會有一個AJAX向後台發送數據,一般格式為x1="+position[0]+"&y1="+position[1]+"&x2="+position[2]+"&y2="+position[3]+"&r="+position[4]+"&id = imgid四個坐標,縮放值,要操作的ID

後PHP接收這個數據後,進行操作

$x1 = $_GET['x1'];
$y1 = $_GET['y1'];
$x2 = $_GET['x2'];
$y2 = $_GET['y2'];
$r = $_GET['r'];
if($x2<$x1||$y2<$y1){
return false;
}
$img="dog.jpg";

$im=imagecreatefromjpeg($img);
$percent = $r*0.01;
list($width, $height) = getimagesize($img);
$new_width = $width * $percent;
$new_height = $height * $percent;

$tempimg = imagecreatetruecolor($new_width,$new_height);
imageresampled($tempimg, $im,0,0,0,0, $new_width,$new_height, $width, $height);
imagejpeg($tempimg,"dog_temp.jpg");
ImageDestroy ($im);

$ims=$tempimg;
$srcW=ImageSX($ims);
$srcH=ImageSY($ims);
$top_x = $x1;
$top_y = $y1;
$bottom_x = $x2;
$bottom_y = $y2;
$newimg = imagecreatetruecolor($bottom_x-$top_x,$bottom_y-$top_y);
imageresampled($newimg, $ims,0,0,$top_x,$top_y, $srcW,$srcH, $srcW, $srcH);
imagejpeg($newimg,"dogs.jpg");
ImageDestroy ($ims);

echo 1;

成功後輸出1讓前台得到進行裁剪區域的刷新!這樣就可以實現PHP裁剪圖片的效果了!

『柒』 PHP圖片裁剪

參考: https://www.cnblogs.com/-mrl/p/8760051.html

『捌』 如何實現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等比例壓縮圖片的實例代碼

具體代碼如下所示:
/**
*
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做一個縮放、裁剪後台圖片的頁面,該如何做

按照我的理解應該是這樣的 ,首先你的伺服器上面有很多圖片,但是圖片尺寸不符合使用要求,你想給一個cms使用者製作一個可以裁減縮放圖片的功能.
你的思路前面2步驟是沒有問題的 .
但是第三個步驟是不對的
我們平常所看到的裁減功能.例如BAI度的頭像設置.是用JS模擬一個框框來模擬裁減.只有當用戶真正點擊保存後.通過js記錄了裁減的起始位置.結束位置,寬高.等參數.通過php的GD庫來重新繪制這張圖片.保存並替換掉原來的 ,並且更新資料庫信息.知道用戶點擊保存之前.這張圖片的數據源都沒有發生變化.

閱讀全文

與php圖片裁剪縮放相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:758
蘋果郵件無法連接伺服器地址 瀏覽:963
phpffmpeg轉碼 瀏覽:672
長沙好玩的解壓項目 瀏覽:145
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:486
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:383
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:350
風翼app為什麼進不去了 瀏覽:779
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:151
伊克塞爾文檔怎麼進行加密 瀏覽:893
app轉賬是什麼 瀏覽:163