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

php圖片縮放裁剪

發布時間:2022-08-15 17:46:51

『壹』 php裁剪 動態裁剪框,要求生成固定大小的圖片

<?php
$sourcefile = 'x.jpg';
#目標寬度
$newwidth = 150;
#目標高度
$newheight = 120;
#目標比例
$newbili = $newwidth / $newheight;

#源圖片寬高
list($width, $height) = getimagesize($sourcefile);

if($width / $height > $newbili){
#原圖較長
$w = $width - $newbili * $height;
$h = $height;
$x = ($width - $w) / 2;
$y = 0;
}else{
#原圖較寬
$w = $width;
$h = $height - $newbili * $width;
$x = 0;
$y = ($height - $h) / 2;
}

$source = imagecreatefromjpeg($sourcefile);
$thumb = imagecreatetruecolor($newwidth, $newheight);

imageresized($thumb, $source, 0, 0, $x, $y, $newwidth, $newheight, $w, $h);
imagejpeg($thumb, "a.jpg");

註:此程序未考慮原圖比目標圖片小的情況

『貳』 為什麼用PHP對上傳圖片進行裁剪的時候,沒成功,而且連上傳的原圖都沒了

加個base64_decode方法試試:
$src = imagecreatefromstring(base64_decode(file_get_contents($src_path)));

『叄』 如何實現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 圖片裁剪

那應該是你的代碼問題了,裁剪說白的就是前台把一張的圖片需要裁剪的位置傳給後台,後台通過圖片處理函數從新生成新的圖片。如果你後台代碼沒問題,就是你前台把裁剪位置的傳到後台的四個坐標點有問題,可以按照這個去排錯。(網上現成的很多,可以下載下來看看)

『伍』 php 上傳圖片,自動按比例只求寬裁剪,請問怎麼做呢

我只能提供步驟和思路,不要想直接拿代碼。。。
第一步:判斷圖片寬高,關鍵函數getimagesize($fullpath);如果超過寬度限制,根據當前寬高比例,算出壓縮後圖片正確的寬高。一點小演算法即可。
第二步:把當前圖片復制一份,關鍵函數imagecreatefromjpeg()等,根據圖片類型(後綴)不同,函數也稍有區別。
第三步:把剛才復制的備用文件壓縮,關鍵函數ImageCopyResampled或者ImageCopyResized 根據php版本和擴展不同選擇
第四步:把生成的縮略圖保存成文件,ImageJPEG等相應函數
完成

『陸』 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無組件裁剪圖片

functioncut_img($img,$w,$h){//要裁減的圖片,寬度,高度
$s=imagecreatefromjpeg($img);//這里以jpg圖片為例,其他圖片要修改這個方法名稱,可以上網參考(就是後面那個後綴名不一樣)
$w=imagesx($s)<$w?imagesx($s):$w;//如果圖片的寬比要求的小,則以原圖寬為准
$h=imagesy($s)<$w?imagesy($s):$h;
$bg=imagecreatetruecolor($w,$h);//創建$w*$h的空白圖像
if(image($bg,$s,0,0,0,0,$w,$h)){
if(imagejpeg($bg,"img/new_img.jpg")){//將生成的圖片保存到img/new_img.jpg
echo"success";
}else{
echo"false";
}
}else{
echo"false";
}
/*
*image($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h)
將src_im圖像中坐標從src_x,src_y開始,寬度為src_w,高度為src_h的一部分拷貝到dst_im圖像中坐標為dst_x和dst_y的位置上。
*/
imagedestroy($s);//關閉圖片
imagedestroy($bg);
//這里只寫了幾個主要操作,你可以再加上開始裁減的坐標,也就是image中的第5,第6兩個參數,那麼在判斷$w和$h的地方也要相應的剪掉開始沒算進去的部分,
//然後保存路徑是否存在的判斷等
}
cut_img("img/03.jpg",100,100);

『捌』 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手機端圖片裁剪怎麼做

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

閱讀全文

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

熱點內容
做賬為什麼要用加密狗 瀏覽:583
考研群體怎麼解壓 瀏覽:154
linux修改命令提示符 瀏覽:224
圓圈裡面k圖標是什麼app 瀏覽:59
pdf加空白頁 瀏覽:945
linux伺服器如何看網卡狀態 瀏覽:316
解壓新奇特視頻 瀏覽:704
圖書信息管理系統java 瀏覽:551
各種直線命令詳解 瀏覽:861
程序員淚奔 瀏覽:145
素材怎麼上傳到伺服器 瀏覽:515
android百度離線地圖開發 瀏覽:189
web可視化編程軟體 瀏覽:288
java筆試編程題 瀏覽:745
win11什麼時候可以裝安卓 瀏覽:563
java不寫this 瀏覽:1001
雲點播電影網php源碼 瀏覽:97
pythonclass使用方法 瀏覽:226
移動加密軟體去哪下載 瀏覽:294
php彈出alert 瀏覽:209