日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php图片编辑失真,PHP处理图片固定大小 不失真 不变形

發(fā)布時間:2023/12/2 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php图片编辑失真,PHP处理图片固定大小 不失真 不变形 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

由于工中使用到此代碼? 借鑒了各位前輩們代碼的基礎(chǔ)上修改而來 如果您的項目中有需要此代碼 要以盡情的復(fù)制和修改?;如果您有更好的代碼煩請告知本人?。

我將感激不盡

下面由上代碼

//圖像處理類

class Image {

private $file;//圖片地址

private $width;//圖片長度

private $height;//圖片長度

private $type;//圖片類型

private $img;//原圖的資源句柄

private $new;//新圖的資源句柄

//構(gòu)造方法,初始化

public function __construct($_file) {

$this->file = $_SERVER["DOCUMENT_ROOT"].$_file;

list($this->width, $this->height, $this->type) = getimagesize($this->file);

$this->img = $this->getFromImg($this->file, $this->type);

}

//cke專用圖像處理

public function ckeImg($new_width = 0,$new_height = 0) {

list($_water_width,$_water_height,$_water_type) = getimagesize(MARK);

$_water = $this->getFromImg(MARK,$_water_type);

if (empty($new_width) && empty($new_height)) {

$new_width = $this->width;

$new_height = $this->height;

}

if (!is_numeric($new_width) || !is_numeric($new_height)) {

$new_width = $this->width;

$new_height = $this->height;

}

if ($this->width > $new_width) { //通過指定長度,獲取等比例的高度

$new_height = ($new_width / $this->width) * $this->height;

} else {

$new_width = $this->width;

$new_height = $this->height;

}

if ($this->height > $new_height) { //通過指定高度,獲取等比例長度

$new_width = ($new_height / $this->height) * $this->width;

} else {

$new_width = $this->width;

$new_height = $this->height;

}

$_water_x = $new_width - $_water_width - 5;

$_water_y = $new_height - $_water_height - 5;

$this->new = imagecreatetruecolor($new_width,$new_height);

imagecopyresampled($this->new,$this->img,0,0,0,0,$new_width,$new_height,$this->width,$this->height);

if ($new_width > $_water_width && $new_height > $_water_height) {

imagecopy($this->new,$_water,$_water_x,$_water_y,0,0,$_water_width,$_water_height);

}

imagedestroy($_water);

}

//縮略圖(固定長高容器,圖像等比例,擴(kuò)容填充,裁剪)[固定了大小,不失真,不變形]

public function thumb($new_width = 0,$new_height = 0) {

if (empty($new_width) && empty($new_height)) {

$new_width = $this->width;

$new_height = $this->height;

}

if (!is_numeric($new_width) || !is_numeric($new_height)) {

$new_width = $this->width;

$new_height = $this->height;

}

//創(chuàng)建一個容器

$_n_w = $new_width;

$_n_h = $new_height;

//創(chuàng)建裁剪點(diǎn)

$_cut_width = 0;

$_cut_height = 0;

if ($this->width < $this->height) {

$new_width = ($new_height / $this->height) * $this->width;

} else {

$new_height = ($new_width / $this->width) * $this->height;

}

if ($new_width < $_n_w) { //如果新高度小于新容器高度

$r = $_n_w / $new_width; //按長度求出等比例因子

$new_width *= $r; //擴(kuò)展填充后的長度

$new_height *= $r; //擴(kuò)展填充后的高度

$_cut_height = ($new_height - $_n_h) / 2; //求出裁剪點(diǎn)的高度

}

if ($new_height < $_n_h) { //如果新高度小于容器高度

$r = $_n_h / $new_height; //按高度求出等比例因子

$new_width *= $r; //擴(kuò)展填充后的長度

$new_height *= $r; //擴(kuò)展填充后的高度

$_cut_width = ($new_width - $_n_w) / 2; //求出裁剪點(diǎn)的長度

}

$this->new = imagecreatetruecolor($_n_w,$_n_h);

imagecopyresampled($this->new,$this->img,0,0,$_cut_width,$_cut_height,$new_width,$new_height,$this->width,$this->height);

}

//加載圖片,各種類型,返回圖片的資源句柄

private function getFromImg($_file, $_type) {

switch ($_type) {

case 1 :

$img = imagecreatefromgif($_file);

break;

case 2 :

$img = imagecreatefromjpeg($_file);

break;

case 3 :

$img = imagecreatefrompng($_file);

break;

default:

Tool::alertBack('警告:此圖片類型本系統(tǒng)不支持!');

}

return $img;

}

//圖像輸出

public function out() {

imagepng($this->new,$this->file);

imagedestroy($this->img);

imagedestroy($this->new);

}

}

?>

CK專用是專門處理CK編輯器所傳圖片? 如有不需要的同學(xué)請刪除

總結(jié)

以上是生活随笔為你收集整理的php图片编辑失真,PHP处理图片固定大小 不失真 不变形的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。